Greasemonkey:从 eval 执行 GM_xmlhttpRequest() (后续)

发布于 2024-08-28 11:27:14 字数 1181 浏览 3 评论 0原文

如何在 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 evaling 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2024-09-04 11:27:14

因为我也有同样的问题,几个月后他们似乎没有答案。

如果您只想在一台机器上或像我一样在开发环境中运行此代码,您可以考虑更改 Greasmonkey Addon 源:
那里只有两个字母:“//”
在 Components/greasemonkey.js,第 47 行 ff

 if (stack.filename != null &&
      stack.filename != gmSvcFilename &&
      stack.filename.substr(0, 6) != "chrome") {
    GM_logError(new Error("Greasemonkey access violation: unsafeWindow " +
                "cannot call " + apiName + ". --> DISABED"));
    // return false;  OUT-COMMENT THIS LINE 
  }

但要小心,因为您可能会遇到 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

 if (stack.filename != null &&
      stack.filename != gmSvcFilename &&
      stack.filename.substr(0, 6) != "chrome") {
    GM_logError(new Error("Greasemonkey access violation: unsafeWindow " +
                "cannot call " + apiName + ". --> DISABED"));
    // return false;  OUT-COMMENT THIS LINE 
  }

But take care, as you mya get security problems with mal scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文