-
Notifications
You must be signed in to change notification settings - Fork 27
/
userChromeDevtoolsSheetLoader.uc.js
80 lines (78 loc) · 2.98 KB
/
userChromeDevtoolsSheetLoader.uc.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
// ==UserScript==
// @name Browser Toolbox Stylesheet Loader
// @version 2.1.4
// @author aminomancer
// @homepageURL https://github.com/aminomancer
// @description Load userChrome and userContent stylesheets into Browser Toolbox windows.
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/userChromeDevtoolsSheetLoader.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/userChromeDevtoolsSheetLoader.uc.js
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// @backgroundmodule
// ==/UserScript==
let EXPORTED_SYMBOLS = [];
(function () {
class ToolboxProcessSheetLoader {
regex = /^chrome:(\/\/devtools\/.*.html.*)/i;
lastSubject = null;
constructor() {
Services.obs.addObserver(this, "domwindowopened");
}
traverseToMainProfile(win, str) {
let dir = Services.dirsvc.get(str, win.Ci.nsIFile);
if (!dir.exists()) {
let toAddChrome = false;
while (dir.target.includes("chrome_debugger_profile")) {
dir = dir.parent;
toAddChrome = true;
}
if (toAddChrome) dir.append("chrome");
}
return dir;
}
getChromePath(win) {
return Services.io
.getProtocolHandler("file")
.QueryInterface(win.Ci.nsIFileProtocolHandler)
.getURLSpecFromDir(this.traverseToMainProfile(win, "UChrm"));
}
isDevtools(win) {
return (
Services.dirsvc
.get("UChrm", Ci.nsIFile)
.target.includes("chrome_debugger_profile") &&
this.regex.test(win.location.href)
);
}
loadSheet(win, path, name, type) {
let sss = win.Cc["@mozilla.org/content/style-sheet-service;1"].getService(
win.Ci.nsIStyleSheetService
);
let uri = win.Services.io.newURI(path + name);
if (!sss.sheetRegistered(uri, sss[type])) {
sss.loadAndRegisterSheet(uri, sss[type]);
}
}
observe(sub) {
if (this.lastSubject === sub) return;
this.lastSubject = sub;
sub.addEventListener("DOMContentLoaded", this, true, { once: true });
}
handleEvent(e) {
switch (e.type) {
case "DOMContentLoaded":
this._onContentLoaded(e);
break;
}
}
_onContentLoaded(e) {
let document = e.originalTarget;
let win = document.defaultView;
this.lastSubject.removeEventListener("DomContentLoaded", this, true);
if (!this.isDevtools(win)) return;
const path = this.getChromePath(win);
this.loadSheet(win, path, "userChrome.css", "AUTHOR_SHEET");
this.loadSheet(win, path, "userContent.css", "USER_SHEET");
}
}
new ToolboxProcessSheetLoader();
})();