Accessing Variables from Greasemonkey to Page & vice versa

greasemonkey unsafewindow
greasemonkey grant
unsafewindow is not defined
tampermonkey namespace
tampermonkey document ready
tampermonkey console log
tampermonkey notification
tampermonkey implicit global variable

I have the following code in test.js which is run right before </body>:

alert('stovetop');
alert(greasy);

I have the following code in test.user.js:

(function () {

    'use strict';
    var greasy = 'greasy variable';
    document.title = 'greasy title';

}());

'stovetop' gets alerted so I know the page javascript works, and document.title gets changes so I know that the script javascript works. However, on the webpage I get the error:

Error: ReferenceError: greasy is not defined Source File: /test.js

How from the webpage do I access the variable set by Greasemonkey and how about vice versa?

cannot access javascript-variable of original page · Issue #2653 , Greasemonkey 4.1beta2 Firefox 58.0b1 - Linux/Kubuntu a script example for: http​://www.greasespot.net/ (picked a random variable to display in  With the update to Greasemonkey 4, many userscripts using jQuery seem to have been broken. In the previous versions, the following code would work on a page using jQuery in a <script> tag: // ==UserScript== // @name Variable access test

Your variable greasy is defined in the scope of the anonymous function. You cannot access greasy even in your userscript, unless it is part of your function. Example:

(function(){
    var foo = 5;
    alert(foo);
}();
alert(foo); //ERROR, because foo is undefined outside of the function.

Do it this way:

var foo = 5;
(function(){
     alert(foo);
}();
alert(foo);

Also, why are you putting all your code in an anonymous function and then executing it?

Can't access variables defined in <script> tags on the actual page , With the update to Greasemonkey 4, many userscripts using jQuery seem to have been broken. In the previous versions, the following code  In order to access variables on the page, use the unsafeWindow object. To use values defined in a script, simply reference them by their names. Since Mozilla provides a rich environment, there are a wide variety of things that have not been imported from the general content scope into the Greasemonkey sandbox. Including, but not limited to:

You can also use localStorage:

localStorage.setItem("numberOfThings", "42");

localStorage.getItem("numberOfThings");

Greasemonkey / Wiki / unsafeWindow, This API object allows a User script to access "custom" properties--variable and functions defined in the page--set by the web page. The unsafeWindow object is​  Accessing Variables from Greasemonkey to Page & vice versa. Greasemonkey scripts operate in a separate scope and may also operate in a sandbox, depending on the @grant settings. Additionally, the question code isolates greasy in a function scope (as gladoscc said). Finally, by default, test.js will fire before the

Greasemonkey Manual:Environment, In order to access variables on the page, use the unsafeWindow object. To use values defined in a script, simply reference them by their names. You are using a global variable, but global variables only last as long as the page does so when you refresh you are clearing all global variables. The only way to save data past a page refresh is with a cookie, upload to the a server, or the HTML5 storage API. With greasemonkey probably you would want to use a cookie.

Greasemonkey, ipt variable loaded in the page from a Greasemonkey script. I tried the var name directly , or using window.varname (which works in the  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Accessing Variables from Greasemonkey to Page & vice

Flickr Mashups, access this JavaScript variable from a Greasemonkey script, you need to use the unsafeWindow is a direct reference to the window object in the web page. This API object allows a User script to access "custom" properties--variable and functions defined in the page--set by the web page. The unsafeWindow object is shorthand for window.wrappedJSObject. It is the raw window object inside the XPCNativeWrapper provided by the Greasemonkey Sandbox.

Comments
  • If you go this way, it'll only work in firefox as Chrome sandboxes userscripts.
  • btw wiki.greasespot.net/UnsafeWindow say's its unsafe to use unsafeWindow, what would a safer alternative be in this case?
  • The "safer" alternative would be to either use the @grant none method but replace unsafeWindow with window (although they are identical when @grant none is in effect), or to use the Script Injection method. ... However, the risk of using unsafeWindow is greatly overblown -- with no real exploits reported. As long as you aren't on a site that's actively targeting GM scripts, there should be no risk.
  • I just used a greasemonkey template script and it had the example code in the anonymous function. I assumed it was required for greasemonkey scripts. I moved the var declaration outside of the function and I still can't alert the variable value from the test.js script on page.
  • Could it be my test.js script is being run before Greasemonkey? If so how can I delay it?