-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcustomizableUI.js
More file actions
74 lines (70 loc) · 2.6 KB
/
customizableUI.js
File metadata and controls
74 lines (70 loc) · 2.6 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
// https://github.com/Infocatcher/Custom_Buttons/blob/master/code_snippets/CustomizableUI.js
// Dummy wrapper to create Custom Buttons using CustomizableUI.jsm
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/CustomizableUI.jsm
//== Configuration begin
CustomButton({
id: "someCustomButton", // Unique identifier
fileInit: "someCustomButton.js", // Relative path to script file or empty string ("")
fileCode: "", // Relative path to script file or empty string ("")
label: "Button name",
tooltip: "Button tooltip",
icon: "chrome://branding/content/icon16.png",
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/CustomizableUI.jsm#Area_constants
defaultArea: CustomizableUI.AREA_NAVBAR // Default toolbar
});
//== Configuration end
function CustomButton(cb) {
var cbEnv = {
xulns: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
xhtmlns: "http://www.w3.org/1999/xhtml",
LOG: function(msg) {
var head = "[Custom Buttons CustomizableUI.jsm: id: " + this._id + "@" + this._phase
+ ", line: " + Components.stack.caller.lineNumber + "]";
Services.console.logStringMessage(head + "\n" + msg);
}
};
var path = new Error().fileName.replace(/[^\\\/]*$/, "");
function cbExec(cb, win, btn, codeEvent) {
// Note: be careful with global variables inside cb-script
var context = Object.assign(btn, cbEnv, {
_id: btn.id,
_phase: codeEvent ? "code" : "init",
event: codeEvent || new win.Object(),
self: btn
});
context.LOG = context.LOG.bind(context);
Object.defineProperty(context, "top", { value: win.top });
var file = path + (codeEvent ? cb.fileCode : cb.fileInit);
context.LOG("cbExec()\n" + file);
Services.scriptloader.loadSubScript(file, context, "UTF-8");
}
(CustomButton = function(cb) {
CustomizableUI.createWidget({
id: "__cb_" + cb.id,
type: "custom",
defaultArea: cb.defaultArea,
onBuild: function(doc) {
var btn = doc.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "toolbarbutton");
var id = cbEnv._id = this.id;
var attrs = {
id: id,
class: "toolbarbutton-1 chromeclass-toolbar-additional",
label: cb.label,
tooltiptext: cb.tooltip,
style: 'list-style-image: url("' + cb.icon + '");',
__proto__: null
};
for(var p in attrs)
btn.setAttribute(p, attrs[p]);
var win = doc.defaultView;
cb.fileCode && btn.addEventListener("command", function(e) {
cbExec(cb, win, btn, e);
}, false);
cb.fileInit && win.setTimeout(function() {
cbExec(cb, win, btn, false);
}, 0);
return btn;
}
});
})(cb);
}