-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_start.js
More file actions
136 lines (119 loc) · 4.76 KB
/
Copy pathcontent_start.js
File metadata and controls
136 lines (119 loc) · 4.76 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// content_start.js
(() => {
if (window.__ni_content_loaded) return;
window.__ni_content_loaded = true;
(() => {
try {
document.documentElement.setAttribute('data-ni-prehide', '1');
if (document.getElementById('ni-prehide-boot')) return;
const st = document.createElement('style');
st.id = 'ni-prehide-boot';
st.textContent = `
html[data-ni-prehide="1"] article :is(h1,h2,h3,p,li,blockquote,figcaption):not([data-news-inverted]),
html[data-ni-prehide="1"] main article :is(h1,h2,h3,p,li,blockquote,figcaption):not([data-news-inverted]),
html[data-ni-prehide="1"] header h1:not([data-news-inverted]),
html[data-ni-prehide="1"] h1#main-heading:not([data-news-inverted]),
html[data-ni-prehide="1"] main [data-component="headline"] h1:not([data-news-inverted]),
html[data-ni-prehide="1"] main [data-testid="headline"] :is(h1,h2):not([data-news-inverted]),
html[data-ni-prehide="1"] main [data-component="subheadline"] h2:not([data-news-inverted]),
html[data-ni-prehide="1"] main [data-testid="subheadline"]:not([data-news-inverted]),
html[data-ni-prehide="1"] main [data-testid="article-body"] p:not([data-news-inverted]),
html[data-ni-prehide="1"] main [data-component="text-block"] p:not([data-news-inverted]) {
color: transparent !important;
-webkit-text-fill-color: transparent !important;
text-shadow: none !important;
}
`;
(document.head || document.documentElement).prepend(st);
// bfcache: set the flag again
window.addEventListener('pageshow', (e) => {
if (e.persisted) {
document.documentElement.setAttribute('data-ni-prehide', '1');
}
}, { once: true });
} catch {}
})();
const NI = (...a) => console.log('[NI][content]', ...a);
let alive = true;
const markDead = () => { alive = false; NI('context -> DEAD'); };
window.addEventListener('pagehide', markDead, { once: true });
window.addEventListener('freeze', markDead, { once: true });
window.addEventListener('beforeunload', markDead, { once: true });
const domain = location.hostname.split('.').slice(-2).join('.');
const key = 'enabled:' + domain;
function safeSendMessage(req, cb) {
if (!alive) return cb({ ok: false, error: 'context_gone' });
if (!chrome?.runtime?.id) return cb({ ok: false, error: 'extension_context_invalidated' });
try {
chrome.runtime.sendMessage(req, (resp) => {
if (chrome.runtime.lastError) {
NI('sendMessage lastError:', chrome.runtime.lastError.message);
cb({ ok: false, error: chrome.runtime.lastError.message });
} else {
cb(resp);
}
});
} catch (e) {
NI('sendMessage exception:', e);
cb({ ok: false, error: String(e) });
}
}
function tryInjectTag(srcUrl) {
return new Promise((resolve) => {
try {
const s = document.createElement('script');
s.src = srcUrl;
s.onload = () => { s.remove(); resolve(true); };
s.onerror = () => resolve(false);
(document.head || document.documentElement).appendChild(s);
} catch (e) {
NI('injectTag error:', e);
resolve(false);
}
});
}
function injectViaSW() {
return new Promise((resolve) => {
safeSendMessage({ action: 'inject_page_script' }, (resp) => {
NI('injectViaSW resp:', resp);
resolve(!!resp?.ok);
});
});
}
async function ensureInjected() {
// const enabled = await new Promise(res => chrome.storage?.local?.get([key], d => res(!!(d && d[key]))));
const enabled = true;
if (!enabled) { NI('disabled on domain', domain); return false; }
NI('inject start');
const okTag = await tryInjectTag(chrome.runtime.getURL('page_inject.js'));
if (okTag) {
NI('inject by <script> OK');
return true;
}
NI('inject by <script> FAIL, trying SW…');
const okSW = await injectViaSW();
NI('inject via SW:', okSW);
return okSW;
}
window.addEventListener('message', (ev) => {
const d = ev.data;
if (!d || ev.source !== window) return;
if (d.type === 'NI_PING') {
window.postMessage({ type: 'NI_PONG', id: d.id, ok: true }, '*');
return;
}
if (d.type === 'NEWS_INVERT_REQUEST' && d.id && d.text) {
NI('REQ -> SW len:', d.text.length);
safeSendMessage({ action: 'invert', text: d.text }, (resp) => {
if (resp?.ok) {
NI('RESP <- SW ok');
window.postMessage({ type: 'NEWS_INVERT_RESPONSE', id: d.id, text: resp.text }, '*');
} else {
NI('RESP <- SW error:', resp?.error);
window.postMessage({ type: 'NEWS_INVERT_RESPONSE', id: d.id, error: resp?.error || 'error' }, '*');
}
});
}
}, { passive: true });
ensureInjected().then(ok => NI('ensureInjected:', ok));
})();