-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcbDisableInitialization.js
More file actions
168 lines (152 loc) · 6.34 KB
/
cbDisableInitialization.js
File metadata and controls
168 lines (152 loc) · 6.34 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
// https://github.com/Infocatcher/Custom_Buttons/tree/master/CB_Disable_Initialization
// Custom Buttons: Disable Initialization button for Custom Buttons
// (code for "initialization" section)
// (c) Infocatcher 2012-2013, 2016
// version 0.1.1 - 2016-02-02
// Adds "Enable initialization" checkbox to custom button's context menu.
// Only for test purposes!
// "Disabled" mean only "if(true) return;" before initealization code.
// Note: button itself aren't needed, this is just way to execute code on window startup.
// So you can place it on hidden toolbar.
var toggleEnabledLabel = (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 "Enable initialization";
})();
const deleteId = "custombuttons-contextpopup-remove";
const toggleEnabledId = "custombuttons-contextpopup-toggleEnabled";
var toggleEnabled = document.getElementById(toggleEnabledId);
if(toggleEnabled)
toggleEnabled.parentNode.removeChild(toggleEnabled);
var deleteItem = document.getElementById(deleteId);
toggleEnabled = deleteItem.cloneNode(true);
toggleEnabled.id = toggleEnabledId;
toggleEnabled.setAttribute("cb_id", toggleEnabledId);
toggleEnabled.setAttribute("type", "checkbox");
toggleEnabled.setAttribute("label", toggleEnabledLabel);
toggleEnabled.setAttribute("oncommand", "toggleCustomButtonEnabled();");
//toggleEnabled.removeAttribute("observes"); // For Firefox 3.6 and older
deleteItem.parentNode.insertBefore(toggleEnabled, deleteItem);
Array.prototype.filter.call( // Process already cloned menu items
document.getElementsByAttribute("observes", deleteItem.getAttribute("observes")),
function(mi) {
return mi != deleteItem && (mi.id || "").substr(0, deleteId.length) == deleteId;
}
).forEach(function(deleteItem, i) {
var clone = toggleEnabled.cloneNode(true);
clone.id += "-cloned-" + i;
deleteItem.parentNode.insertBefore(clone, deleteItem);
});
// Process #custombuttons-contextpopup-sub
const deleteIdSub = deleteId + "-sub";
var deleteItemSub = document.getElementById(deleteIdSub);
if(deleteItemSub) {
var clone = toggleEnabled.cloneNode(true);
if(deleteItemSub.hasAttribute("observes"))
clone.setAttribute("observes", deleteItemSub.getAttribute("observes"));
else
clone.removeAttribute("observes");
clone.id += "-sub";
deleteItemSub.parentNode.insertBefore(clone, deleteItemSub);
}
addEventListener("popupshowing", function(e) {
var popup = e.target;
if(popup.localName != "menupopup" || (popup.id || "").substr(0, 14) != "custombuttons-")
return;
var toggleEnabled = popup.getElementsByAttribute("cb_id", toggleEnabledId)[0];
if(!toggleEnabled)
return;
var btn = getBtn();
var initCode = btn && btn.cbInitCode || "";
if(/^\s*(?:if\s*\(true\)\s*)?return(?:;|\s*\n)/.test(initCode))
toggleEnabled.removeAttribute("checked");
else
toggleEnabled.setAttribute("checked", "true");
if(/^\s*(?:\/\*Initialization Code\*\/\s*)?$/.test(initCode)) // Nothing to do :)
toggleEnabled.setAttribute("disabled", "true");
else
toggleEnabled.removeAttribute("disabled");
}, false);
window.toggleCustomButtonEnabled = function() { // Should be global to work in cloned menus
var btn = getBtn();
if(!btn)
return;
// Trick to prevent "unreachable code after return statement" warning
const disablePrefix = "if(true) return; // Disabled by Disable Initialization button\n\n";
var initCode = btn.cbInitCode;
//if(initCode.substr(0, disablePrefix.length) == disablePrefix)
// initCode = initCode.substr(disablePrefix.length);
if(/^\s*(?:if\s*\(true\)\s*)?return;?(?:\s*\/\/[^\n\r]*)?\n+/.test(initCode))
initCode = RegExp.rightContext;
else
initCode = disablePrefix + initCode;
//btn.destroy("update");
//btn.cbInitCode = initCode;
//btn.setAttribute("cb-init", initCode);
var link = custombuttons.makeButtonLink("edit", btn.id);
var cbService = custombuttons.cbService;
var param = cbService.getButtonParameters(link);
param = param.wrappedJSObject || param;
param.initCode = initCode;
cbService.installButton(param);
//btn.init();
};
function getBtn() {
var btn = custombuttons.popupNode;
if(!btn || (btn.id || "").substr(0, 20) != "custombuttons-button")
return null;
return btn;
}
var style = '\
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");\n\
toolbarbutton[id^="custombuttons-button"][cb-init^="return;"]:not([cb-edit-state]),\n\
toolbarbutton[id^="custombuttons-button"][cb-init^="if(true) return;"]:not([cb-edit-state]) {\n\
outline: 1px dotted !important;\n\
outline-offset: -1px !important;\n\
}';
var styleNode = document.insertBefore(
document.createProcessingInstruction(
"xml-stylesheet",
'href="' + "data:text/css,"
+ encodeURIComponent(style) + '" type="text/css"'
),
document.firstChild
);
function destructor(reason) {
if(reason == "update" || reason == "delete") {
styleNode.parentNode.removeChild(styleNode);
Array.prototype.slice.call(document.getElementsByAttribute("cb_id", toggleEnabledId)).forEach(function(btn) {
btn.parentNode.removeChild(btn);
});
delete window.toggleCustomButtonEnabled;
}
}
if(
typeof addDestructor == "function" // Custom Buttons 0.0.5.6pre4+
&& addDestructor != ("addDestructor" in window && window.addDestructor)
)
addDestructor(destructor, this);
else
this.onDestroy = destructor;