comparison 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
comparison
equal deleted inserted replaced
20:a07982ba7259 21:e82bff1ef296
73 iframe.addEventListener("DOMContentLoaded", onPanelLoad, true); 73 iframe.addEventListener("DOMContentLoaded", onPanelLoad, true);
74 statusBar.appendChild(iframe); 74 statusBar.appendChild(iframe);
75 return iframe; 75 return iframe;
76 } 76 }
77 77
78 function logError(element) {
79 var window = element.ownerDocument.defaultView;
80 var console = window.wrappedJSObject.console;
81 console.error.apply(console, arguments);
82 }
83
84 function absolutifyUrl(document, url) {
85 var anchor = document.createElement("a");
86 anchor.setAttribute("href", url);
87 document.body.appendChild(anchor);
88 var absolute = anchor.href;
89 document.body.removeChild(anchor);
90 return absolute;
91 }
92
78 function pageLoad_inject_sidebar_functions(document) { 93 function pageLoad_inject_sidebar_functions(document) {
79 var window = document.defaultView; 94 if (document.body &&
80 var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) 95 document.body.firstChild &&
81 .getInterface(Ci.nsIWebNavigation) 96 document.body.firstChild.nodeName == "FEATURE") {
82 .QueryInterface(Ci.nsIDocShellTreeItem) 97 var window = document.defaultView;
83 .rootTreeItem 98 var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
84 .QueryInterface(Ci.nsIInterfaceRequestor) 99 .getInterface(Ci.nsIWebNavigation)
85 .getInterface(Ci.nsIDOMWindow); 100 .QueryInterface(Ci.nsIDocShellTreeItem)
86 101 .rootTreeItem
87 document = null; 102 .QueryInterface(Ci.nsIInterfaceRequestor)
88 103 .getInterface(Ci.nsIDOMWindow);
89 var functionsToImport = { 104 var widgets = document.getElementsByTagName("widget");
90 _addSidebar: function _addSidebar(url, width) { 105 for (var i = 0; i < widgets.length; i++) {
91 if (typeof(url) != "string") 106 var widget = widgets[i];
92 throw new Error("URL must be a string"); 107 var location = widget.getAttribute("location");
93 108 if (location == "status-bar") {
94 if (typeof(width) != "number") 109 var href = widget.getAttribute("href");
95 throw new Error("width must be a number"); 110 if (typeof(href) == "string") {
96 111 href = absolutifyUrl(document, href);
97 var panelIframe = addStatusBarPanel(chromeWindow, url, width); 112 var width = widget.getAttribute("width");
98 chromeWindow = null; 113 if (width && width.match(/^[0-9]+$/)) {
99 114 var panelIframe = addStatusBarPanel(chromeWindow, href, width);
100 panelIframe.addEventListener( 115 window.addEventListener(
101 "load", 116 "unload",
102 function(loadEvent) { 117 function() { panelIframe.parentNode.removeChild(panelIframe); },
103 var panelDocument = loadEvent.originalTarget; 118 true
104 var evt = window.document.createEvent("MessageEvent"); 119 );
105 var origin = (panelDocument.location.protocol + "//" + 120 } else
106 panelDocument.location.host); 121 logError(widget, "must specify width as integer");
107 evt.initMessageEvent("message", 122 } else
108 false, 123 logError(widget, "must specify href");
109 true, 124 } else
110 "sidebar created", 125 logError(widget, "has unknown location", location);
111 origin,
112 "",
113 panelDocument.defaultView);
114 window.addEventListener(
115 "unload",
116 function() { panelIframe.parentNode.removeChild(panelIframe); },
117 true
118 );
119 window.dispatchEvent(evt);
120 window = null;
121 },
122 true);
123 } 126 }
124 }; 127 }
125
126 var functionsToEval = {
127 addSidebar: function addSidebar(options) {
128 window.addEventListener(
129 "message",
130 function onMessage(event) {
131 // TODO: Make this secure by looking at the origin, etc.
132 window.console.log(event);
133 if (event.data == "sidebar created") {
134 window.removeEventListener("message", onMessage, false);
135 var panel = event.source;
136 options.callback(panel);
137 }
138 },
139 false
140 );
141
142 // Absolut-ify the URL if necessary.
143 var anchor = window.document.createElement("a");
144 anchor.setAttribute("href", options.url);
145 window.document.body.appendChild(anchor);
146 options.url = anchor.href;
147 window.document.body.removeChild(anchor);
148
149 window._addSidebar(options.url, options.width);
150 }
151 };
152
153 importFunctionsIntoWindow(functionsToImport, window);
154 evalFunctionsIntoWindow(functionsToEval, window);
155 } 128 }