2

これは私のGM_xmlhttpRequestスクリプトです:

// ==UserScript==
// @name        test
// @namespace   test
// @include     http://stackoverflow.com/*
// @version     1
// ==/UserScript==

GM_xmlhttpRequest({
  method: "GET",
  url: "http://example.com",
  onload: function(response) {
    alert(response.responseText);
  }
});

function begin(){
    alert("ready");
}

$(document).ready(function() {
    begin();
}); 

「準備完了」ではなく、example.com の内容のみを警告します。

しかし、私が次のことをしても何も起こりません - アラートはまったくありません:

function begin(){
    GM_xmlhttpRequest({
      method: "GET",
      url: "http://example.com",
      onload: function(response) {
        alert(response.responseText);
      }
    });
    alert("ready");
}

$(document).ready(function() {
    begin();
}); 

私は何を間違っていますか?

4

1 に答える 1

3

最初の例は、GM_xmlhttpRequest によって返されたコンテンツを示していると確信していますが、「ready」は示していません。

jQuery/$ は、Greasemonkey 内で直接アクセスすることはできません。ページ内にロードされます(この場合はstackoverflow.comによって)。ページの機能/プロパティにアクセスするには、unsafeWindow オブジェクト ( http://wiki.greasespot.net/UnsafeWindow )を使用できます。

unsafeWindow.$(document).ready(function() {
    begin();
}); 

ただし、begin() を直接呼び出すことをお勧めします。ここでは必要ありません $.ready()。DOMContentLoaded イベントが発生すると、GM スクリプトが常に実行されるためです。$.ready()

于 2013-01-02T11:15:38.983 に答える