-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
32 lines (27 loc) · 1.08 KB
/
contentScript.js
File metadata and controls
32 lines (27 loc) · 1.08 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
function applyFocusIfEnabled() {
// Only apply on video pages
if (window.location.pathname === '/watch') {
chrome.storage.sync.get('focusEnabled', (data) => {
toggleFocusMode(!!data.focusEnabled);
});
}
}
function toggleFocusMode(enabled) {
const toggle = (selector, hide) => {
document.querySelectorAll(selector).forEach(el => {
el.style.display = hide ? 'none' : '';
});
};
toggle('#related, #comments, ytd-watch-next-secondary-results-renderer, #masthead-container, #secondary', enabled);
}
// Initial application
applyFocusIfEnabled();
// Ensure focus mode is applied after DOM is ready (for first navigation after browser start)
document.addEventListener('DOMContentLoaded', applyFocusIfEnabled);
// Listen for YouTube navigation events (SPA navigation)
window.addEventListener('yt-navigate-finish', applyFocusIfEnabled);
// Observe DOM changes to re-apply focus mode if YouTube re-renders parts of the page
const observer = new MutationObserver(() => {
applyFocusIfEnabled();
});
observer.observe(document.body, { childList: true, subtree: true });