2010-02-08
サーバーサイド jQuery をやってみる!
最近、社内で PHP Spidermonkey が流行って(?)いるようです><!
Cybozu Inside Out: SpiderMonkeyを使ってPHPでサーバーサイドJavaScript
id:ama-ch さすがです><
というわけで
僕も、 PHP の Spidermonkey でどのくらいのことが出来るのか試してみました><
まず、 Hello, world!
<?php // new して $js = new JSContext(); // print 関数作って $js->registerFunction(function($v) { print $v; }, 'print'); // こんにちはこんにちは! $js->evaluateScript('print("Hello, world!!")');
Hello, world!
おおお、簡単!
次は、 id:m-hiyama さんが作った minidom.js を読み込んでみる!
minidom.js は Pure JavaScript による DOM の実装です!
詳しくはこちら
<?php $js = new JSContext(); $js->registerFunction(function($v) { print $v; }, 'print'); // minidom.js を読み込んで $js->evaluateScript(file_get_contents('http://www.chimaira.org/tools/minidom.js.txt')); // div 要素を作ってみる! $js->evaluateScript('print(document.createElement("div").toString(0))');
<div ></div>
なんかでたー!
おおお! minidom.js ++
調子に乗って、 John Resig さんが作った htmlparser.js を読み込んでみる!
htmlparser は Pure JavaScript で書かれた HTML パーサーです!
詳しくはこちら
<?php $js = new JSContext(); $js->registerFunction(function($v) { print $v; }, 'print'); $js->evaluateScript(file_get_contents('http://www.chimaira.org/tools/minidom.js.txt')); // htmlparserjs を読み込む! $js->evaluateScript(file_get_contents('http://ejohn.org/files/htmlparser.js')); // JavaScript を用意! $script = <<< END HTMLParser("<html><body>foo<div>bar</div>baz</body></html>", { start: function(tag) { print(tag + "(start)\\n"); }, chars: function(text) { print(text + "(text)\\n"); }, end: function(tag) { print(tag + "(end)\\n"); } }) END; // 実行!!1 $js->evaluateScript($script);
どきどき
html(start) body(start) foo(text) div(start) bar(text) div(end) baz(text) body(end) html(end)
おおおお、ちゃんとパースできたあああ
パースして DOM を構築してみる
こんどは、 htmlparser でパースした HTML を minidom.js の DOM 実装に乗せてみる
<?php $js = new JSContext(); $js->registerFunction(function($v) { print $v; }, 'print'); $js->evaluateScript(file_get_contents('http://www.chimaira.org/tools/minidom.js.txt')); $js->evaluateScript(file_get_contents('http://ejohn.org/files/htmlparser.js')); // ちょっと htmlparser と minidom を拡張 $js->evaluateScript(file_get_contents('http://amachang.sakura.ne.jp/misc/php_jquery/fix.js')); // html を読み込んでみる! $js->assign('html', file_get_contents('http://amachang.sakura.ne.jp/misc/php_jquery/pasee.html')); // パース実行! $js->evaluateScript('parseHtml(html, document);'); // toSource で DOM 構造を見て見る! print $js->evaluateScript('document.documentElement.toSource()');
#2={_root:#1={_root:#1#, ownerDocument:#1#, parentNode:null, nodeType:9, nodeName:"#document", _name:"main", _idMap:{}, childNodes:[#2#], firstChild:#2#, lastChild:#2#, documentElement:#2#, implementa ....
おおお、なんかパースできてそう!!
jQuery を使ってみる!
でっきるかなー!
<?php $js = new JSContext(); $js->registerFunction(function($v) { print $v; }, 'print'); $js->evaluateScript(file_get_contents('http://www.chimaira.org/tools/minidom.js.txt')); $js->evaluateScript(file_get_contents('http://ejohn.org/files/htmlparser.js')); $js->evaluateScript(file_get_contents('http://amachang.sakura.ne.jp/misc/php_jquery/fix.js')); $js->assign('html', file_get_contents('http://amachang.sakura.ne.jp/misc/php_jquery/pasee.html')); $js->evaluateScript('parseHtml(html, document)'); // jquery を読み込む! $js->evaluateScript(file_get_contents('http://code.jquery.com/jquery.js')); // JavaScript を実行! print $js->evaluateScript('Array.join($(".hoge"), "\n")');
<div class="hoge" >foo</div> <div class="hoge" >bar</div> <div class="hoge" >baz</div>
きたーヽ(;'□')ノ
というわけで
なんとか PHP の Spidermonkey で jQuery を動かすことが出来ました!ぱちぱちー
でも、適当かつ無理やり動かしてるので jQuery でも動かない機能が多々あると思います><
ごめんなさいごめんなさい><
まあ、ともあれ JavaScript がサーバーサイドで動くってーのはめっちゃ楽しいですね!
ではではーヽ(`・ω・´;)ノ
トラックバック - http://d.hatena.ne.jp/amachang/20100208/1265640443
リンク元
- 468 http://reader.livedoor.com/reader/
- 342 http://b.hatena.ne.jp/hotentry
- 296 http://pipes.yahoo.com/pipes/pipe.info?_id=3eebace824bb60a10f13c841c2c64478
- 281 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja-JP-mac:official&hs=7o&q=IT戦記&btnG=検索&lr=lang_ja&aq=f&oq=
- 217 http://pipes.yahoo.com/pipes/pipe.info?_id=faa858a20082ef6d25ad27557e37e011
- 215 http://nplll.com/mutter/archives/2009/03/ie8ie8ie7ie6.php
- 204 http://twitter.com/amachang
- 188 http://b.hatena.ne.jp/hotentry/it
- 183 http://www.google.com/reader/view/
- 178 http://www.google.co.jp/reader/view/
