【问题标题】:Accessing Variables from Greasemonkey to Page & vice versa从 Greasemonkey 访问变量到页面,反之亦然
【发布时间】:2012-11-09 05:22:10
【问题描述】:

我在

标签: javascript firefox firefox-addon greasemonkey


【解决方案1】:

您的变量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);

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

【讨论】:

  • 我刚刚使用了一个greasemonkey 模板脚本,它在匿名函数中有示例代码。我认为它是greasemonkey 脚本所必需的。我将 var 声明移到函数之外,但仍然无法从页面上的 test.js 脚本中提醒变量值。
  • 会不会是我的 test.js 脚本在 Greasemonkey 之前运行?如果是这样,我该如何延迟?
【解决方案2】:
  • Greasemonkey 脚本在单独的范围内运行,也可以在沙箱中运行,具体取决于@grant settings

  • 此外,问题代码将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 changed unsafeWindow handling with version 2.0, the next sample script will not work with GM 2.0 or later。其他两种解决方案仍然有效。

// ==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. 您可以针对this page (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。

【讨论】:

  • btw wiki.greasespot.net/UnsafeWindow 说使用 unsafeWindow 是不安全的,在这种情况下有什么更安全的选择?
  • “更安全”的替代方案是使用@grant none 方法,但将unsafeWindow 替换为window(尽管当@grant none 生效时它们是相同的), 使用 Script Injection 方法。 ...但是,使用unsafeWindow 的风险被大大夸大了——没有报告真正的漏洞利用。只要您不在积极针对 GM 脚本的网站上,就应该没有风险。
【解决方案3】:

你也可以使用localStorage:

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

localStorage.getItem("numberOfThings");

【讨论】:

    猜你喜欢
    • 2017-01-08
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多