2006-01-27
■ Greasemonkeyでprototype.jsやscript.aculo.usを使う方法
Greasemonkeyでprototype.jsやscript.aculo.usが使えたら便利だろうな、と考えたことのある開発者は少なくないのではないでしょうか。ちょっとそんなアイデアを試してみたことのある方ならわかると思うのですが、Greasemonkeyでそういった外部JavaScriptライブラリは簡単には使えません。案外ハードルが高いです。
Googleで調べていたら、面白い解決方法が見つかったので紹介します。
Loading External JavaScript Libraries in Greasemonkey
このブログで解説されているアプローチを用いれば、Greasemonkeyで外部JavaScriptライブラリが使えるようになります。
ポイントは、
といったところでしょうか。
このブログの解説を参考にして、簡単なサンプルを作ってみました。このGreasemonkeyをインストールすると、各サイトの一番下に"hello"と表示されます。その"hello"の文字をクリックすると、Effect.Fade()によって"hello"の文字がフェードアウトします。自作のlibs.jsを通して、prototype.jsとscript.aculo.usに含まれるeffects.jsをロードし、それらのライブラリに含まれる機能の一部をGreasemonkeyで利用しています。
// ==UserScript==
// @name GM Hack
// @namespace http://vaio.redirectme.net/lib/greasemonkey/
// @description Loads external javascript libraries on the fly
// @include *
// ==/UserScript==
(function() {
// See: Loading External JavaScript Libraries in Greasemonkey -
// http://manalang.com/archives/2006/01/04/loading-external-javascript-libraries-in-greasemonkey/
var l = unsafeWindow;
function waitForLoadLibs() {
if (typeof l.Prototype != 'undefined' && typeof l.Effect != 'undefined') {
// The libraries have been loaded.
// Now you can use the libraries via the 'l' variable.
// Create a greeting div section and append it to the body element.
var greeting = document.createElement("div");
greeting.id = "hello";
greeting.innerHTML = "<h1>hello</h1>";
document.body.appendChild(greeting);
// $() is provided by prototype.js.
// Effect.Fade() is provided by effects.js(script.aculo.us).
hello = l.$('hello');
hello.addEventListener('click',
function() { new l.Effect.Fade(hello); }, false);
} else {
// Try again...
window.setTimeout(waitForLoadLibs, 100);
}
}
function loadLibs() {
// Note: 'libs.js' loads prototype.js and effects.js(script.aculo.us).
var libs_js = document.createElement("script");
libs_js.type = "text/javascript";
libs_js.src = "http://your.machine/js/libs.js";
var head = document.getElementsByTagName("head")[0];
head.appendChild(libs_js);
waitForLoadLibs();
}
window.addEventListener('load', loadLibs(), false);
})();
ちなみに、libs.jsでは以下のようにしてライブラリをロードしています。
// prototype.js
var prototype_js = document.createElement("script");
prototype_js.type = "text/javascript";
prototype_js.src = "http://your.machine/js/prototype.js";
// effects.js
var effects_js = document.createElement("script");
effects_js.type = "text/javascript";
effects_js.src = "http://your.machine/js/effects.js";
// load scripts
var head = document.getElementsByTagName("head")[0];
head.appendChild(prototype_js);
head.appendChild(effects_js);
unsafeWindowを経由してライブラリにアクセスするため、いちいち"l."が必要になるのがちょっと気になりますね。
l.$(...); l.Effect.Fade(...);
トラックバック - http://d.hatena.ne.jp/ysano2005/20060127/1138382734
- http://d.hatena.ne.jp/aki77/20060130
- http://d.hatena.ne.jp/Yuichirou/20060222
- http://d.hatena.ne.jp/ku__ra__ge/20060203
- http://d.hatena.ne.jp/Yuichirou/10000401
- http://d.hatena.ne.jp/ysano2005/20060305
- これまで作ったGreasemonkeyスクリプト まとめ
- http://d.hatena.ne.jp/daftbeats/20070519
- greasemonkeyでnucleusの管理画面をいじってみました その2
- http://d.hatena.ne.jp/re_guzy/20070731
- 365日誓約マラソン - prototype.jsまとめ
- 365日誓約マラソン - Greasemonkeyでprototype.jsをやってみたけど...
- Ulmhaft - (今さら)ニコニコ動画でflvを抽出するGreasemonkey
- 365日誓約マラソン - greasemonkeyから外部ライブラリ読み込む
リンク元
- 2 http://d.hatena.ne.jp/nipotan/20060105
- 2 http://del.icio.us/toshi8350?v=3&url=http://d.hatena.ne.jp/ysano2005/20060127/1138382734&title=CMS researcher - Greasemonkeyでprototype.jsやscript.aculo.usを使う方法
- 1 http://a.hatena.ne.jp/kunit/
- 1 http://a.hatena.ne.jp/waka0930/
- 1 http://b.hatena.ne.jp/entrylist?sort=eid&url=http://d.hatena.ne.jp/ysano2005/
- 1 http://d.hatena.ne.jp/keyword/prototype.js
- 1 http://feedbringer.net/feed
- 1 http://r.hatena.ne.jp/tosik/検索/
- 1 http://search.yahoo.co.jp/search?p=メモリーが少ない&fr=top&src=top
- 1 http://sleipnir.excite.co.jp/search.gw?target=combined&search=pound+再Configure&look=sleipnir_jp&lang=all&pref=all

