DomParserをGreasemonkeyから使う

GreaseMonkeyって面白い。指定のページを開くと対応したjavascriptを走らせることが可能。

http://diveintogreasemonkey.org/toc/
を参考にコードを書いてみていたら, new DomParser()で、
DomParser is not constructorと怒られる。

少し調べてみると、解決。新しい版では作法が変わっている模様。
http://la.ma.la/blog/diary_200601100209.htm

var wp = new XPCNativeWrapper(window, "DOMParser()");
var parser = new wp.DOMParser();

GM_xmlhttpRequestのコールバック関数中で、XPath検索がうまく行かず、サイズ0が返される。今のところ'//*'はうまく行っている模様。

namespace付のXMLの場合は、name space resoloverを第3引数に渡してやる必要がありました。
http://www-xray.ast.cam.ac.uk/~jgraham/mozilla/xpath-tutorial.html

document.evaluate("//html:td/mathml:math", document, NSResolver, XPathResult.ANY_TYPE, null);

function NSResolver(prefix) {
if(prefix == 'html') {
return 'http://www.w3.org/1999/xhtml';
}
else if(prefix == 'mathml') {
return 'http://www.w3.org/1998/Math/MathML'
}
else {
//this shouldn't ever happen
return null;
}
}