Mercurial > my-ubiquity-commands
annotate random-stuff/random-stuff.js @ 27:305345a0e472 default tip
Modifications to work w/ latest jetpack.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 18 May 2009 22:51:37 -0700 |
parents | ecd868a32247 |
children |
rev | line source |
---|---|
20 | 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 | |
21
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
78 function logError(element) { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
79 var window = element.ownerDocument.defaultView; |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
80 var console = window.wrappedJSObject.console; |
22
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
81 if (console) |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
82 console.error.apply(console, arguments); |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
83 else { |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
84 var items = ["<" + element.nodeName.toLowerCase() + ">"]; |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
85 for (var i = 1; i < arguments.length; i++) |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
86 items.push(arguments[i].toString()); |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
87 Components.utils.reportError(items.join(" ")); |
ecd868a32247
better error logging, streamlined example
Atul Varma <varmaa@toolness.com>
parents:
21
diff
changeset
|
88 } |
21
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
89 } |
20 | 90 |
21
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
91 function absolutifyUrl(document, url) { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
92 var anchor = document.createElement("a"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
93 anchor.setAttribute("href", url); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
94 document.body.appendChild(anchor); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
95 var absolute = anchor.href; |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
96 document.body.removeChild(anchor); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
97 return absolute; |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
98 } |
20 | 99 |
21
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
100 function pageLoad_inject_sidebar_functions(document) { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
101 if (document.body && |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
102 document.body.firstChild && |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
103 document.body.firstChild.nodeName == "FEATURE") { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
104 var window = document.defaultView; |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
105 var chromeWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
106 .getInterface(Ci.nsIWebNavigation) |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
107 .QueryInterface(Ci.nsIDocShellTreeItem) |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
108 .rootTreeItem |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
109 .QueryInterface(Ci.nsIInterfaceRequestor) |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
110 .getInterface(Ci.nsIDOMWindow); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
111 var widgets = document.getElementsByTagName("widget"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
112 for (var i = 0; i < widgets.length; i++) { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
113 var widget = widgets[i]; |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
114 var location = widget.getAttribute("location"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
115 if (location == "status-bar") { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
116 var href = widget.getAttribute("href"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
117 if (typeof(href) == "string") { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
118 href = absolutifyUrl(document, href); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
119 var width = widget.getAttribute("width"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
120 if (width && width.match(/^[0-9]+$/)) { |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
121 var panelIframe = addStatusBarPanel(chromeWindow, href, width); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
122 window.addEventListener( |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
123 "unload", |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
124 function() { panelIframe.parentNode.removeChild(panelIframe); }, |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
125 true |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
126 ); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
127 } else |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
128 logError(widget, "must specify width as integer"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
129 } else |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
130 logError(widget, "must specify href"); |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
131 } else |
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
132 logError(widget, "has unknown location", location); |
20 | 133 } |
21
e82bff1ef296
Changed code to be more html-like than JS-ish.
Atul Varma <varmaa@toolness.com>
parents:
20
diff
changeset
|
134 } |
20 | 135 } |