-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmergeInitialization.js
More file actions
60 lines (55 loc) · 1.33 KB
/
mergeInitialization.js
File metadata and controls
60 lines (55 loc) · 1.33 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
// https://github.com/Infocatcher/Custom_Buttons/blob/master/code_snippets/mergeInitialization.js
// Example for safely use only one button for initialization of many "buttons" without UI
var _destructors = [];
function destructor() {
var args = arguments;
_destructors.forEach(function(destructor) {
try {
destructor.apply(this, args);
}
catch(e) {
Components.utils.reportError(e);
}
}, this);
}
if("defineProperty" in Object) { // Firefox 4+
Object.defineProperty(this, "onDestroy", {
get: function() {
return _destructors.length ? destructor : undefined;
},
set: function(f) {
_destructors.push(f);
},
enumerable: true,
configurable: true
});
}
else {
this.__defineGetter__("onDestroy", function() {
return _destructors.length ? destructor : undefined;
});
this.__defineSetter__("onDestroy", function(f) {
_destructors.push(f);
});
}
// Usage example:
(function() {
// Some code #1
LOG("Initialize button #1");
function destructor(reason) {
LOG("onDestroy #1 " + reason);
}
this.onDestroy = destructor;
}).apply(this, arguments);
(function() {
// Some code #2
LOG("Initialize button #2");
function destructor(reason) {
LOG("onDestroy #2 " + reason);
}
this.onDestroy = destructor;
}).apply(this, arguments);
(function() {
// Code of any button without UI here
}).apply(this, arguments);
// ...