Skip to content

Instantly share code, notes, and snippets.

@teramako
Created January 20, 2010 11:37
Show Gist options
  • Save teramako/281786 to your computer and use it in GitHub Desktop.
Save teramako/281786 to your computer and use it in GitHub Desktop.
Jetpack Feature: test XPath
jetpack.future.import("slideBar");
let NS = {
xhtml: "http://www.w3.org/1999/xhtml",
xul: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
};
function nsResolver(prefix){
return NS[prefix] || null;
}
function evaluate(xpath, elm){
if (!elm) elm = jetpack.tabs.focused.contentDocument;
let doc = elm.ownerDocument ? elm.ownerDocument : elm;
let res = doc.evaluate(xpath, elm, nsResolver, 7, null);
res.__iterator__ = function(){
for (let i=0, len=this.snapshotLength; i<len; i++)
yield this.snapshotItem(i);
};
return res;
}
var anchorListId = "anchorList";
jetpack.slideBar.append({
with:400,
html: <html>
<style><![CDATA[
h1 { font-size: mediume; }
ul { font-size: small; }
]]></style>
<body>
<h1>Anchor List</h1>
<ul id={anchorListId}></ul>
</body>
</html>,
onSelect: function(widget){
let ul = $("#"+ anchorListId, widget.contentDocument)
ul.empty();
for (let a in evaluate("//a"))
if (a.textContent)
ul.append(<li><a href={a.href}>{a.textContent}</a></li>.toXMLString());
}
})
// vim: sw=2 ts=2 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment