为什么将 grant 从 none 更改为 GM_xmlhttpRequest 会破坏我的代码?

3 javascript greasemonkey facebook userscripts tampermonkey

简而言之,我不想将警报 URL 和响应正文发送到我的应用程序。此代码有效,但除非我授予它,否则我无法使用 GM_xmlhttpRequest。

不做任何改变,代码就会神奇地崩溃。我不确定发生了什么变化以及如何修复它。我以为我可以使用console.log数据并将其复制/粘贴到我的应用程序中,但 Facebook 禁用了 console.log。

我想过做 xmlhttpRequest 但这也被某种方式阻止了。我通过在控制台中执行代码进行测试。这 3 行似乎在除了 Facebook 域之外的任何地方都有效。我认为这与CORS有关。

// ==UserScript==
// @name        FBTest
// @namespace   test
// @include     https://*.facebook.com/*
// @version     1
// @grant       none
// ==/UserScript==
//change none to GM_xmlhttpRequest
(function() {
    var proxied = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function(method, url) {
        alert(url);
        return proxied.apply(this, [].slice.call(arguments));
    }; 
})();
Run Code Online (Sandbox Code Playgroud)

Bro*_*ams 6

当您授予 GM_xmlhttpRequest 时,它会打开沙箱- 这意味着您无法window.XMLHttpRequest像现在在不同的上下文中那样访问。

要解决此问题,请使用脚本注入来拦截 AJAX。并且,使用消息传递或自定义事件来访问用户脚本上下文中的数据。

以下是使用自定义事件的示例脚本(不易受到第 3 方攻击):

// ==UserScript==
// @name        _Intercept AJAX with grant/sandbox on
// @match       https://*.facebook.com/*
// @grant       GM_xmlhttpRequest
// ==/UserScript==

function xmlOpenIntercept () {
    var proxied = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function (method, newUrl) {
        var cEvnt = new CustomEvent ('newAjaxStart', {'detail': newUrl} );
        document.body.dispatchEvent (cEvnt);

        return proxied.apply (this, [].slice.call (arguments) );
    };
}
addJS_Node (null, null, xmlOpenIntercept);  //-- Injects code


//--- This code listens for the right kind of message.
document.body.addEventListener ("newAjaxStart", receiveAjaxMessage);

function receiveAjaxMessage (zEvent) {
    console.log ("Intercepted AJAX to: ", zEvent.detail);
}

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad)  scriptNode.addEventListener ("load", runOnLoad);
    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);
}
Run Code Online (Sandbox Code Playgroud)