Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
77 lines (64 loc) · 1.91 KB
/
Copy pathcontent.js
File metadata and controls
77 lines (64 loc) · 1.91 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
(function () {
"use strict";
if (!window.__ntulearn.isEnabled("backButtonFix", false)) return;
// URL pattern helpers
const DOCUMENT_RE =
/\/ultra\/courses\/[^/]+\/outline\/.+/;
const OUTLINE_RE = /\/ultra\/courses\/[^/]+\/outline$/;
function isDocumentView(url) {
return DOCUMENT_RE.test(url);
}
function isOutlineView(url) {
return OUTLINE_RE.test(url);
}
// --- Strategy 1: pushState monkey-patch ---
const originalPushState = history.pushState.bind(history);
history.pushState = function (state, title, url) {
try {
if (url) {
const targetPath = new URL(url, location.origin).pathname;
if (isDocumentView(location.pathname) && isOutlineView(targetPath)) {
history.back();
return;
}
}
} catch (_) {
// Fall through to original pushState if URL parsing fails
}
return originalPushState(state, title, url);
};
// --- Strategy 2: MutationObserver on close button ---
const CLOSE_BUTTON_SELECTOR =
'button[data-bbui-close="true"], bb-close-panel button';
const fixedButtons = new WeakSet();
function handleCloseButton(button) {
if (fixedButtons.has(button)) return;
fixedButtons.add(button);
button.addEventListener(
"click",
function (e) {
if (isDocumentView(location.pathname)) {
e.stopImmediatePropagation();
e.preventDefault();
history.back();
}
},
true
);
}
function scanForCloseButtons() {
document
.querySelectorAll(CLOSE_BUTTON_SELECTOR)
.forEach(handleCloseButton);
}
const observer = new MutationObserver(scanForCloseButtons);
function startObserver() {
scanForCloseButtons();
observer.observe(document.body, { childList: true, subtree: true });
}
if (document.body) {
startObserver();
} else {
document.addEventListener("DOMContentLoaded", startObserver);
}
})();