Mercurial > my-ubiquity-commands
comparison random-stuff/random-stuff.js @ 20:a07982ba7259
Added random stuff.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 06 May 2009 09:20:13 -0700 |
parents | |
children | e82bff1ef296 |
comparison
equal
deleted
inserted
replaced
19:900a2fabd11c | 20:a07982ba7259 |
---|---|
1 const BG_PROPS = ["backgroundImage", | |
2 "backgroundPosition", | |
3 "backgroundRepeat", | |
4 "backgroundColor", | |
5 "backgroundAttachment"]; | |
6 | |
7 function copyBackground(fromElement, toElement) { | |
8 var window = fromElement.ownerDocument.defaultView; | |
9 var style = window.getComputedStyle(fromElement, null); | |
10 BG_PROPS.forEach( | |
11 function(name) { | |
12 toElement.style[name] = style[name]; | |
13 }); | |
14 } | |
15 | |
16 function evalFunctionsIntoWindow(functions, window) { | |
17 var sandbox = Components.utils.Sandbox(window); | |
18 var codeLines = []; | |
19 | |
20 for (name in functions) | |
21 if (typeof(functions[name]) == "function") | |
22 codeLines.push("window." + name + " = " + | |
23 functions[name].toString() + ";"); | |
24 | |
25 sandbox.window = window.wrappedJSObject; | |
26 | |
27 Components.utils.evalInSandbox(codeLines.join('\n'), sandbox); | |
28 } | |
29 | |
30 function importFunctionsIntoWindow(functions, window) { | |
31 var sandbox = Components.utils.Sandbox(window); | |
32 var codeLines = []; | |
33 | |
34 for (name in functions) | |
35 if (typeof(functions[name]) == "function") { | |
36 codeLines.push("window." + name + " = " + name + ";"); | |
37 sandbox.importFunction(functions[name]); | |
38 } | |
39 | |
40 sandbox.window = window.wrappedJSObject; | |
41 Components.utils.evalInSandbox(codeLines.join('\n'), sandbox); | |
42 } | |
43 | |
44 function injectPanelWindowFunctions(iframe) { | |
45 var functions = { | |
46 close: function close() { | |
47 iframe.parentNode.removeChild(iframe); | |
48 } | |
49 }; | |
50 | |
51 importFunctionsIntoWindow(functions, iframe.contentWindow); | |
52 } | |
53 | |
54 function onPanelLoad(evt) { | |
55 if (evt.originalTarget.nodeName == "#document") { | |
56 var iframe = this; | |
57 | |
58 iframe.removeEventListener("DOMContentLoaded", onPanelLoad, true); | |
59 injectPanelWindowFunctions(iframe); | |
60 copyBackground(this.parentNode, iframe.contentDocument.body); | |
61 } | |
62 } | |
63 | |
64 function addStatusBarPanel(window, url, width) { | |
65 var document = window.document; | |
66 var statusBar = document.getElementById("status-bar"); | |
67 var iframe = document.createElement("iframe"); | |
68 iframe.setAttribute("type", "content"); | |
69 iframe.setAttribute("src", url); | |
70 iframe.setAttribute("width", width); | |
71 iframe.setAttribute("height", statusBar.boxObject.height); | |
72 iframe.style.overflow = "hidden"; | |
73 iframe.addEventListener("DOMContentLoaded", onPanelLoad, true); | |
74 statusBar.appendChild(iframe); | |
75 return iframe; | |
76 } | |
77 | |
78 function pageLoad_inject_sidebar_functions(document) { | |
79 var window = document.defaultView; | |
80 var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) | |
81 .getInterface(Ci.nsIWebNavigation) | |
82 .QueryInterface(Ci.nsIDocShellTreeItem) | |
83 .rootTreeItem | |
84 .QueryInterface(Ci.nsIInterfaceRequestor) | |
85 .getInterface(Ci.nsIDOMWindow); | |
86 | |
87 document = null; | |
88 | |
89 var functionsToImport = { | |
90 _addSidebar: function _addSidebar(url, width) { | |
91 if (typeof(url) != "string") | |
92 throw new Error("URL must be a string"); | |
93 | |
94 if (typeof(width) != "number") | |
95 throw new Error("width must be a number"); | |
96 | |
97 var panelIframe = addStatusBarPanel(chromeWindow, url, width); | |
98 chromeWindow = null; | |
99 | |
100 panelIframe.addEventListener( | |
101 "load", | |
102 function(loadEvent) { | |
103 var panelDocument = loadEvent.originalTarget; | |
104 var evt = window.document.createEvent("MessageEvent"); | |
105 var origin = (panelDocument.location.protocol + "//" + | |
106 panelDocument.location.host); | |
107 evt.initMessageEvent("message", | |
108 false, | |
109 true, | |
110 "sidebar created", | |
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 } | |
124 }; | |
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 } |