开发者

greasemonkey: perform GM_xmlhttpRequest() from eval (follow up)

开发者 https://www.devze.com 2022-12-26 09:47 出处:网络
How can you call GM_xmlhttpRequest inside of an eval where you are evaling some complicated code, some of which calls GM_xmlhttpRequest.

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 开发者_StackOverflow社区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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消