Created
July 18, 2012 16:17
-
-
Save Infocatcher/3137247 to your computer and use it in GitHub Desktop.
Ugly JXON for Custom Buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xul = { | |
$tagName: "menupopup", | |
$xmlns: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", | |
$property: { toString: function() { return "[Object RawProperty]"; } }, | |
$oncommand: function(e) { | |
alert(e.target.getAttribute("value")); | |
}, | |
oncommand: "this.oncommand(event);", | |
$childNodes: [ | |
{ $tagName: "menuitem", label: "Item - 0", value: "0" }, | |
{ $tagName: "menuitem", label: "Item - 1", value: "1" } | |
] | |
}; | |
var o = { | |
parseFromObject: function(o) { | |
if("$textContent" in o) | |
return document.createTextNode(o.$textContent); | |
var node = document.createElementNS( | |
o.$xmlns || "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", | |
o.$tagName | |
); | |
for(var p in o) if(o.hasOwnProperty(p)) { | |
if(p == "$childNodes") { // Child nodes | |
o[p].forEach(function(child) { | |
node.appendChild(this.parseFromObject(child)); | |
}, this); | |
continue; | |
} | |
if(p.charAt(0) == "$") { // Properties | |
if(p != "$tagName" && p != "$xmlns") | |
node[p.substr(1)] = o[p]; | |
continue; | |
} | |
// Attributes | |
var attr = o[p]; | |
if(typeof attr == "object") | |
node.setAttributeNS(attr.$xmlns, p, attr.value); | |
else | |
node.setAttribute(p, attr); | |
} | |
return node; | |
} | |
}; | |
var menu = o.parseFromObject(xul); | |
document.documentElement.appendChild(menu); | |
//alert(new XMLSerializer().serializeToString(menu)); | |
alert(menu.property); | |
menu.showPopup(); | |
setTimeout(function() { | |
menu.parentNode.removeChild(menu); | |
}, 1e4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment