Greasemonkey:从 eval 执行 GM_xmlhttpRequest() (后续)
如何在 eval
内部调用 GM_xmlhttpRequest
,在其中 eval
处理一些复杂的代码,其中一些代码调用 GM_xmlhttpRequest
>。
这是从 eval 执行 GM_xmlhttpRequest() 的后续内容,
这是一些示例代码:
// ==UserScript==
// @name Test GM AJAX
// ==/UserScript==
console = unsafeWindow.console;
function fetch(msg) {
console.log('fetching: '+msg);
GM_xmlhttpRequest({
method: 'GET',
url: 'http://google.com',
onload: function(responseDetails) {
console.log(msg);
}
});
}
function complicated(arg1, arg2) {
fetch(arg1 + arg2);
}
console.log('trying');
var code = 'complicated("Ya", "y!")';
function myEval(code) {
eval(code);
eval('setTimeout(function(){'+code+'},0)');
eval('setTimeout(fetch,0)');
eval('setTimeout(function(){console.log("here");fetch("cool")},0)');
fetch("BOO");
}
myEval(code);
输出:
trying
fetching: Yay!
fetching: BOO
fetching: Yay!
fetching: 30
here
fetching: cool
BOO
30
所以唯一有效的获取是setTimeout(fetch,0)
但我需要实际执行包含复杂代码的code
。
有什么想法吗?
How can you call GM_xmlhttpRequest
inside of an eval
where you are eval
ing some complicated code, some of which calls GM_xmlhttpRequest
.
This is a follow up to Perform GM_xmlhttpRequest() from eval
Here is some sample code:
// ==UserScript==
// @name Test GM AJAX
// ==/UserScript==
console = unsafeWindow.console;
function fetch(msg) {
console.log('fetching: '+msg);
GM_xmlhttpRequest({
method: 'GET',
url: 'http://google.com',
onload: function(responseDetails) {
console.log(msg);
}
});
}
function complicated(arg1, arg2) {
fetch(arg1 + arg2);
}
console.log('trying');
var code = 'complicated("Ya", "y!")';
function myEval(code) {
eval(code);
eval('setTimeout(function(){'+code+'},0)');
eval('setTimeout(fetch,0)');
eval('setTimeout(function(){console.log("here");fetch("cool")},0)');
fetch("BOO");
}
myEval(code);
which outputs:
trying
fetching: Yay!
fetching: BOO
fetching: Yay!
fetching: 30
here
fetching: cool
BOO
30
So the only fetch that worked was the setTimeout(fetch,0)
but I need to actually execute the code
which includes come complicated code.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为我也有同样的问题,几个月后他们似乎没有答案。
如果您只想在一台机器上或像我一样在开发环境中运行此代码,您可以考虑更改 Greasmonkey Addon 源:
那里只有两个字母:“//”
在 Components/greasemonkey.js,第 47 行 ff
但要小心,因为您可能会遇到 Mal 脚本的安全问题。
as I have the same problem, just months later and their seems no answer out there.
If you only want to run this code on one machine or in an developing environmetn as I do, you might consider changing the Greasmonkey Addon source:
There it is only two letters: "//"
in components / greasemonkey.js, line 47 ff
But take care, as you mya get security problems with mal scripts.