Mercurial > my-ubiquity-commands
diff random-stuff/random-stuff.js @ 21:e82bff1ef296
Changed code to be more html-like than JS-ish.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 07 May 2009 15:26:40 -0700 |
parents | a07982ba7259 |
children | ecd868a32247 |
line wrap: on
line diff
--- a/random-stuff/random-stuff.js Wed May 06 09:20:13 2009 -0700 +++ b/random-stuff/random-stuff.js Thu May 07 15:26:40 2009 -0700 @@ -75,81 +75,54 @@ return iframe; } -function pageLoad_inject_sidebar_functions(document) { - var window = document.defaultView; - var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIWebNavigation) - .QueryInterface(Ci.nsIDocShellTreeItem) - .rootTreeItem - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow); - - document = null; +function logError(element) { + var window = element.ownerDocument.defaultView; + var console = window.wrappedJSObject.console; + console.error.apply(console, arguments); +} - var functionsToImport = { - _addSidebar: function _addSidebar(url, width) { - if (typeof(url) != "string") - throw new Error("URL must be a string"); - - if (typeof(width) != "number") - throw new Error("width must be a number"); - - var panelIframe = addStatusBarPanel(chromeWindow, url, width); - chromeWindow = null; +function absolutifyUrl(document, url) { + var anchor = document.createElement("a"); + anchor.setAttribute("href", url); + document.body.appendChild(anchor); + var absolute = anchor.href; + document.body.removeChild(anchor); + return absolute; +} - panelIframe.addEventListener( - "load", - function(loadEvent) { - var panelDocument = loadEvent.originalTarget; - var evt = window.document.createEvent("MessageEvent"); - var origin = (panelDocument.location.protocol + "//" + - panelDocument.location.host); - evt.initMessageEvent("message", - false, - true, - "sidebar created", - origin, - "", - panelDocument.defaultView); - window.addEventListener( - "unload", - function() { panelIframe.parentNode.removeChild(panelIframe); }, - true - ); - window.dispatchEvent(evt); - window = null; - }, - true); +function pageLoad_inject_sidebar_functions(document) { + if (document.body && + document.body.firstChild && + document.body.firstChild.nodeName == "FEATURE") { + var window = document.defaultView; + var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsIDocShellTreeItem) + .rootTreeItem + .QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindow); + var widgets = document.getElementsByTagName("widget"); + for (var i = 0; i < widgets.length; i++) { + var widget = widgets[i]; + var location = widget.getAttribute("location"); + if (location == "status-bar") { + var href = widget.getAttribute("href"); + if (typeof(href) == "string") { + href = absolutifyUrl(document, href); + var width = widget.getAttribute("width"); + if (width && width.match(/^[0-9]+$/)) { + var panelIframe = addStatusBarPanel(chromeWindow, href, width); + window.addEventListener( + "unload", + function() { panelIframe.parentNode.removeChild(panelIframe); }, + true + ); + } else + logError(widget, "must specify width as integer"); + } else + logError(widget, "must specify href"); + } else + logError(widget, "has unknown location", location); } - }; - - var functionsToEval = { - addSidebar: function addSidebar(options) { - window.addEventListener( - "message", - function onMessage(event) { - // TODO: Make this secure by looking at the origin, etc. - window.console.log(event); - if (event.data == "sidebar created") { - window.removeEventListener("message", onMessage, false); - var panel = event.source; - options.callback(panel); - } - }, - false - ); - - // Absolut-ify the URL if necessary. - var anchor = window.document.createElement("a"); - anchor.setAttribute("href", options.url); - window.document.body.appendChild(anchor); - options.url = anchor.href; - window.document.body.removeChild(anchor); - - window._addSidebar(options.url, options.width); - } - }; - - importFunctionsIntoWindow(functionsToImport, window); - evalFunctionsIntoWindow(functionsToEval, window); + } }