-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchrome-mod.js
173 lines (148 loc) · 5.24 KB
/
chrome-mod.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { Trait } = require("api-utils/traits");
const observers = require("api-utils/observer-service");
const { Worker, Loader } = require('api-utils/content');
const { EventEmitter } = require('api-utils/events');
const { WindowTrackerTrait, windowIterator } = require('api-utils/window-utils');
const { Cc, Ci } = require("chrome");
const mediator = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
/**
* ChromeMod constructor
* @constructor
*/
exports.ChromeMod = Loader.compose(EventEmitter, {
on: EventEmitter.required,
_listeners: EventEmitter.required,
contentScript: Loader.required,
contentScriptFile: Loader.required,
contentScriptWhen: Loader.required,
include: null,
type: null,
id: null,
constructor: function ChromeMod(options) {
this._onNewWindow = this._onNewWindow.bind(this);
this._onUncaughtError = this._onUncaughtError.bind(this);
options = options || {};
if ('contentScript' in options)
this.contentScript = options.contentScript;
if ('contentScriptFile' in options)
this.contentScriptFile = options.contentScriptFile;
if ('contentScriptWhen' in options)
this.contentScriptWhen = options.contentScriptWhen;
if ('onAttach' in options)
this.on('attach', options.onAttach);
if ('onError' in options)
this.on('error', options.onError);
function normalize(strOrArray) {
if (Array.isArray(strOrArray))
return strOrArray;
else if (strOrArray)
return [strOrArray];
else
return [];
}
this.include = normalize(options.include);
this.type = normalize(options.type);
this.id = normalize(options.id);
chromeModManager.on("new-window", this._onNewWindow);
},
destroy: function destroy() {
chromeModManager.removeListener("new-window", this._onNewWindow);
},
_compareStringWithArrayOfStringRegexp : function(array, str) {
// Accept any on wild card
if (array.length==1 && array[0]=="*") return true;
// Attribute may be null, we should not accept them.
if (!str) return false;
for(let i=0, l=array.length; i<l; i++) {
let rule = array[i];
if (typeof rule=="RegExp" && rule.test(str))
return true;
else if (typeof rule=="string" && rule==str)
return true;
}
return false;
},
_onNewWindow: function _onNewWindow(window) {
// Match windows either on document location, window type or window id
if (!this._compareStringWithArrayOfStringRegexp(this.include,
window.document.location.href) &&
!this._compareStringWithArrayOfStringRegexp(this.type,
window.document.documentElement.getAttribute("windowtype")) &&
!this._compareStringWithArrayOfStringRegexp(this.id,
window.document.documentElement.getAttribute("id")) )
return;
this._emit('attach', Worker({
window: window.wrappedJSObject,
contentScript: this.contentScript,
contentScriptFile: this.contentScriptFile,
onError: this._onUncaughtError
}));
},
_onUncaughtError: function _onUncaughtError(e) {
if (this._listeners('error').length == 0)
console.exception(e);
//else
// this._emit("error", e);
}
});
const ChromeModManager = Trait.compose(
EventEmitter.resolve({
on: '_on'
}), {
constructor: function PageModRegistry() {
observers.add(
"chrome-document-global-created",
this._onDocumentGlobalCreated = this._onDocumentGlobalCreated.bind(this)
);
},
_destructor: function _destructor() {
observers.remove(
"chrome-document-global-created",
this._onDocumentGlobalCreated
);
// Empty EventEmitter
this._removeAllListeners();
},
_onDocumentGlobalCreated: function _onDocumentGlobalCreated(window) {
// Emit an event immediatly if document is already loaded,
// else wait for its to be ready
if (window.document && window.document.readyState == "complete") {
this._emit("new-window", window);
return;
}
let self = this;
window.addEventListener("DOMContentLoaded", function load() {
window.removeEventListener("DOMContentLoaded", load, true);
self._emit("new-window", window);
}, true);
},
on: function (name, callback) {
this._on(name, callback);
if (name != "new-window")
return;
// Emit all existing windows on listener registration
let winEnum = mediator.getXULWindowEnumerator(null);
while (winEnum.hasMoreElements()){
let win = winEnum.getNext();
if(!(win instanceof Ci.nsIXULWindow))
continue;
let docShellEnum = win.docShell.getDocShellEnumerator(
Ci.nsIDocShellTreeItem.typeAll,
Ci.nsIDocShell.ENUMERATE_FORWARDS);
while(docShellEnum.hasMoreElements()) {
let docShell = docShellEnum.getNext();
if(docShell instanceof Ci.nsIDocShell) {
let domDocument = docShell.contentViewer.DOMDocument;
let domWindow = domDocument.defaultView;
callback(domWindow);
}
}
}
}
});
const chromeModManager = ChromeModManager();