访问从 Greasemonkey 到 Page & 的变量反之亦然 [英] Accessing Variables from Greasemonkey to Page & vice versa

查看:27
本文介绍了访问从 Greasemonkey 到 Page & 的变量反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

(function () {

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

}());

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

'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:

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

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

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

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

推荐答案

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

    此外,问题代码将greasy隔离在一个函数范围内(如gladoscc所说).

    Additionally, the question code isolates greasy in a function scope (as gladoscc said).

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

    Finally, by default, test.js will fire before the Greasemonkey script does, so it won't see any set variables, anyway. Use @run-at document-start to address that.


    所以,给定这个 test.js,在 之前运行:


    So, given this test.js, run right before </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 或更高版本.其他两种解决方案仍然有效.


    With sandbox, no function scope, unsafeWindow:
    ==> Important update: Greasemonkey changed unsafeWindow handling with version 2.0, the next sample script will not work with GM 2.0 or later. The other two solutions still work.

    // ==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);
    }
    

    <小时>

    注意事项:


    Notes:

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

    1. You can test these script against this page (output.jsbin.com/esikut/1).
    2. With no sandbox, unsafeWindow and window are the same.
    3. All of these scripts produce the same output on the console:

    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.

  • The Script Injection code will work in a variety of browsers besides Firefox. unsafeWindow currently only works in Firefox+Greasemonkey(or Scriptish) or Chrome+Tampermonkey.

    这篇关于访问从 Greasemonkey 到 Page &amp; 的变量反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆