-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
91 lines (81 loc) · 2.45 KB
/
Copy pathcontentScript.js
File metadata and controls
91 lines (81 loc) · 2.45 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
// Content script that hides YouTube video titles only when the extension is enabled.
// It reads the `enabled` flag from chrome.storage.local and listens for changes.
let enabled = false;
let originalDocumentTitle = document.title;
function hideTitles() {
// Hide the page title element if present
const titleElement = document.querySelector(
"h1.style-scope.ytd-watch-metadata"
);
if (titleElement) {
titleElement.style.display = "none";
}
// Replace the document title (browser tab)
try {
document.title = "YouTube Video - Title Hidden";
} catch (e) {
// ignore
}
}
function showTitles() {
const titleElement = document.querySelector(
"h1.style-scope.ytd-watch-metadata"
);
if (titleElement) {
// restore display (empty string lets CSS determine it)
titleElement.style.display = "";
}
// restore original document title if we have it
try {
if (originalDocumentTitle) document.title = originalDocumentTitle;
} catch (e) {
// ignore
}
}
// Observe DOM changes so we can hide titles on SPA navigations.
const observer = new MutationObserver(() => {
if (!enabled) return;
if (window.location.pathname.startsWith("/watch")) {
hideTitles();
}
});
observer.observe(document.body, { childList: true, subtree: true });
// Initialize enabled state from storage. If storage isn't available (e.g., running
// outside extension environment), default to disabled.
function readEnabledFromStorage() {
if (typeof chrome !== "undefined" && chrome.storage && chrome.storage.local) {
chrome.storage.local.get(["enabled"], (res) => {
enabled = !!res.enabled;
// keep a copy of the current document.title so we can restore it
originalDocumentTitle = originalDocumentTitle || document.title;
if (enabled && window.location.pathname.startsWith("/watch")) {
hideTitles();
} else if (!enabled) {
showTitles();
}
});
} else {
// No chrome.storage available: leave disabled
enabled = false;
}
}
// Listen for changes to the enabled flag so we can toggle in real time.
if (
typeof chrome !== "undefined" &&
chrome.storage &&
chrome.storage.onChanged
) {
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "local") return;
if (changes.enabled) {
enabled = !!changes.enabled.newValue;
if (enabled) {
hideTitles();
} else {
showTitles();
}
}
});
}
// Run initial read
readEnabledFromStorage();