ようへい
ラベル Greasemonkey の投稿を表示しています。 すべての投稿を表示
ラベル Greasemonkey の投稿を表示しています。 すべての投稿を表示

2012年12月28日金曜日

JavaScript用 複数行文字列 → 文字列配列 変換ツール

JavaScript向けに、複数行文字列を文字列配列に変換するツールを作りました。

変換サンプル

#gbx3, #gbx4 {
    background-color: #2D2D2D;
    background-image: none;
    background-position: 0 -138px;
    background-repeat: repeat-x;
    border-bottom: 1px solid #000000;
    font-size: 24px;
    height: 29px;
    opacity: 1;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 990;
}
.gbtb .gbts {
    background: url("//ssl.gstatic.com/gb/images/b_8d5afc09.png") repeat scroll -27px -22px transparent;
    border: 0 none;
    font-size: 0;
    padding: 29px 0 0;
    width: 1px;
}
.gbtb .gbts {
    background: url('//ssl.gstatic.com/gb/images/b_8d5afc09.png') repeat scroll -27px -22px transparent;
    border: 0 none;
    font-size: 0;
    padding: 29px 0 0;
    width: 1px;
}
['#gbx3, #gbx4 {',
'background-color: #2D2D2D;',
'background-image: none;',
'background-position: 0 -138px;',
'background-repeat: repeat-x;',
'border-bottom: 1px solid #000000;',
'font-size: 24px;',
'height: 29px;',
'opacity: 1;',
'position: absolute;',
'top: 0;',
'width: 100%;',
'z-index: 990;',
'}',
'.gbtb .gbts {',
'background: url("//ssl.gstatic.com/gb/images/b_8d5afc09.png") repeat scroll -27px -22px transparent;',
'border: 0 none;',
'font-size: 0;',
'padding: 29px 0 0;',
'width: 1px;',
'}',
'.gbtb .gbts {',
'background: url(\'//ssl.gstatic.com/gb/images/b_8d5afc09.png\') repeat scroll -27px -22px transparent;',
'border: 0 none;',
'font-size: 0;',
'padding: 29px 0 0;',
'width: 1px;',
'}'];

変換ツール



関連記事

2012年10月17日水曜日

Firefox での CDATA の扱い

Firefox 17 beta1 に更新したところ、とあるGreasemonkeyスクリプトが動かなくなった。
スクリプト内部で使用している、CDATAが、Firefox 17ではsyntax errorとなっていた。
警告: E4X は非推奨となりました。 この機能は、Firefox 16 において content 向けにはデフォルトで無効となり、Firefox 17 では chrome 向けにもデフォルトで無効となる予定です。 そして、Firefox 18 において削除される予定となっています。 代わりに、DOMParser/DOMSerializer や非ネイティブの JXON アルゴリズムを使用してください。
JavaScript
E4X が無効化されました

 Bug 778851 – Turn javascript.options.xml.content off by default

ECMAScript for XML (E4X) は非推奨となり、Firefox 17 で無効化されました。隠し設定 javascript.options.xml.content の値を true に変更すれば有効になりますが、Firefox 18 Firefox の近い将来のバージョン (当初 Firefox 18 とされていましたが現状未定のようです) では実装そのものが完全に削除される予定です。
Firefox 17 のサイト互換性に関わる修正のまとめ | Mozilla Developer Street (modest)
https://dev.mozilla.jp/2012/10/firefox-17-site-compatibility/
これによると、ECMAScript for XML (E4X) は、Firefox 17ではデフォルトで無効、Firefox 18で削除予定らしい。
about:configで有効にできるみたいですけど、一時しのぎにしかならないな。
Greasemonkeyのページにも書いてあった。
E4X is deprecated. It will stop working in phases, Firefox 16 through 18.
Multi Line Strings - GreaseSpot Wiki
http://wiki.greasespot.net/Multi_Line_Strings
こうなると、CDATAで実装していたようなヒアドキュメントをJavaScriptで実装する手段が無いな・・・。
ということで、CDATAを文字列の配列に変換するスクリプトを作りました。
詳しくは以下のページを参照ください。

JavaScript内の CDATA → 文字列配列 変換スクリプト - ログろいど

しかし、これからGreasemonkey内にスタイルシート埋め込む時の手間が増えるな・・・。
何かいい方法無いだろうか。
その後
SpiderMonkey から E4X サポートが削除されました。
Firefox 21 ベータ版リリースノート
http://www.mozilla.jp/firefox/21.0/releasenotes/
Firefox 21で正式に削除となるようです。
関連記事

JavaScript内の CDATA → 文字列配列 変換ツール

Firefox 17から無効となるE4X
Greasemonkeyスクリプト内でCDATAを使用している方も多いと思います。(私もその一人でした)
そんな方のために、CDATAを文字列の配列に変換するスクリプトを作りました。

変換サンプル

var hoge=<><![CDATA[
<pre class="hoge" id='fuga'>
  hoge
</pre>
]]></>.toString();

var fuga=<><![CDATA[<pre class="hogehoge" id='fugafuga'>
  fuga
</pre>]]></>;

var fuga=<><![CDATA[<pre class="bar" id='bar'>bar</pre>]]></>.toString();
var hoge=['<pre class="hoge" id=\'fuga\'>',
'  hoge',
'</pre>'].join("\n");
 
var fuga=['<pre class="hogehoge" id=\'fugafuga\'>',
'  fuga',
'</pre>'].join("\n");
 
var fuga='<pre class="bar" id=\'bar\'>bar</pre>';

変換ツール



関連記事

2012年9月18日火曜日

[SyntaxHighlighter] Greasemonkey用Brush

Greasemonkey用のBrushを作ってみましたので、公開します。
Brushのエイリアスは、gmgreasemonkeyです。
このBrushを使うと、Greasemonkey APIが以下のようにハイライトされます。
GM_xmlhttpRequest({
  method: 'GET',
  url: 'http://www.google.co.jp/',
  timeout: 5000,
  onload: function(r){
    GM_addStyle(<><![CDATA[
#hoge {
  color: #CCC;
}
]]></>.toString());
    GM_log( GM_info.script.version +"\n"+r.responseText );
  }
});
以下で実際に確認することが可能です。
SyntaxHighlighter用タグ 変換/生成ツール - ログろいど

ダウンロード

共に、3.0.83専用です。
他のバージョンでの動作は未確認です。
非圧縮版(2.00KB)
https://sites.google.com/site/logroid/syntaxhighlighter/shBrushGreasemonkey.js
圧縮版(1.06KB)
https://sites.google.com/site/logroid/syntaxhighlighter/shBrushGreasemonkey.min.js
関連記事

[Greasemonkey] timeout と synchronous の関係について

先日の記事で、GM_xmlhttpRequesttimeoutパラメタを指定したらエラーを吐いた件で、詳細を調査してみました。

検証スクリプト

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.
GM_xmlhttpRequest - GreaseSpot Wiki
http://wiki.greasespot.net/GM_xmlhttpRequest
ちなみにtimeoutの実装が行われたコミットログは以下。
Allow timeout option for GM_xmlhttpRequest. · b4ecc6f · arantius/greasemonkey · GitHub
https://github.com/arantius/greasemonkey/commit/b4ecc6ff731ec9e38402fb46a3c7343908896828 こちらにも何も書かれていません。
とりあえずtimeout使いたいときはsynchronousfalseにすればいいという事がわかったので良しとするか。
関連記事

2012年8月26日日曜日

Greasemonkey 1.0 リリース

ついに、Greasemonkey 1.0 がリリースされました。
やっと安定版というところでしょうか。
Greasespot: Greasemonkey 1.0 Release
http://www.greasespot.net/2012/08/greasemonkey-10-release.html 1.0での主な変更点は、@grantという権限パラメタがMetaブロックに追加されています。
スクリプト内で使用しているGM_関数を、あらかじめ@grantで宣言しなければいけないようです。
Greasemonkey 1.0 adds a special Metadata Block imperative: @grant. As of version 1.0:

 If a script does not specify any @grant values, Greasemonkey will attempt to auto-detect the right settings.
 If a script specifies any values (or they have been auto detected), then it will be provided with only those API methods that it declares.
 The valid values are the names of those GM_ prefixed values that you wish your script to be granted access to. 
 Otherwise the script will be granted no special API privileges, and thus run without the security constraints Greasemonkey scripts have traditionally had. If you want your script to operate in this mode, you should explicitly declare @grant none.
@grant - GreaseSpot Wiki
http://wiki.greasespot.net/@grant
@grantの追記は大きな手間ではなかったのですが、1.0にすると以下のエラーが出るようになりました。
なお、@grantにGM_xmlhttpRequestは追記済みです。
[Exception... "A parameter or an operation is not supported by the underlying object"  code: "15" nsresult: "0x8053000f (InvalidAccessError)"  location: "chrome://greasemonkey/content/xmlhttprequester.js Line: 86"]
ちなみに、xmlhttprequester.jsの該当行は以下。
// this function is intended to be called in chrome's security context, so
// that it can access other domains without security warning
GM_xmlhttpRequester.prototype.chromeStartRequest =
function(safeUrl, details, req) {
  this.setupReferer(details, req);

  var setupRequestEvent = GM_util.hitch(this, 'setupRequestEvent', this.wrappedContentWin);

  setupRequestEvent(req, "abort", details);
  setupRequestEvent(req, "error", details);
  setupRequestEvent(req, "load", details);
  setupRequestEvent(req, "progress", details);
  setupRequestEvent(req, "readystatechange", details);
  setupRequestEvent(req, "timeout", details);
  if (details.upload) {
    setupRequestEvent(req.upload, "abort", details.upload);
    setupRequestEvent(req.upload, "error", details.upload);
    setupRequestEvent(req.upload, "load", details.upload);
    setupRequestEvent(req.upload, "progress", details.upload);
  }

  req.mozBackgroundRequest = !!details.mozBackgroundRequest;

  req.open(details.method, safeUrl,
      !details.synchronous, details.user || "", details.password || "");

  if (details.overrideMimeType) {
    req.overrideMimeType(details.overrideMimeType);
  }
  if (details.timeout) {
    req.timeout = details.timeout;
  }

  if (details.headers) {
    var headers = details.headers;

    for (var prop in headers) {
      if (Object.prototype.hasOwnProperty.call(headers, prop)) {
        req.setRequestHeader(prop, headers[prop]);
      }
    }
  }
どうやらGM_xmlhttpRequestのtimeoutパラメタが悪さしているみたい。
ということで、一時的にGM_xmlhttpRequestのtimeoutパラメタをコメントアウトすることで回避できました。
ちなみに以下のページから、0.9系をインストールして確認すると、上記のようなエラーは出ませんでした。
おそらく、1.0でのバグだと考えられます。
Greasemonkey :: Versions :: Add-ons for Firefox
https://addons.mozilla.org/ja/firefox/addon/greasemonkey/versions/ ちなみに、@grantを設定していないスクリプトで表示される警告メッセージはabout:configのgreasemonkey.showGrantsWarningをfalseにすることで、表示しないようにすることが出来るようです。

後日談

timeoutを設定した際にエラーを吐いた件は使い方がまずかったみたい。
[Greasemonkey] timeout と synchronous の関係について - ログろいど
関連記事