<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2006 &gt; June &gt; 19 &gt; ふぁっきん not well-formed, XMLHttpRequest とかで</title>
  <link href="http://lowreal.net/logs/2006/06/19/1"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2006/06/19/1.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2006/06/19/1.xhtml"/>
  <updated>2006-06-19T02:08:11+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2006/06/19/1</id>
  <entry>
    <title>ふぁっきん not well-formed, XMLHttpRequest とかで</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2006/06/19/1.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2006/06/19/1.xhtml"/>
    <updated>2006-06-19T02:08:11+09:00</updated>
    <published>2006-06-19T02:08:11+09:00</published>
    <id>http://lowreal.net/2006/06/19/1</id>
    <category term="js"/>
    <category term="gm"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>GreaseMonkey では GM_xmlhttpRequest で他のページとってくるじゃないですか。で、onload して req.responseXML をとるなり req.responseText をとるなりするけど、どうにもこうにも not well-formed なページ (HTML とか) をとってきてノード検索するとき面倒くさいんですよ。responseXML はもちろん空だし、responseText はもちろんただのテキストだし。</p>
        <p>で、なんとなく思いついたので以下のように解決するようにした。</p>
        <pre class="ECMAScript">
GM_xmlhttpRequest({
	method : "GET",
	url : i.href,

	onload : function (req) {
		// てきとーなエレメント作って突っ込む (ブラウザにパースさせる)
		var d = document.createElement("div");
		d.innerHTML = req.responseText;

		// d を最初のコンテキストノードにして文書を XPath 検索
		$X(".//foobar", d);
	},

	onerror : function (req) {
		alert(req.responseText);
	}
});
</pre>
        <p>なんてキモイ！</p>
        <p>問題点は application/xhtml+xml なページから not well-formed なページをとってくるっていう場合は使えないこと。なぜなら innerHTML が使えないから。でも殆どの場合 (というか殆どのページは) text/html なので問題ない感じ。はてなとかはてなとかはてなとか。</p>
        <ins datetime="2006-06-20T16:59:02+09:00">
          <p>Firefox 1.5 からは application/xhtml+xml なページでも innerHTML 使えるみたいです。by <a xmlns="http://www.w3.org/2005/Atom" href="http://susu.whitesnow.jp/note/">yoko さん</a></p>
        </ins>
      </div>
    </content>
  </entry>
</feed>
