I have a site running in a few environments on different URLs (i.e. dev.mysite.com, qa.mysite.com, uat.mysite.com, etc).
There is a Greasemonkey script (myGM.user.js) at each of the environments but its version can be different and depending on the environment a different web service needs to be called to get some data.
In my user script, I would like to get the URL from which that user script was originally installed, so I could then extract the host and use it as a prefix for my web service URL.
For example, if my script is installed from
http://dev.mysite.com/myGM.user.js,
I want to get
http://dev.mysite.com/myGM.user.js,
extract
http://dev.mysite.com/
from it and call
http://dev.mysite.com/myWebService/getData
for data.
What’s the easiest way to accomplish that? Thanks!
I’ve ended up adding an ASPX page (could be PHP or whatever) to the same folder as
myGM.user.js. It simply determines the current host (server-side) and outputs it as text.So if you browsed to
http://dev.mysite.com/getHost.aspxthe response would be
http://dev.mysite.com/And if you browsed to
http://uat.mysite.com/getHost.aspxresponse would be
http://uat.mysite.com/Then, in my
myGM.user.jsI’ve added// @resource rootUrl GetSiteRoot.aspxThis resource directive takes a URL relative to the URL from where the user script is installed from and downloads the response alongside user script itself.
This allows me to call
GM_getResourceText('rootUrl')in the body of the user script and know exactly where my script was installed from w/o having to make any changes from environment to environment.
P.S. I will, however, submit a feature request for this to be added GM_Info API.