views:

125

answers:

0

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?