代码之家  ›  专栏  ›  技术社区  ›  Paul Tarjan

greasemonkey:从eval执行GM\u xmlhttpRequest()(后续)

  •  0
  • Paul Tarjan  · 技术社区  · 16 年前

    你怎么打电话 GM_xmlhttpRequest 在一个 eval 你在哪 评估 通用汽车公司要求 .

    这是对 Perform GM_xmlhttpRequest() from eval

    下面是一些示例代码:

    // ==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

    有什么想法吗?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Claudio    15 年前

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

    如果您只想像我一样在一台计算机或开发环境中运行此代码,您可以考虑更改Greasmonkey加载项源: 只有两个字母:“//” 组件中/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 
      }
    

    但是要小心,因为你可能会遇到错误脚本的安全问题。