-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
410 lines (354 loc) · 11.5 KB
/
Copy pathcontent.js
File metadata and controls
410 lines (354 loc) · 11.5 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
let isolationEnabled = false;
let scrubInterval = null;
// --- SIDEBAR COLLAPSE ---
const SIDEBAR_COLLAPSE_SVG_PATHS = [
"M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z",
"15.41 7.41 14 6 8 12 14 18 15.41 16.59 10.83 12",
];
const SIDEBAR_COLLAPSE_ARIA_HINTS = ["collapse side panel", "hide side panel"];
const SIDEBAR_COLLAPSE_ICON_CODEPOINTS = ["\uE5DE"];
const SIDEBAR_LEFTMOST_THRESHOLD_PX = 8;
const SIDEBAR_ENFORCE_INTERVAL_MS = 1000;
function isVisibleElement(el) {
return !!(el && el.offsetParent !== null);
}
function isSidebarCollapseIcon(svg) {
const html = svg && svg.innerHTML;
if (!html) return false;
return SIDEBAR_COLLAPSE_SVG_PATHS.some((sig) => html.includes(sig));
}
function isLeftMostButton(btn) {
try {
const rect = btn.getBoundingClientRect();
return rect.left <= SIDEBAR_LEFTMOST_THRESHOLD_PX;
} catch {
return false;
}
}
function isAnyVisibleButtonAtLeftMost(buttons) {
return buttons.some((btn) => isVisibleElement(btn) && isLeftMostButton(btn));
}
function clickFirstVisibleButton(buttons) {
for (const btn of buttons) {
if (!isVisibleElement(btn)) continue;
btn.click();
return true;
}
return false;
}
function findButtonsByIconCodepoint() {
const nodes = Array.from(document.querySelectorAll("button, span, i, div"));
return nodes
.filter((el) => {
const text = (el.textContent || "").trim();
if (!text) return false;
return SIDEBAR_COLLAPSE_ICON_CODEPOINTS.some((cp) => text.includes(cp));
})
.map((el) => (el.tagName === "BUTTON" ? el : el.closest("button")))
.filter(Boolean);
}
function nativelyCollapseSidebar() {
try {
const codepointButtons = findButtonsByIconCodepoint();
if (isAnyVisibleButtonAtLeftMost(codepointButtons)) return true;
if (clickFirstVisibleButton(codepointButtons)) return true;
const iconButtons = Array.from(document.querySelectorAll("svg"))
.filter(isSidebarCollapseIcon)
.map((svg) => svg.closest("button"))
.filter(Boolean);
if (isAnyVisibleButtonAtLeftMost(iconButtons)) return true;
if (clickFirstVisibleButton(iconButtons)) return true;
const ariaButtons = Array.from(document.querySelectorAll("button")).filter((btn) => {
const label = (btn.getAttribute("aria-label") || "").toLowerCase();
return SIDEBAR_COLLAPSE_ARIA_HINTS.some((hint) => label.includes(hint));
});
if (isAnyVisibleButtonAtLeftMost(ariaButtons)) return true;
if (clickFirstVisibleButton(ariaButtons)) return true;
} catch {}
return false;
}
let collapseAttempts = 0;
let collapseInterval = null;
let lastSidebarEnforceAt = 0;
function attemptSidebarCollapse() {
if (collapseInterval) clearInterval(collapseInterval);
collapseAttempts = 0;
collapseInterval = setInterval(() => {
collapseAttempts++;
if (nativelyCollapseSidebar() || collapseAttempts > 15) {
clearInterval(collapseInterval);
}
}, 200);
}
function enforceSidebarClosed() {
const now = Date.now();
if (now - lastSidebarEnforceAt < SIDEBAR_ENFORCE_INTERVAL_MS) return;
lastSidebarEnforceAt = now;
nativelyCollapseSidebar();
}
// --- SESSION STATE ---
const SESSION_FLAG = "__sv_iso_enabled__";
function setSessionFlag(on) {
try {
if (on) sessionStorage.setItem(SESSION_FLAG, "1");
else sessionStorage.removeItem(SESSION_FLAG);
} catch {}
}
// --- HOVER SUPPRESSION ---
let hoverSuppressInstalled = false;
let isDragging = false;
function _isOverCanvas(x, y) {
try {
const els = document.elementsFromPoint(x, y) || [];
return els.some((el) => el && el.tagName === "CANVAS");
} catch {
return true;
}
}
function _onPointerDown(e) {
if (!isolationEnabled) return;
isDragging = true;
}
function _onPointerUp(e) {
if (!isolationEnabled) return;
isDragging = false;
}
function _onPointerMove(e) {
if (!isolationEnabled) return;
if (isDragging) return;
if (!e || typeof e.clientX !== "number") return;
if (!_isOverCanvas(e.clientX, e.clientY)) return;
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
}
function installHoverSuppressor() {
if (hoverSuppressInstalled) return;
hoverSuppressInstalled = true;
const opts = { capture: true, passive: false };
window.addEventListener("pointerdown", _onPointerDown, opts);
window.addEventListener("pointerup", _onPointerUp, opts);
window.addEventListener("pointercancel", _onPointerUp, opts);
window.addEventListener("pointermove", _onPointerMove, opts);
window.addEventListener("mousemove", _onPointerMove, opts);
window.addEventListener("touchmove", _onPointerMove, opts);
}
function uninstallHoverSuppressor() {
if (!hoverSuppressInstalled) return;
hoverSuppressInstalled = false;
const opts = { capture: true };
window.removeEventListener("pointerdown", _onPointerDown, opts);
window.removeEventListener("pointerup", _onPointerUp, opts);
window.removeEventListener("pointercancel", _onPointerUp, opts);
window.removeEventListener("pointermove", _onPointerMove, opts);
window.removeEventListener("mousemove", _onPointerMove, opts);
window.removeEventListener("touchmove", _onPointerMove, opts);
isDragging = false;
}
// --- ICON PATCH ---
const TRANSPARENT_DATA_URI =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMB/6X+oQAAAABJRU5ErkJggg==";
let iconPatchInstalled = false;
let originalImageSrcDesc = null;
function shouldBlankIconUrl(url) {
if (!url || typeof url !== "string") return false;
return (
url.includes("maps.gstatic.com/mapfiles/") ||
url.includes("gstatic.com/mapfiles/") ||
url.includes("/mapfiles/")
);
}
function installIconPatch() {
if (iconPatchInstalled) return;
try {
originalImageSrcDesc = Object.getOwnPropertyDescriptor(HTMLImageElement.prototype, "src");
if (!originalImageSrcDesc || !originalImageSrcDesc.set) return;
Object.defineProperty(HTMLImageElement.prototype, "src", {
get: originalImageSrcDesc.get,
set: function (v) {
if (shouldBlankIconUrl(v)) {
return originalImageSrcDesc.set.call(this, TRANSPARENT_DATA_URI);
}
return originalImageSrcDesc.set.call(this, v);
}
});
iconPatchInstalled = true;
} catch {}
}
function uninstallIconPatch() {
if (!iconPatchInstalled) return;
try {
if (originalImageSrcDesc) {
Object.defineProperty(HTMLImageElement.prototype, "src", originalImageSrcDesc);
}
} catch {}
iconPatchInstalled = false;
originalImageSrcDesc = null;
}
// --- CSS ALLOW-LIST & LAYOUT FIX ---
const ISO_CLASS = "sv-iso-on";
const KEEP_CLASS = "sv-iso-keep";
const STYLE_ID = "__sv_iso_style__";
function ensureIsoStyle() {
let st = document.getElementById(STYLE_ID);
if (!st) {
st = document.createElement("style");
st.id = STYLE_ID;
document.documentElement.appendChild(st);
}
return st;
}
function setIsoCss(enabled) {
const st = ensureIsoStyle();
st.textContent = enabled
? `
html, body { background: #000 !important; }
html.${ISO_CLASS},
html.${ISO_CLASS} body {
margin: 0 !important;
width: 100vw !important;
height: 100vh !important;
overflow: hidden !important;
}
/* Hide everything in this frame */
html.${ISO_CLASS} * {
visibility: hidden !important;
opacity: 0 !important;
pointer-events: none !important;
}
/* Allow-list: only elements we mark as keep */
html.${ISO_CLASS} .${KEEP_CLASS} {
visibility: visible !important;
opacity: 1 !important;
pointer-events: auto !important;
}
/* THE FIX: Detach canvas from ghost layouts and stretch it */
html.${ISO_CLASS} canvas.${KEEP_CLASS} {
visibility: visible !important;
opacity: 1 !important;
pointer-events: auto !important;
background: #000 !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
width: 100vw !important;
height: 100vh !important;
z-index: 999999 !important;
margin: 0 !important;
padding: 0 !important;
object-fit: cover !important;
}
`
: "";
}
function clearKeepMarks() {
document.querySelectorAll(`.${KEEP_CLASS}`).forEach((el) => el.classList.remove(KEEP_CLASS));
}
function markKeepChain(canvas) {
clearKeepMarks();
let el = canvas;
while (el) {
el.classList.add(KEEP_CLASS);
if (el === document.body) break;
el = el.parentElement || (el.getRootNode() instanceof ShadowRoot ? el.getRootNode().host : null);
}
}
// --- CANVAS SELECTION ---
function isLargeCanvas(c) {
const r = c.getBoundingClientRect();
return r.width > 300 && r.height > 300;
}
function isWebGLCanvas(c) {
try { return !!(c.getContext("webgl2") || c.getContext("webgl")); }
catch { return false; }
}
function getBestCanvasFromCenterStack() {
const cx = Math.floor(window.innerWidth / 2);
const cy = Math.floor(window.innerHeight / 2);
let stack = [];
try { stack = document.elementsFromPoint(cx, cy) || []; }
catch { stack = []; }
const canvases = stack.filter((el) => el && el.tagName === "CANVAS" && isLargeCanvas(el));
if (!canvases.length) return null;
for (let i = canvases.length - 1; i >= 0; i--) {
if (isWebGLCanvas(canvases[i])) return canvases[i];
}
return canvases[canvases.length - 1];
}
function getBestCanvasByArea() {
const canvases = Array.from(document.querySelectorAll("canvas"))
.filter(isLargeCanvas)
.map((c) => ({ c, r: c.getBoundingClientRect(), webgl: isWebGLCanvas(c) }));
if (!canvases.length) return null;
const list = canvases.filter((o) => o.webgl).length ? canvases.filter((o) => o.webgl) : canvases;
list.sort((a, b) => b.r.width * b.r.height - a.r.width * a.r.height);
return list[0].c;
}
function getBestCanvas() {
return getBestCanvasFromCenterStack() || getBestCanvasByArea();
}
// --- SCRUB LOGIC ---
function performScrub() {
if (!isolationEnabled) return;
installIconPatch();
document.documentElement.classList.add(ISO_CLASS);
setIsoCss(true);
enforceSidebarClosed();
const canvas = getBestCanvas();
if (!canvas) return;
markKeepChain(canvas);
}
function startScrubbing() {
if (scrubInterval) clearInterval(scrubInterval);
lastSidebarEnforceAt = 0;
attemptSidebarCollapse();
performScrub();
installHoverSuppressor();
scrubInterval = setInterval(performScrub, 120);
}
function stopScrubbing() {
if (scrubInterval) clearInterval(scrubInterval);
if (collapseInterval) clearInterval(collapseInterval);
lastSidebarEnforceAt = 0;
isolationEnabled = false;
uninstallHoverSuppressor();
uninstallIconPatch();
clearKeepMarks();
document.documentElement.classList.remove(ISO_CLASS);
setIsoCss(false);
}
// --- MESSAGES ---
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "SET_ISOLATION") {
if (msg.enabled) {
isolationEnabled = true;
setSessionFlag(true);
startScrubbing();
sendResponse({ ok: true, enabled: true });
} else {
stopScrubbing();
setSessionFlag(false);
sendResponse({ ok: true, enabled: false });
}
return;
}
if (msg.type === "GET_ISOLATION_STATE") {
sendResponse({ ok: true, enabled: isolationEnabled });
return;
}
});
// --- AUTO-REAPPLY AFTER REFRESH ---
try {
if (sessionStorage.getItem(SESSION_FLAG) === "1") {
isolationEnabled = true;
startScrubbing();
}
} catch {}
try {
chrome.runtime.sendMessage({ type: "HELLO_ISO" }, (resp) => {
if (resp && resp.ok && resp.enabled) {
isolationEnabled = true;
startScrubbing();
}
});
} catch {}