Repository navigation
Expand file tree
/
Copy pathimplementation.js
More file actions
115 lines (102 loc) · 3.68 KB
/
Copy pathimplementation.js
File metadata and controls
115 lines (102 loc) · 3.68 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
var { ExtensionCommon } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionCommon.sys.mjs"
);
const PREF = "mail.dark-reader.enabled";
const XHTML = "http://www.w3.org/1999/xhtml";
const NOTE_ID = "pslm-auto-note";
// Resolve a tab to its about:message window.
// - 3-pane mail tab: chromeBrowser -> about:3pane -> messageBrowser -> about:message
// - message tab: chromeBrowser -> about:message
// - message window: window.messageBrowser -> about:message
function getAboutMessage(nativeTab) {
let win =
nativeTab.chromeBrowser?.contentWindow ??
nativeTab.messageBrowser?.contentWindow;
if (win?.location.href.startsWith("about:3pane")) {
win = win.messageBrowser?.contentWindow;
}
return win?.location.href.startsWith("about:message") ? win : null;
}
this.darkReader = class extends ExtensionCommon.ExtensionAPI {
getAPI(context) {
const fires = new Set(); // active onUserToggled listeners
const watched = new Map(); // toggle element -> { handler, unload, win }
const notes = new Set(); // "Auto" labels we added
function aboutMessageFor(tabId) {
try {
return getAboutMessage(context.extension.tabManager.get(tabId).nativeTab);
} catch {
return null;
}
}
function unwatch(el) {
const entry = watched.get(el);
if (!entry) return;
try {
el.removeEventListener("change", entry.handler);
entry.win.removeEventListener("unload", entry.unload);
} catch {}
watched.delete(el);
}
context.callOnClose({
close() {
for (const el of [...watched.keys()]) unwatch(el);
for (const note of notes) {
try { note.remove(); } catch {}
}
notes.clear();
},
});
return {
darkReader: {
async setEnabled(enabled) {
if (Services.prefs.getBoolPref(PREF, true) !== enabled) {
Services.prefs.setBoolPref(PREF, enabled);
}
},
async watchTab(tabId) {
const win = aboutMessageFor(tabId);
const el = win?.document.getElementById("disableDarkReader");
if (!el || watched.has(el)) return;
const handler = e => {
if (!e.isTrusted) return; // ignore anything not from a real click
const darkEnabled = !e.target.checked; // checked = dark mode OFF
for (const fire of fires) fire.async(tabId, darkEnabled);
};
const unload = () => unwatch(el);
el.addEventListener("change", handler);
win.addEventListener("unload", unload, { once: true });
watched.set(el, { handler, unload, win });
},
async setNote(tabId, visible) {
const doc = aboutMessageFor(tabId)?.document;
if (!doc) return;
let note = doc.getElementById(NOTE_ID);
if (!note) {
if (!visible) return;
const toggle = doc.getElementById("darkReaderToggle");
if (!toggle) return;
note = doc.createElementNS(XHTML, "span");
note.id = NOTE_ID;
note.textContent = "Auto";
note.title = "Light mode remembered for this sender by Per-Sender Light Mode";
note.style.cssText =
"font-size: 0.85em; opacity: 0.7; margin-inline-end: 6px; align-self: center;";
toggle.before(note);
notes.add(note);
}
note.hidden = !visible;
},
onUserToggled: new ExtensionCommon.EventManager({
context,
name: "darkReader.onUserToggled",
register(fire) {
fires.add(fire);
return () => fires.delete(fire);
},
}).api(),
},
};
}
};