11

我在 </body> 之前运行的test.js中有以下代码:

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

我在test.user.js中有以下代码:

(function () {

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

}());

'stovetop' 收到警报,所以我知道页面 javascript 有效,并document.title得到更改,所以我知道脚本 javascript 有效。但是,在网页上我收到错误:

错误:ReferenceError:未定义油腻源文件:/test.js

我如何从网页访问 Greasemonkey 设置的变量,反之亦然?

4

3 回答 3

31
  • Greasemonkey 脚本在单独的范围内运行,也可能在沙箱中运行,具体取决于@grant设置

  • 此外,问题代码greasy在函数范围内隔离(如gladoscc所说)。

  • 最后,默认情况下,test.js将在 Greasemonkey 脚本之前触发,因此无论如何它都不会看到任何设置的变量。用来@run-at document-start解决这个问题。


所以,给定这个test.js,在之前运行</body>

window.targetPages_GlobalVar = 'stovetop';

console.log ("On target page, local global: ", targetPages_GlobalVar);
console.log ("On target page, script global: ", gmScripts_GlobalVar);

然后以下将起作用:

没有沙箱:

// ==UserScript==
// @name        _Greasemonkey and target page, variable interaction
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*
// @include     http://output.jsbin.com/esikut/*
// @run-at      document-start
// @grant       none
// ==/UserScript==

//--- For @grant none, could also use window. instead of unsafeWindow.
unsafeWindow.gmScripts_GlobalVar = 'greasy';

console.log ("In GM script, local global: ", unsafeWindow.targetPages_GlobalVar);
console.log ("In GM script, script global: ", gmScripts_GlobalVar);

window.addEventListener ("DOMContentLoaded", function() {
    console.log ("In GM script, local global, after ready: ", unsafeWindow.targetPages_GlobalVar);
}, false);


使用沙箱,没有功能范围,unsafeWindow:
==>重要更新: Greasemonkey 更改了 2.0 版的 unsafeWindow 处理,下一个示例脚本将不适用于 GM 2.0 或更高版本。其他两种解决方案仍然有效。

// ==UserScript==
// @name        _Greasemonkey and target page, variable interaction
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*
// @include     http://output.jsbin.com/esikut/*
// @run-at      document-start
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

unsafeWindow.gmScripts_GlobalVar = 'greasy';

console.log ("In GM script, local global: ", unsafeWindow.targetPages_GlobalVar);
console.log ("In GM script, script global: ", unsafeWindow.gmScripts_GlobalVar);

window.addEventListener ("DOMContentLoaded", function() {
    console.log ("In GM script, local global, after ready: ", unsafeWindow.targetPages_GlobalVar);
}, false);


使用沙箱,无功能范围,脚本注入

// ==UserScript==
// @name        _Greasemonkey and target page, variable interaction
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*
// @include     http://output.jsbin.com/esikut/*
// @run-at      document-start
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

function GM_main () {
    window.gmScripts_GlobalVar = 'greasy';

    console.log ("In GM script, local global: ", window.targetPages_GlobalVar);
    console.log ("In GM script, script global: ", window.gmScripts_GlobalVar);

    window.addEventListener ("DOMContentLoaded", function() {
        console.log ("In GM script, local global, after ready: ", window.targetPages_GlobalVar);
    }, false);
}

addJS_Node (null, null, GM_main);

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

笔记:

  1. 您可以针对此页面(output.jsbin.com/esikut/1)测试这些脚本。
  2. 没有沙箱,unsafeWindowwindow一样。
  3. 所有这些脚本在控制台上产生相同的输出:

    In GM script, local global: undefined
    In GM script, script global: greasy
    On target page, local global: stovetop
    On target page, script global: greasy
    In GM script, local global, after ready: stovetop
    
  4. 脚本注入代码将在除 Firefox 之外的各种浏览器中工作。unsafeWindow目前仅适用于 Firefox+Greasemonkey(或 Scriptish)或 Chrome+Tampermonkey。

于 2012-11-21T02:55:16.613 回答
2

您的变量greasy是在匿名函数的范围内定义的。您甚至无法greasy在您的用户脚本中访问,除非它是您功能的一部分。例子:

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

这样做:

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

另外,为什么要将所有代码放在匿名函数中然后执行它?

于 2012-11-21T01:46:35.780 回答
2

您还可以使用localStorage

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

localStorage.getItem("numberOfThings");
于 2018-10-09T21:12:01.600 回答