gpt4 book ai didi

javascript - 为什么将授权从无更改为 GM_xmlhttpRequest 会破坏我的代码?

转载 作者:行者123 更新时间:2023-11-29 17:54:23 29 4
gpt4 key购买 nike

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

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

我考虑过执行 xmlhttpRequest,但不知为何也被阻止了。我通过在控制台中执行代码进行了测试。除了 Facebook 域外,这 3 行似乎在任何地方都有效。我相信这与 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));
};
})();

最佳答案

当您授予 GM_xmlhttpRequest 时,it switches on the sandbox -- 这意味着您不能像那样访问 window.XMLHttpRequest,因为它现在处于不同的上下文中。

要解决此问题,请使用 script injection拦截 AJAX。并且,使用 messaging或自定义事件以访问用户脚本上下文中的数据。

这是一个使用自定义事件的示例脚本(不易受到第 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);
}

关于javascript - 为什么将授权从无更改为 GM_xmlhttpRequest 会破坏我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623574/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com