-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpageHandler.js
More file actions
93 lines (79 loc) · 3.13 KB
/
pageHandler.js
File metadata and controls
93 lines (79 loc) · 3.13 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
settingsLoadedCallbacks.push(initExtension);
function initExtension() {
const PAGES = Object.freeze({
"subscriptions": "/feed/subscriptions",
"video": "/watch",
"short": "/shorts",
"channel": "/videos",
"channelLive": "/streams",
"home": ""
});
let pageChangeTimeout = null;
let lastHandledPage = null;
// Debounced page change handler - delays processing to allow URL to update
// during SPA navigation before we check which page we're on (fixes #180)
function schedulePageChange() {
clearTimeout(pageChangeTimeout);
pageChangeTimeout = setTimeout(handlePageChange, 100);
}
async function handlePageChange() {
let page = getCurrentPage();
// Skip if page hasn't actually changed (avoids duplicate processing)
if (page === lastHandledPage) {
log("Page change event fired but URL unchanged, skipping: " + page);
return;
}
lastHandledPage = page;
log("Page was changed to " + page);
//unload old page
stopSubs();
try {
//handle new page
switch (page) {
case PAGES.subscriptions:
await initSubs();
break;
case PAGES.video:
onVideoPage();
break
case PAGES.home:
if (settings["settings.hide.watched.support.home"]) initSubs();
break;
default:
if (page.includes(PAGES.short)) {
onShortPage();
} else if ((page.includes(PAGES.channel) || page.includes(PAGES.channelLive)) && settings["settings.hide.watched.support.channel"]) {
await initSubs();
}
}
} catch (e) {
logError(e)
}
}
function initPageHandler() {
// Method 1: Try the old progress bar element (for old layout)
let pageLoader = document.querySelector("yt-page-navigation-progress");
if (pageLoader != null) {
// Old layout - use existing MutationObserver approach
log("Found page loader (old layout)");
let pageChangeObserver = new MutationObserver((mutations) => {
mutations.forEach((mutationRecord) => {
//is page fully loaded?
if (mutationRecord.target.getAttribute("aria-valuenow") === "100") {
schedulePageChange();
}
});
});
//observe when the page loader becomes visible or hidden
pageChangeObserver.observe(pageLoader, {attributes: true, attributeFilter: ['hidden']});
} else {
// Method 2: New layout - use yt-navigate-finish event
log("Using yt-navigate-finish event (new layout)");
document.addEventListener('yt-navigate-finish', schedulePageChange);
}
// Handle initial page load regardless of method
handlePageChange();
}
log("Initializing...");
initPageHandler();
}