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

Posted by Paul Tarjan on Stack Overflow See other posts from Stack Overflow or by Paul Tarjan
Published on 2010-04-13T08:39:58Z Indexed on 2010/04/13 8:43 UTC
Read the original article Hit count: 255

Filed under:
|
|

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 http://stackoverflow.com/questions/1074236

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?

© Stack Overflow or respective owner

Related posts about greasemonkey

Related posts about AJAX