先日の記事で、
GM_xmlhttpRequestに
timeoutパラメタを指定したらエラーを吐いた件で、詳細を調査してみました。
検証スクリプト
GM_xmlhttpRequestのみを実行するスクリプトを組んだ。
こんな感じ。
// ==UserScript==
// @name GM_xmlhttpRequest Test
// @namespace http://logroid.blogspot.jp/
// @version 1
// @description GM_xmlhttpRequest 検証スクリプト
// @include http://127.0.0.1/*
// @include http://127.0.0.1:*/*
// @grant GM_log
// @grant GM_xmlhttpRequest
// ==/UserScript==
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.google.co.jp/',
timeout: 5000,
onload: function(r){
GM_log("synchronous:false\n"+r.responseText);
}
});
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.google.co.jp/',
timeout: 5000,
synchronous:true,
onload: function(r){
GM_log("synchronous:true\n"+r.responseText);
}
});
実行
スクリプトをインストールし、実行した。
成功すればGoogleのトップのHTMLソースがコンソールに出力されるはず。
synchronous:falseになっているGM_xmlhttpRequestは成功した。
しかし、
synchronous:trueになっているGM_xmlhttpRequestでは以下のエラーが出力された。
エラー: A parameter or an operation is not supported by the underlying object
ソースファイル: file:///C:/Users/hoge/AppData/Roaming/Mozilla/Firefox/Profiles/hoge/gm_scripts/GM_xmlhttpRequest_Test/gm_xmlhttprequest_test.user.js
行: 96
検証結果
どうやら、GM_xmlhttpRequestのtimeoutはsynchronousがfalseの場合のみ有効なパラメタであるようです。
ただし、リファレンスを見ても、そのようなことは記載されていませんでした。
エラーメッセージから推測するとFirefoxが出していると思うので、Greasemonkeyではなく、Firefox側の制限であると考えられます。
- synchronous
- Boolean (Compatibility: 0.9.9+) When true, this is a synchronous request. Be careful: The entire Firefox UI will be locked and frozen until the request completes. In this mode, more data will be available in the return value.
- timeout
- Number (Compatibility: Greasemonkey 1.0+, Firefox 12) The number of milliseconds to wait before terminating the call; zero (the default) means wait forever.
ちなみにtimeoutの実装が行われたコミットログは以下。
Allow timeout option for GM_xmlhttpRequest. · b4ecc6f · arantius/greasemonkey · GitHub
https://github.com/arantius/greasemonkey/commit/b4ecc6ff731ec9e38402fb46a3c7343908896828
こちらにも何も書かれていません。
とりあえず
timeout使いたいときは
synchronousを
falseにすればいいという事がわかったので良しとするか。