gpt4 book ai didi

javascript - GM_xmlhttpRequest : Why is it never firing the onload in Firefox?

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

此代码在 Chrome 中运行良好。但是,在 Firefox 中,当它遇到 GMxhr 请求时,它就会停止。它一直到 GMxhr 调用,然后就...停止。我已经检查了所有我能想到的东西,确保 responseType 参数只为 chrome 等设置......但仍然没有。有什么想法吗?

var body       = document.getElementsByTagName('body')[0]
, messageDiv = document.createElement('div')
;

messageDiv.id = 'xhrComlink';
body.appendChild(messageDiv);

function getUri(e) {
'use strict';
var chrome = navigator.userAgent.toString().toLowerCase().indexOf('chrome') !== -1;

var bin2base64 = function bin2base64 (binary) {
...
};


var storeRetrievedFile = function storeRetrievedFile(response) {
console.log(2);
var thisComlink = e.target
, evt = document.createEvent("MouseEvents")
;

var text = response.responseText
, len = text.length
, arr = new Uint8Array(len)
, i = 0
;

if (!chrome) {
for( i = 0; i < len; ++i ) {
arr[i] = text.charCodeAt(i) & 0xFF;
}
}

thisComlink.innerHTML = '';
thisComlink.appendChild(
document.createTextNode(
chrome ? bin2base64(response.responseText) : bin2base64(arr.buffer)
)
);

evt.initMouseEvent("dblclick", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
thisComlink.dispatchEvent(evt);
};

var gmXOptions = { method : 'GET'
, overrideMimeType : 'text/plain; charset=x-user-defined'
, onload : storeRetrievedFile
, url : e.target.innerHTML
};

chrome && (gmXOptions.responseType = 'arraybuffer');

console.log(1);

GM_xmlhttpRequest(gmXOptions);
}

在控制台中,我在 Firefox 上只得到“1”。在 Chrome 中,我得到“1”、“2”,它会重写 innerHTML,触发事件,然后开始。

我还检查以确保 Firefox 一切正确,gmXOptions 的目录与 log(1) 调用位于同一位置:

火狐:

method  "GET"
overrideMimeType "text/plain; charset=x-user-defined"
url "http://www.home...e-and-land-packages.jpg"
onload storeRetrievedFile(response)

编辑以解释答案;也许这会在将来帮助其他人:

Firefox 的 GM 有一个烦人的行为/错误:

1) 编写附加事件监听器的用户脚本。在该事件监听器函数中,例如上面的 getUri(),使用 GM_ 函数。

2) 现在从任何其他 javascript 上下文调用该事件的触发器, 刚刚创建监听器的上下文除外。例如,使用常见的 Chrome 友好的“注入(inject) jquery,回调到 main(),继续”模式。

3) 但是这样做,您将无法访问 main() 中的 GM 函数。所以你必须把它们放在脚本的开头,在 main() 之外,在你注入(inject) jQuery 之前,然后在那些使用 GM(GM 上下文)的函数和 main 中的函数(一个注入(inject)的,非-GM 上下文),在这种情况下带有事件。

结果)在 GM 术语中,unsafeWindow 正在调用 GM_xmlhttpRequest。这会触发 Firefox Greasemonkey 中的“安全”,并且它默默地阻止对 GM_xmlhttpRequest 的调用。您必须使用解决方法来“清理”堆栈,使 Firefox GM 的“安全性”满意。然后它调用 GM_xmlhttpRequest。

最佳答案

我终于找到了问题所在,尽管 Firefox Greasemonkey 真的很难找到它,而且无论出于何种原因,它根本没有抛出任何错误/警告/等消息,这可能有助于更快地解决这个问题。

无论如何,这就是问题所在:http://wiki.greasespot.net/0.7.20080121.0_compatibility

添加

 setTimeout(function() {

}, 0);

围绕 GM_xhr 调用,现在一切正常。

关于javascript - GM_xmlhttpRequest : Why is it never firing the onload in Firefox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11007605/

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