-
Notifications
You must be signed in to change notification settings - Fork 9
/
bootstrap.js
executable file
·227 lines (208 loc) · 7.81 KB
/
bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
var gAustralis = false;
try {
Cu.import("resource:///modules/CustomizableUI.jsm");
gAustralis = true;
} catch (e) {}
const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
XPCOMUtils.defineLazyGetter(this, "sss", function () {
return Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
});
var prefsPrefix = "extensions.cck2wizard.";
var idPrefix = "cck2wizard-";
function showInstallPanel(doc, callback) {
var panel = doc.getElementById(idPrefix + "install-panel");
if (panel) {
callback(panel);
return;
}
var panel = doc.createElement("panel");
panel.setAttribute("id", idPrefix + "login-panel");
panel.setAttribute("width", "300");
panel.setAttribute("orient", "vertical");
panel.setAttribute("type", "arrow");
var popupset = doc.getElementById("mainPopupSet");
popupset.appendChild(panel);
var request = new XMLHttpRequest();
request.mozBackgroundRequest = true;
request.open("GET", "chrome://cck2wizard/content/install-panel.xul");
request.onload = function() {
var installDialog = doc.importNode(request.responseXML.documentElement, true);
panel.appendChild(installDialog);
callback(panel);
};
request.send();
}
function install(aData, aReason) {
if (aReason != 5) {
// return;
}
Services.prefs.setCharPref(prefsPrefix + "toolbarID", "nav-bar");
}
function uninstall(aData, aReason) {
Services.prefs.clearUserPref(prefsPrefix + "toolbarID");
Services.prefs.clearUserPref(prefsPrefix + "nextSiblingID");
// CCK2 configs are NOT removed
}
function startup(aData, aReason) {
sss.loadAndRegisterSheet(Services.io.newURI("chrome://cck2wizard/skin/toolbar.css", null, null), sss.USER_SHEET);
var win = Services.wm.getMostRecentWindow("navigator:browser");
var url;
switch (aReason) {
case 5: // ADDON_INSTALL
showInstallPanel(win.document, function(panel) {
panel.addEventListener("click", function(event) {
win.document.getElementById(idPrefix + "button").click();
win.document.getElementById(idPrefix + "install-panel").hidePopup();
}, false);
panel.openPopup(win.document.getElementById(idPrefix + "button"), "", 0, 0, false, false, null);
});
url = "http://mike.kaply.com/addons/cck2/install/";
break;
case 7: // ADDON_UPGRADE
url = "http://mike.kaply.com/addons/cck2/upgrade/";
break;
}
if (url) {
win.openUILinkIn(url, "tab");
}
let enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let win = enumerator.getNext().QueryInterface(Ci.nsIDOMWindow);
if (win.document.readyState === "complete")
loadIntoWindow(win);
else
win.addEventListener("load", function() {
win.removeEventListener("load", arguments.callee, false);
loadIntoWindow(win);
})
}
Services.wm.addListener(windowListener);
}
function shutdown(data, reason) {
sss.unregisterSheet(Services.io.newURI("chrome://cck2wizard/skin/toolbar.css", null, null), sss.USER_SHEET);
Services.wm.removeListener(windowListener);
let enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let win = enumerator.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(win);
}
}
/* Save the current toolbarbutton position in preferences
*/
function saveButtonPosition(event) {
var button = event.target.ownerDocument.getElementById(idPrefix + "button");
var buttonPlacement = null;
if (gAustralis) {
buttonPlacement = CustomizableUI.getPlacementOfWidget(idPrefix + "button");
}
if (!button || (gAustralis && !buttonPlacement)) {
Services.prefs.clearUserPref(prefsPrefix + "toolbarID");
Services.prefs.clearUserPref(prefsPrefix + "nextSiblingID");
return;
}
if (buttonPlacement) {
Services.prefs.setCharPref(prefsPrefix + "toolbarID", buttonPlacement.area);
} else {
Services.prefs.setCharPref(prefsPrefix + "toolbarID", button.parentNode.id);
}
if (button.nextSibling) {
Services.prefs.setCharPref(prefsPrefix + "nextSiblingID", button.nextSibling.id);
}
}
function onCommand(event) {
if (event.originalTarget.id != idPrefix + "button")
return;
var win = Services.ww.getWindowByName("cck2wizard", null);
if (win) {
win.focus();
return;
}
Services.ww.openWindow(null, "chrome://cck2wizard/content/cck2wizard.xul", "cck2wizard", "resizable=yes,centerscreen", null);
}
function loadIntoWindow(window) {
if (!window || window.document.documentElement.getAttribute("windowtype") != "navigator:browser")
return;
let doc = window.document;
let toolbox = doc.getElementById("navigator-toolbox");
if (toolbox) {
let button = doc.createElement("toolbarbutton");
button.setAttribute("id", idPrefix + "button");
button.setAttribute("label", "CCK2 Wizard");
button.setAttribute("class", "toolbarbutton-1");
toolbox.palette.appendChild(button);
if (Services.prefs.prefHasUserValue(prefsPrefix + "toolbarID")) {
var toolbarID = Services.prefs.getCharPref(prefsPrefix + "toolbarID");
var nextSibling = null;
try {
nextSibling = doc.getElementById(Services.prefs.getCharPref(prefsPrefix + "nextSiblingID"));
} catch (ex) {}
if (gAustralis) {
var buttonPlacement = null;
if (nextSibling) {
buttonPlacement = CustomizableUI.getPlacementOfWidget(nextSibling.id);
if (buttonPlacement.area != toolbarID) {
buttonPlacement = null;
}
}
if (!nextSibling || !buttonPlacement) {
CustomizableUI.addWidgetToArea(idPrefix + "button", toolbarID);
} else {
CustomizableUI.addWidgetToArea(idPrefix + "button", toolbarID, buttonPlacement.position);
}
} else {
var toolbar = doc.getElementById(toolbarID);
var nextSibling = null;
try {
nextSibling = doc.getElementById(Services.prefs.getCharPref(prefsPrefix + "nextSiblingID"));
} catch (ex) {}
if (!toolbar) {
toolbar = doc.getElementById("nav-bar");
}
toolbar.insertItem(idPrefix + "button", nextSibling);
}
}
window.addEventListener("aftercustomization", saveButtonPosition, false);
window.addEventListener("command", onCommand, false);
}
}
function unloadFromWindow(window) {
var doc = window.document;
if (!window || doc.documentElement.getAttribute("windowtype") != "navigator:browser")
return;
let button = doc.getElementById(idPrefix + "button");
if (button) {
button.parentNode.removeChild(button);
} else {
var toolbox = doc.getElementById("navigator-toolbox");
if (toolbox && toolbox.palette) {
var element = toolbox.palette.querySelector("#" + buttonID);
if (element) {
element.parentNode.removeChild(element);
}
}
}
let panel = doc.getElementById(idPrefix + "install-panel");
if (panel) {
panel.parentNode.removeChild(panel);
}
window.removeEventListener("aftercustomization", saveButtonPosition, false);
window.removeEventListener("command", onCommand, false);
}
var windowListener = {
onOpenWindow: function(window) {
let domWindow = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener("load", function() {
domWindow.removeEventListener("load", arguments.callee, false);
loadIntoWindow(domWindow);
}, false);
},
onCloseWindow: function(window) {
let domWindow = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
},
onWindowTitleChange: function(window) {}
};