I’m new at Greasemonkey. I’m trying to create a user script to improve my user experience with a site I often visit.
So I’d like to create a javascript function f(). My function requires a value that, according to my quick research and try at understanding the site’s structure, only exists inside a javascript function g().
I am not sure of which file holds g() on the server, but what I do know is that once the page has finished downloading, g() can be used.
I would like to extract the string value from g() without executing it (with g.toString()).
My question is: how can I access g(), without compromising my computer with unsafeWindow?
(window.g returns null inside the Greasemonkey script).
You have to use
unsafeWindow. It’s really not that evil.unsafeWindowis relatively safe. I have previously discovered a method to access an unrestrictedwindowobject in GreaseMonkey. Using a specific method, it’s possible to read the original code of the Userscript by the affected page. Specific GreaseMonkey functions (GM_getValue, ..) cannot be used though: Advanced GreaseMonkey: Using constructors/methods/variables at a remote pageEDIT, regarding the title change
If you fear that
gis not a function, or that thetoStringmethod of the function is overwritten, use the following code:Is it safe?
The previous code is the safest approach, because no methods of
gare invoked. The GreaseMonkey wrapper also prevents the affected page from reading the script:The previous code will not show the real caller, but an useless wrapper function: