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

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

问题描述

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

 警报( '炉子'); 
警报(油腻);

我在 test.user.js p>

 (function(){

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

}());

'stovetop'会被警告,所以我知道页面的JavaScript工作,和文档.title 得到的变化,所以我知道脚本JavaScript的作品。然而,在网页上,我得到的错误:


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


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

解决方案

Greasemonkey脚本在单独的作用域中运行,也可以在沙箱中运行,取决于 @grant 设置。 / p>

  • 另外,问题代码在函数作用域中隔离 greasy >

  • 最后,默认情况下, test.js 会在Greasemonkey脚本执行前触发,所以它不会看到任何设置变量,无论如何。使用 @ run-at document-start 为了解决这个问题。






    因此, em> 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和目标页面,变量交互
    // @include http://YOUR_SERVER.COM/YOUR_PATH/*
    // @include http://jsbin.com/esikut/*
    // @ run-at document-start
    // @grant none
    // == / UserScript ==

    // ---对于@grant none,也可以使用window 。而不是unsafeWindow。
    unsafeWindow.gmScripts_GlobalVar ='油腻';
    $ b $ console.log(在GM脚本中,本地全局:,unsafeWindow.targetPages_GlobalVar);
    console.log(在GM脚本中,全局脚本:,gmScripts_GlobalVar);

    window.addEventListener(DOMContentLoaded,function(){
    console.log(在GM脚本中,本地全局,准备就绪后,unsafeWindow.targetPages_GlobalVar);
    },false);



    使用沙盒,函数作用域 unsafeWindow

    ==> 重要更新 Greasemonkey更改2.0版的unsafeWindow处理,下一个示例脚本将不适用于GM 2.0或更高版本。其他两个解决方案仍然有效。

      // == UserScript == 
    // @name _Greasemonkey和目标页面,变量交互
    // @include http://YOUR_SERVER.COM/YOUR_PATH/*
    // @include http://jsbin.com/esikut/*
    // @ run-在document-start
    // @grant GM_addStyle
    // == / UserScript ==
    / * - 需要@grant指令来解决设计变更
    介绍GM 1.0。它恢复沙箱。
    * /

    unsafeWindow.gmScripts_GlobalVar ='油腻';
    $ b $ console.log(在GM脚本中,本地全局:,unsafeWindow.targetPages_GlobalVar);
    console.log(在GM脚本中,全局脚本:,unsafeWindow.gmScripts_GlobalVar);

    window.addEventListener(DOMContentLoaded,function(){
    console.log(在GM脚本中,本地全局,准备就绪后,unsafeWindow.targetPages_GlobalVar);
    },false);



    使用沙箱,没有函数作用域,脚本注射


      // == UserScript == 
    // @name _Greasemonkey和目标页面,变量交互
    // @include http://YOUR_SERVER.COM/YOUR_PATH/*
    // @include http://jsbin.com/esikut/*
    // @ run-at document-start
    // @grant GM_addStyle
    // == / UserScript ==
    / * - 需要@grant指令来解决设计更改
    在GM 1.0中引入。它恢复沙箱。
    * /

    函数GM_main(){
    window.gmScripts_GlobalVar ='greasy';
    $ b $ console.log(在GM脚本中,本地全局:,window.targetPages_GlobalVar);
    console.log(在GM脚本中,script global:,window.gmScripts_GlobalVar);

    window.addEventListener(DOMContentLoaded,function(){
    console.log(在GM脚本中,本地全局,准备就绪后,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. 您可以在 < window 是相同的。

    2. 所有这些脚本在控制台上产生相同的输出:

      在GM脚本中,局部全局:undefined
      在GM脚本中,全局脚本:greasy
      在$ {
      $}目标页面,本地全局:炉灶
      在目标页面上,全局脚本:greasy
      在GM脚本中,本地全局,准备就绪后:炉灶


    3. Script Injection 代码可以在Firefox以外的各种浏览器中使用。 unsafeWindow 目前仅适用于Firefox + Greasemonkey(或Scriptish)或Chrome + Tampermonkey。
    4. 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?

      解决方案


      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);
      

      Then the following will work:

      No sandbox:

      // ==UserScript==
      // @name        _Greasemonkey and target page, variable interaction
      // @include     http://YOUR_SERVER.COM/YOUR_PATH/*
      // @include     http://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);
      


      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://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);
      


      With sandbox, no function scope, Script Injection:

      // ==UserScript==
      // @name        _Greasemonkey and target page, variable interaction
      // @include     http://YOUR_SERVER.COM/YOUR_PATH/*
      // @include     http://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. You can test these script against this page (jsbin.com/esikut).
      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
        

      4. 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天全站免登陆