Repository navigation
Expand file tree
/
Copy pathuserChromeDevtoolsSheetLoader.uc.js
More file actions
101 lines (99 loc) · 3.79 KB
/
Copy pathuserChromeDevtoolsSheetLoader.uc.js
File metadata and controls
101 lines (99 loc) · 3.79 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
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
// ==UserScript==
// @name Browser Toolbox Stylesheet Loader
// @version 2.1
// @author aminomancer
// @homepage https://github.com/aminomancer
// @description Load userChrome and userContent stylesheets into Browser Toolbox windows
// @backgroundmodule
// ==/UserScript==
let EXPORTED_SYMBOLS = [];
(function () {
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
class ToolboxProcessSheetLoader {
constructor() {
Services.obs.addObserver(this, "domwindowopened", false);
this.regex = /^chrome:(\/\/devtools\/.*.html.*)/i;
this.lastSubject = null;
}
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"));
}
loadSheet(win, path, name, type) {
let sss = win.Cc["@mozilla.org/content/style-sheet-service;1"].getService(
win.Ci.nsIStyleSheetService
);
try {
sss.loadAndRegisterSheet(win.Services.io.newURI(path + name), sss[type]);
} catch (e) {}
}
unloadSheet(win, path, name, type) {
let sss = win.Cc["@mozilla.org/content/style-sheet-service;1"].getService(
win.Ci.nsIStyleSheetService
);
try {
sss.unegisterSheet(win.Services.io.newURI(path + name), sss[type]);
} catch (e) {}
}
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;
case "uninit":
this._onWindowUninit(e);
break;
}
}
_onContentLoaded(e) {
let document = e.originalTarget;
let win = document.defaultView;
this.lastSubject.removeEventListener("DomContentLoaded", this, true);
if (
!Services.dirsvc
.get("UChrm", Ci.nsIFile)
.target.includes("chrome_debugger_profile") ||
!this.regex.test(win.location.href)
)
return;
const path = this.getChromePath(win);
this.loadSheet(win, path, "userChrome.css", "AUTHOR_SHEET");
this.loadSheet(win, path, "userContent.css", "USER_SHEET");
win.addEventListener("uninit", this);
}
_onWindowUninit(e) {
let win = e.target;
if (
!Services.dirsvc
.get("UChrm", Ci.nsIFile)
.target.includes("chrome_debugger_profile") ||
!this.regex.test(win.location.href)
)
return;
const path = this.getChromePath(win);
this.unloadSheet(win, path, "userChrome.css", "AUTHOR_SHEET");
this.unloadSheet(win, path, "userContent.css", "USER_SHEET");
win.removeEventListener("uninit", this);
}
}
new ToolboxProcessSheetLoader();
})();