设置@grant值时如何访问`window`(目标页面)对象?

igl*_*vzx 5 javascript firefox greasemonkey

假设我正在使用以下网页:

<html>
<body>
<span id="click">click me</span>
<script>
var hello = function() {
    alert('hello');
}

document.getElementById('click').addEventListener('click', function(e) {
    hello();
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的Greasemonkey脚本是:

// ==UserScript==
// @name        My Script
// @include     http://example.com/hello.html
// @version     1
// @grant       none
// ==/UserScript==

window.hello = function() {
    alert('goodbye');
}
Run Code Online (Sandbox Code Playgroud)

禁用Greasemonkey脚本后,单击#click页面上的元素将显示"hello"警报.启用脚本后,单击该元素将显示"再见"警报.

很简单.在hello从网页的功能正在由Greasemonkey的脚本功能所取代.

现在让我们说我想使用Greasemonkey API.当我将@grant值设置为除'none'之外的有效值(例如// @grant GM_setClipboard)[导致Greasemonkey将脚本作为"内容脚本"运行时,而不是像'none'那样在页面的范围内运行),Greasemonkey脚本无法工作.

window.hello 不再定位页面上的正确对象.

更换window.hellounsafeWindow.hello看起来像它的工作,而是下面的错误是在JS控制台抛出:

错误:拒绝访问对象的权限

如何在@grant GM_setClipboard设置目标并替换hello页面上的原始函数时重写Greasemonkey脚本?

系统信息:

  • Windows 7 64位
  • Firefox 32.0
  • Greasemonkey 2.2

Bro*_*ams 17

当您设置@grant非none以外的值时,Greasemonkey会激活其沙箱,Greasemonkey 2.0会彻底更改unsafeWindow处理.

现在,为了在目标页面范围中创建或覆盖变量,您必须从技术菜单中正确选择.例如:

阅读:

致电:

写/设置:


考虑这个HTML:

<button id="helloBtn">Say "Hello".</button>
Run Code Online (Sandbox Code Playgroud)

这个javascript:

var simpleGlobalVar = "A simple, global var in the page scope.";
var globalObject    = {Letter: "A", Number: 2};

function simpleFunction () {
    console.log ("The target page's simpleFunction was called.");
}

var sayHello = function() {
    console.log ('Hello.');
}

document.getElementById ('helloBtn').addEventListener ('click', function () {
    sayHello ();
} );
Run Code Online (Sandbox Code Playgroud)

你可以在这个jsFiddle页面看到它.

如果您针对该页面安装并运行此Greasemonkey脚本:

// ==UserScript==
// @name     _Demonstrate accessing target-page variables with @grant values set
// @include  http://fiddle.jshell.net/sepwL7n6/*/show/
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==

console.log ("*** Greasemonkey script start.");

$("body").append ('<div id="gmArea">Added by Greasemonkey:<p></p></div>');
$("#gmArea > p:first").append ('<button id="gmShow">Access select target-page variables and functions</button>');
$("#gmArea > p:first").append ('<button id="gmChange">Change javascript things in the target-page scope.</button>');

$("#gmShow").click ( function () {
    //-- Access things from the target-page scope:
    console.log ("----------------");
    console.log ("==> simpleGlobalVar is: ", unsafeWindow.simpleGlobalVar);
    console.log ("==> globalObject    is: ", unsafeWindow.globalObject);
    console.log ("==> Calling target's simpleFunction():");
    unsafeWindow.simpleFunction ();

    //-- WARNING! This next technique is not robust, but works in some cases.
    console.log ("==> Calling target's button's click().");
    unsafeWindow.document.getElementById ('helloBtn').click ();
} );

$("#gmChange").click ( function () {
    this.disabled = true;   //-- Can only click once.
    unsafeWindow.simpleGlobalVar    = "Simple var... Intercepted by GM!";
    unsafeWindow.globalObject       = cloneInto (gmObject, unsafeWindow);
    unsafeWindow.sayHello           = exportFunction (sayHello, unsafeWindow);
    console.log ("==> Target page objects were changed.");
} );

var gmMessageStr    = "Function... Intercepted by GM, but also can use GM_ functions!";
function sayHello () {
    sayHello.K      = (sayHello.K  ||  0) + 1;
    console.log (gmMessageStr);
    GM_addStyle ('body {background: ' + (sayHello.K % 2  ?  "lime"  :  "white") + ';}');
}
var gmObject        = {message: "Object overridden by GM."};
Run Code Online (Sandbox Code Playgroud)


打开控制台并按下按钮,您将看到GM脚本能够读取和更改页面的变量和功能.


笔记:

  1. 这是所有Firefox特有的.
  2. 对于跨平台代码以及某些复杂情况,您可以使用脚本注入.但是注入的代码不能直接访问GM_函数.
  3. 请注意,这些技术仅适用于全局的javascript变量和函数.