-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patheditCustomButtonInTab.js
More file actions
279 lines (262 loc) · 11 KB
/
editCustomButtonInTab.js
File metadata and controls
279 lines (262 loc) · 11 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// http://infocatcher.ucoz.net/js/cb/editCustomButtonInTab.js
// https://github.com/Infocatcher/Custom_Buttons/tree/master/Edit_Custom_Button_in_Tab
// Edit Custom Button in Tab button for Custom Buttons
// (code for "initialization" section)
// (c) Infocatcher 2012-2014, 2017-2019
// version 0.1.9 - 2019-08-16
// Note:
// In Firefox 3.6 and older:
// - Force enables "Save size and position of editor windows separately for each custom button"
// option for editor in tab (because doesn't work without this)
// - tab with editor can't be closed sometimes using OK/Cancel buttons
var editInTabLabel = (function() {
var locale = (function() {
if("Services" in window && "locale" in Services) {
var locales = Services.locale.requestedLocales // Firefox 64+
|| Services.locale.getRequestedLocales && Services.locale.getRequestedLocales();
if(locales)
return locales[0];
}
var prefs = "Services" in window && Services.prefs
|| Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
function pref(name, type) {
return prefs.getPrefType(name) != prefs.PREF_INVALID ? prefs["get" + type + "Pref"](name) : undefined;
}
if(!pref("intl.locale.matchOS", "Bool")) { // Also see https://bugzilla.mozilla.org/show_bug.cgi?id=1414390
var locale = pref("general.useragent.locale", "Char");
if(locale && locale.substr(0, 9) != "chrome://")
return locale;
}
return Components.classes["@mozilla.org/chrome/chrome-registry;1"]
.getService(Components.interfaces.nsIXULChromeRegistry)
.getSelectedLocale("global");
})().match(/^[a-z]*/)[0];
if(locale == "ru")
return "Редактировать во вкладке…";
return "Edit button in tab…";
})();
const editorBaseUri = "chrome://custombuttons/content/editor.xul";
const cbIdTabAttr = "custombuttons-editInTab-id";
const editId = "custombuttons-contextpopup-edit";
const editInTabId = editId + "InTab";
var editInTab = document.getElementById(editInTabId);
if(editInTab)
editInTab.parentNode.removeChild(editInTab);
var editItem = document.getElementById(editId);
editInTab = editItem.cloneNode(true);
editInTab.id = editInTabId;
editInTab.setAttribute("cb_id", editInTabId);
editInTab.setAttribute("label", editInTabLabel);
editInTab.setAttribute("oncommand", "editCustomButtonInTab();");
if(!Object.create) // Firefox 3.6 and older
editInTab.removeAttribute("observes");
editInTab.setAttribute("image", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAitJREFUeNrEk0tIVVEUhr99zrnHc1+SkYQVFWo20G6SgkFRDorEILFJDoogkoLoXfOopEZCYIFlIITlRIUiC5UmFZVk1igwCAozfD9uat17z97tc5XK0pGDFiz289/rXz//FkopFhMGiwyr5GoHHgkpZUnCde9LqXwwHyuR3HVNIzZpGrubbLtjfHwYyzvywIYpHp0o386ajAAD3/R1MRfugZeFoGcgbm9pedo+7VVVCMvTwKt8tGwrfXGb5mfRJFj8VV0Ki7hpk9t5gW35lYx9GcSZGFOGSjJQvuXpYd4PTLPUYU6m+Q1SgzbBkEPey0tUHjlJfU8j0c8j+Aa/ahFn2x2cdknVgLBfpx5DyTQIBGx8+oHjras4vX8DtfWNFG0q4gmPyas9VqgZqEbNAEvPUnW1sCMI+QVBPXc02Ar5ONiURW55NsHus1TkjNDW2srHdYcormo/Z2kG+0p35BPTfaaHrVnBBAmt75QwKK3LYuPetdDbDxMTdHc2ELn4gfWj32l+MFQhdla1KVdKpCvxBPXAcWUSlQEaYgeIlK2GvmENHuHOmxVcW3mLoDuqn09gGjNiF8xnEPWw+DUFpTA0BS+uc/tdDodrnhf+445dV9p/LaQw+SH8nBfVRBMpKi0zm5LIEupq7lF541Vhy6lIV3XGTRw5qdWXM078bRSBK3zs6b3MWxEgplxSPnWS2e8kwXfPbO4SanIefy4cf7bWtdAl8d9/408BBgBVmNFVzOyEfgAAAABJRU5ErkJggg==");
editItem.parentNode.insertBefore(editInTab, editItem.nextSibling);
Array.prototype.filter.call( // Process already cloned menu items
document.getElementsByAttribute("observes", editItem.getAttribute("observes")),
function(mi) {
var id = mi.id || "";
return mi != editItem
&& id.substr(0, editId.length) == editId
&& id.substr(0, editInTabId.length) != editInTabId;
}
).forEach(function(editItem, i) {
var clone = editInTab.cloneNode(true);
clone.id += "-cloned-" + i;
editItem.parentNode.insertBefore(clone, editItem.nextSibling);
});
// Process #custombuttons-contextpopup-sub
const editIdSub = editId + "-sub";
var editItemSub = document.getElementById(editIdSub);
if(editItemSub) {
var clone = editInTab.cloneNode(true);
if(editItemSub.hasAttribute("observes"))
clone.setAttribute("observes", editItemSub.getAttribute("observes"));
else
clone.removeAttribute("observes");
clone.id += "-sub";
editItemSub.parentNode.insertBefore(clone, editItemSub.nextSibling);
}
window.editCustomButtonInTab = function(btn, newTab) { // Should be global to work in cloned menus
if(!btn)
btn = custombuttons.popupNode;
if(!btn)
return;
var btnId = btn.id;
var link = custombuttons.makeButtonLink("edit", btnId);
var cbService = "cbICustomButtonsService" in Components.interfaces
? Components.classes["@xsms.nm.ru/custombuttons/cbservice;1"]
.getService(Components.interfaces.cbICustomButtonsService)
: Components.classes["@xsms.nm.ru/custombuttons/cbservice;1"] // Custom Buttons 0.0.5.9+
.getService(Components.interfaces.nsISupports)
.wrappedJSObject;
var param = cbService.getButtonParameters(link);
var editorUriFull = editorBaseUri
+ "?window=" + cbService.getWindowId(document.documentURI)
+ "&id=" + btnId;
var editorUri = cbService.mode & 64 /*CB_MODE_SAVE_EDITOR_SIZE_SEPARATELY*/
|| !Object.create // Firefox 3.6 and older
? editorUriFull
: editorBaseUri;
// Search for already opened tab
var rawParam = unwrap(param);
var isSeaMonkey = "Services" in window && Services.appinfo.name == "SeaMonkey";
var ws = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getEnumerator(isSeaMonkey ? null : "navigator:browser");
while(ws.hasMoreElements()) {
let win = ws.getNext();
if(isSeaMonkey && win.location.href != "chrome://navigator/content/navigator.xul")
continue;
let gBrowser = win.gBrowser;
let tabs = gBrowser.tabs || gBrowser.tabContainer.childNodes;
for(let i = 0, l = tabs.length; i < l; ++i) {
let tab = tabs[i];
if(tab == newTab)
continue;
let browser = tab.linkedBrowser;
if(!browser)
continue;
let loc = browser.currentURI.spec;
if(loc.substr(0, editorBaseUriLength) != editorBaseUri)
continue;
let isSameEditor = loc == editorUriFull
|| tab.getAttribute(cbIdTabAttr) == btnId;
let win = browser.contentWindow; // Will be null for unloaded tab
if(!isSameEditor && win) {
let rawWin = unwrap(win);
let winParam = "arguments" in rawWin && rawWin.arguments.length
? unwrap(rawWin.arguments[0])
: rawWin.editor && rawWin.editor.param;
isSameEditor = winParam && winParam.buttonLink == link;
}
if(isSameEditor) {
gBrowser.selectedTab = tab;
win && win.focus();
newTab && setTimeout(function() {
gBrowser.removeTab(newTab);
}, 0);
return;
}
}
}
// Or open new tab
var tab = newTab;
if(!tab) {
tab = gBrowser.selectedTab = gBrowser.addTab(editorUri, {
triggeringPrincipal: "Services" in window // Firefox 63+
&& Services.scriptSecurityManager
&& Services.scriptSecurityManager.getSystemPrincipal()
});
initSessionStore();
tab.setAttribute(cbIdTabAttr, btn.id);
}
var browser = tab.linkedBrowser;
browser.addEventListener("DOMContentLoaded", function load(e) {
var doc = e.target;
if(doc.location != editorUri)
return;
browser.removeEventListener(e.type, load, false);
var win = doc.defaultView;
win.arguments = [param];
var iconLink = doc.createElementNS("http://www.w3.org/1999/xhtml", "link");
iconLink.rel = "shortcut icon";
//iconLink.href = "chrome://custombuttons-context/content/icons/default/custombuttonsEditor.ico";
iconLink.href = getStdImage(rawParam.image);
iconLink.style.display = "none";
doc.documentElement.insertBefore(iconLink, doc.documentElement.firstChild);
var alreadyAsked = false;
function checkUnsaved(e) {
if(alreadyAsked)
return;
var dlg = unwrap(doc).documentElement;
if(
"_fireButtonEvent" in dlg
? !dlg._fireButtonEvent("cancel")
: !dlg.cancelDialog()
)
e.preventDefault();
}
function onDialogCancel(e) {
alreadyAsked = true;
// win.setTimeout shouldn't fire while confirmation dialog from the same window are opened
win.setTimeout(function() {
alreadyAsked = false;
}, 100);
}
function destroy(e) {
win.removeEventListener("dialogcancel", onDialogCancel, false);
win.removeEventListener("beforeunload", checkUnsaved, false);
win.removeEventListener("unload", destroy, false);
}
win.addEventListener("dialogcancel", onDialogCancel, false);
win.addEventListener("beforeunload", checkUnsaved, false);
win.addEventListener("unload", destroy, false);
}, false);
};
function unwrap(o) {
return o.wrappedJSObject || o; // Firefox 3.6 and older
}
function getStdImage(iid) {
if(/^custombuttons-stdicon-(\d)$/.test(iid)) switch(+RegExp.$1) {
// chrome://custombuttons/skin/custombuttons.css
// toolbarbutton[cb-stdicon="custombuttons-stdicon-*"] { ... }
case 1: return "chrome://custombuttons/skin/button.png";
case 2: return "chrome://custombuttons/skin/stdicons/rbutton.png";
case 3: return "chrome://custombuttons/skin/stdicons/gbutton.png";
case 4: return "chrome://custombuttons/skin/stdicons/bbutton.png";
}
return iid || "chrome://custombuttons/skin/button.png";
}
function initSessionStore() {
initSessionStore = function() {};
var ss = "nsISessionStore" in Components.interfaces
? (
Components.classes["@mozilla.org/browser/sessionstore;1"]
|| Components.classes["@mozilla.org/suite/sessionstore;1"]
).getService(Components.interfaces.nsISessionStore)
: SessionStore; // Firefox 61+ https://bugzilla.mozilla.org/show_bug.cgi?id=1450559
ss.persistTabAttribute(cbIdTabAttr);
}
function checkTab(tab) {
var cbId = tab.getAttribute(cbIdTabAttr);
if(!cbId)
return;
initSessionStore();
let btn = document.getElementById(cbId);
if(btn)
editCustomButtonInTab(btn, tab);
}
const editorBaseUriLength = editorBaseUri.length;
// We can't use only SSTabRestoring: user can reload tab with editor
addEventListener("DOMContentLoaded", function(e) {
var doc = e.target;
if(doc.location.href.substr(0, editorBaseUriLength) != editorBaseUri)
return;
var tabs = gBrowser.tabs || gBrowser.tabContainer.childNodes;
for(var i = 0, l = tabs.length; i < l; ++i) {
let tab = tabs[i];
let browser = tab.linkedBrowser;
if(browser && browser.contentDocument == doc) {
checkTab(tab);
break;
}
}
}, true, document.getElementById("appcontent")); // Firefox 60+, gBrowser isn't a DOM node anymore
checkTab(gBrowser.selectedTab);
function destructor(reason) {
if(reason == "update" || reason == "delete") {
Array.prototype.slice.call(document.getElementsByAttribute("cb_id", editInTabId)).forEach(function(btn) {
btn.parentNode.removeChild(btn);
});
delete window.editCustomButtonInTab;
}
}
if(
typeof addDestructor == "function" // Custom Buttons 0.0.5.6pre4+
&& addDestructor != ("addDestructor" in window && window.addDestructor)
)
addDestructor(destructor, this);
else
this.onDestroy = destructor;