-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathuserChrome.jsm
231 lines (203 loc) · 8.46 KB
/
userChrome.jsm
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
let EXPORTED_SYMBOLS = [];
const { xPref } = ChromeUtils.import('chrome://userchromejs/content/xPref.jsm');
const { Management } = ChromeUtils.import('resource://gre/modules/Extension.jsm');
const { AppConstants } = ChromeUtils.import('resource://gre/modules/AppConstants.jsm');
let UC = {
webExts: new Map(),
sidebar: new Map()
};
let _uc = {
ALWAYSEXECUTE: 'rebuild_userChrome.uc.js',
BROWSERCHROME: AppConstants.MOZ_APP_NAME == 'thunderbird' ? 'chrome://messenger/content/messenger.xhtml' : 'chrome://browser/content/browser.xhtml',
BROWSERTYPE: AppConstants.MOZ_APP_NAME == 'thunderbird' ? 'mail:3pane' : 'navigator:browser',
BROWSERNAME: AppConstants.MOZ_APP_NAME.charAt(0).toUpperCase() + AppConstants.MOZ_APP_NAME.slice(1),
PREF_ENABLED: 'userChromeJS.enabled',
PREF_SCRIPTSDISABLED: 'userChromeJS.scriptsDisabled',
chromedir: Services.dirsvc.get('UChrm', Ci.nsIFile),
scriptsDir: '',
sss: Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService),
getScripts: function () {
this.scripts = {};
let files = this.chromedir.directoryEntries.QueryInterface(Ci.nsISimpleEnumerator);
while (files.hasMoreElements()) {
let file = files.getNext().QueryInterface(Ci.nsIFile);
if (/\.uc\.js$/i.test(file.leafName)) {
_uc.getScriptData(file);
}
}
},
getScriptData: function (aFile) {
let aContent = this.readFile(aFile);
let header = (aContent.match(/^\/\/ ==UserScript==\s*\n(?:.*\n)*?\/\/ ==\/UserScript==\s*\n/m) || [''])[0];
let match, rex = {
include: [],
exclude: []
};
let findNextRe = /^\/\/ @(include|exclude)\s+(.+)\s*$/gm;
while ((match = findNextRe.exec(header))) {
rex[match[1]].push(match[2].replace(/^main$/i, _uc.BROWSERCHROME).replace(/\*/g, '.*?'));
}
if (!rex.include.length) {
rex.include.push(_uc.BROWSERCHROME);
}
let exclude = rex.exclude.length ? '(?!' + rex.exclude.join('$|') + '$)' : '';
let def = ['', ''];
let author = (header.match(/\/\/ @author\s+(.+)\s*$/im) || def)[1];
let filename = aFile.leafName || '';
return this.scripts[filename] = {
filename: filename,
file: aFile,
url: Services.io.getProtocolHandler('file').QueryInterface(Ci.nsIFileProtocolHandler).getURLSpecFromDir(this.chromedir) + filename,
name: (header.match(/\/\/ @name\s+(.+)\s*$/im) || def)[1],
description: (header.match(/\/\/ @description\s+(.+)\s*$/im) || def)[1],
version: (header.match(/\/\/ @version\s+(.+)\s*$/im) || def)[1],
author: (header.match(/\/\/ @author\s+(.+)\s*$/im) || def)[1],
regex: new RegExp('^' + exclude + '(' + (rex.include.join('|') || '.*') + ')$', 'i'),
id: (header.match(/\/\/ @id\s+(.+)\s*$/im) || ['', filename.split('.uc.js')[0] + '@' + (author || 'userChromeJS')])[1],
homepageURL: (header.match(/\/\/ @homepageURL\s+(.+)\s*$/im) || def)[1],
downloadURL: (header.match(/\/\/ @downloadURL\s+(.+)\s*$/im) || def)[1],
updateURL: (header.match(/\/\/ @updateURL\s+(.+)\s*$/im) || def)[1],
optionsURL: (header.match(/\/\/ @optionsURL\s+(.+)\s*$/im) || def)[1],
startup: (header.match(/\/\/ @startup\s+(.+)\s*$/im) || def)[1],
shutdown: (header.match(/\/\/ @shutdown\s+(.+)\s*$/im) || def)[1],
onlyonce: /\/\/ @onlyonce\b/.test(header),
isRunning: false,
get isEnabled() {
return (xPref.get(_uc.PREF_SCRIPTSDISABLED) || '').split(',').indexOf(this.filename) == -1;
}
}
},
readFile: function (aFile, metaOnly = false) {
let stream = Cc['@mozilla.org/network/file-input-stream;1'].createInstance(Ci.nsIFileInputStream);
stream.init(aFile, 0x01, 0, 0);
let cvstream = Cc['@mozilla.org/intl/converter-input-stream;1'].createInstance(Ci.nsIConverterInputStream);
cvstream.init(stream, 'UTF-8', 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
let content = '',
data = {};
while (cvstream.readString(4096, data)) {
content += data.value;
if (metaOnly && content.indexOf('// ==/UserScript==') > 0) {
break;
}
}
cvstream.close();
return content.replace(/\r\n?/g, '\n');
},
everLoaded: [],
loadScript: function (script, win) {
if (!script.regex.test(win.location.href) || (script.filename != this.ALWAYSEXECUTE && !script.isEnabled)) {
return;
}
if (script.onlyonce && script.isRunning) {
if (script.startup) {
eval(script.startup);
}
return;
}
try {
Services.scriptloader.loadSubScript(script.url + '?' + script.file.lastModifiedTime,
script.onlyonce ? { window: win } : win);
script.isRunning = true;
if (script.startup) {
eval(script.startup);
}
if (!script.shutdown) {
this.everLoaded.push(script.id);
}
} catch (ex) {
Cu.reportError(ex);
}
},
windows: function (fun, onlyBrowsers = true) {
let windows = Services.wm.getEnumerator(onlyBrowsers ? this.BROWSERTYPE : null);
while (windows.hasMoreElements()) {
let win = windows.getNext();
if (!win._uc)
continue;
if (!onlyBrowsers) {
let frames = win.docShell.getAllDocShellsInSubtree(Ci.nsIDocShellTreeItem.typeAll, Ci.nsIDocShell.ENUMERATE_FORWARDS);
let res = frames.some(frame => {
let fWin = frame.domWindow;
let {document, location} = fWin;
if (fun(document, fWin, location))
return true;
});
if (res)
break;
} else {
let {document, location} = win;
if (fun(document, win, location))
break;
}
}
},
createElement: function (doc, tag, atts, XUL = true) {
let el = XUL ? doc.createXULElement(tag) : doc.createElement(tag);
for (let att in atts) {
el.setAttribute(att, atts[att]);
}
return el
}
};
if (xPref.get(_uc.PREF_ENABLED) === undefined) {
xPref.set(_uc.PREF_ENABLED, true, true);
}
if (xPref.get(_uc.PREF_SCRIPTSDISABLED) === undefined) {
xPref.set(_uc.PREF_SCRIPTSDISABLED, '', true);
}
let UserChrome_js = {
observe: function (aSubject) {
aSubject.addEventListener('DOMContentLoaded', this, {once: true});
},
handleEvent: function (aEvent) {
let document = aEvent.originalTarget;
let window = document.defaultView;
this.load(window);
},
load: function (window) {
let location = window.location;
if (!this.sharedWindowOpened && location.href == 'chrome://extensions/content/dummy.xhtml') {
this.sharedWindowOpened = true;
Management.on('extension-browser-inserted', function (topic, browser) {
browser.messageManager.addMessageListener('Extension:BackgroundViewLoaded', this.messageListener.bind(this));
}.bind(this));
} else if (/^(chrome:(?!\/\/global\/content\/commonDialog\.x?html)|about:(?!blank))/i.test(location.href)) {
window.UC = UC;
window._uc = _uc;
window.xPref = xPref;
if (window._gBrowser) // bug 1443849
window.gBrowser = window._gBrowser;
if (xPref.get(_uc.PREF_ENABLED)) {
Object.values(_uc.scripts).forEach(script => {
_uc.loadScript(script, window);
});
} else if (!UC.rebuild) {
_uc.loadScript(_uc.scripts[_uc.ALWAYSEXECUTE], window);
}
}
},
messageListener: function (msg) {
const browser = msg.target;
const { addonId } = browser._contentPrincipal;
browser.messageManager.removeMessageListener('Extension:BackgroundViewLoaded', this.messageListener);
if (browser.ownerGlobal.location.href == 'chrome://extensions/content/dummy.xhtml') {
UC.webExts.set(addonId, browser);
Services.obs.notifyObservers(null, 'UCJS:WebExtLoaded', addonId);
} else {
let win = browser.ownerGlobal.windowRoot.ownerGlobal;
UC.sidebar.get(addonId)?.set(win, browser) || UC.sidebar.set(addonId, new Map([[win, browser]]));
Services.obs.notifyObservers(win, 'UCJS:SidebarLoaded', addonId);
}
}
};
if (!Services.appinfo.inSafeMode) {
_uc.chromedir.append(_uc.scriptsDir);
_uc.getScripts();
let windows = Services.wm.getEnumerator(null);
while (windows.hasMoreElements()) {
let win = windows.getNext();
if (!('UC' in win))
UserChrome_js.load(win)
}
Services.obs.addObserver(UserChrome_js, 'chrome-document-global-created', false);
}