栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Greasemonkey访问变量到Page,反之亦然

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从Greasemonkey访问变量到Page,反之亦然

  • 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/ @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或更高版本。其他两个解决方案仍然有效。

// ==Userscript==// @name        _Greasemonkey and target page, variable interaction// @include     http://YOUR_SERVER.COM/YOUR_PATH/ @run-at      document-start// @grant    GM_addStyle// ==/Userscript==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/ @run-at      document-start// @grant       GM_addStyle// ==/Userscript==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. 由于没有沙箱中,
    unsafeWindow
    并且
    window
    是相同的。
  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。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号