我在 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正常工作。但是,在网页上我得到了错误:
document.title
错误:ReferenceError:未定义油腻性源文件:/test.js
如何从网页访问Greasemonkey设置的变量,反之亦然?
Greasemonkey脚本在单独的范围内运行,也可能在沙箱中运行,具体取决于@grant设置。
@grant
另外,问题代码隔离greasy在功能范围内(如gladoscc所说)。
greasy
最后,默认情况下, test.js 将在Greasemonkey脚本之前启动,因此无论如何它都不会看到任何设置变量。使用@run-at document-start来解决这个问题。
@run-at document-start
因此,鉴于此 test.js ,请在运行之前</body>:
</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更改了unsafeWindow 2.0版本的处理,下一个示例脚本将不适用于GM 2.0或更高版本。其他两个解决方案仍然有效。
unsafeWindow
// ==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); }
笔记:
window
所有这些脚本在控制台上产生相同的输出:
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
该 脚本注入 代码将在各种除了Firefox浏览器的工作。unsafeWindow目前仅适用于Firefox + Greasemonkey(或Scriptish)或Chrome + Tampermonkey。