-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.js
More file actions
218 lines (200 loc) · 6.25 KB
/
Copy pathoptions.js
File metadata and controls
218 lines (200 loc) · 6.25 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
/**
* YouTube Advanced Keyword Filter - Options Page Script
*
* @author tojicb-fushiguro
* @repository https://github.com/tojicb-fushiguro/YouTube-Filter
*/
const _api = (typeof browser !== 'undefined' && browser.storage)
? browser
: {
storage: {
sync: {
get: (defaults) => new Promise((resolve, reject) =>
chrome.storage.sync.get(defaults, (result) => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve(result);
})
),
set: (items) => new Promise((resolve, reject) =>
chrome.storage.sync.set(items, () => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve();
})
)
}
},
tabs: {
query: (queryInfo) => new Promise((resolve, reject) =>
chrome.tabs.query(queryInfo, (tabs) => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve(tabs);
})
),
sendMessage: (tabId, message) => new Promise((resolve, reject) =>
chrome.tabs.sendMessage(tabId, message, (response) => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve(response);
})
)
}
};
const DEFAULT_SETTINGS = {
keywords: "",
blocklist: "",
regex: false,
enabled: true,
wordBoundary: false,
softHide: false,
dateFilter: "any",
// Core
hideShorts: false,
hideHomepageFeed: false,
hideAllComments: false,
// Feeds
hideSponsoredCards: false,
hideSubscriptionCard: false,
hideSubscriptionButton: false,
hideMembersOnlyVideos: false,
hidePlaylistCards: false,
hideMixRadioPlaylists: false,
// Watch Page
hideVideoSidebar: false,
hideLiveChat: false,
hideWatchPlaylistPanel: false,
// Navigation
hideTopHeader: false,
hideNotificationBell: false,
hideExploreSection: false,
hideMoreFromYoutube: false,
hideYouSection: false,
disableAutoplay: false
};
const TOGGLE_IDS = [
'enabled',
'hideShorts', 'hideHomepageFeed', 'hideAllComments',
'hideSponsoredCards', 'hideSubscriptionCard', 'hideSubscriptionButton',
'hideMembersOnlyVideos', 'hidePlaylistCards', 'hideMixRadioPlaylists',
'hideVideoSidebar', 'hideLiveChat', 'hideWatchPlaylistPanel',
'hideTopHeader', 'hideNotificationBell',
'hideExploreSection', 'hideMoreFromYoutube', 'hideYouSection',
'disableAutoplay',
'regex', 'wordBoundary', 'softHide'
];
const INPUT_IDS = ['keywords', 'blocklist', 'dateFilter'];
function initNav() {
const links = document.querySelectorAll('.nav-link');
const sections = document.querySelectorAll('.settings-section');
links.forEach((link) => {
link.addEventListener('click', (e) => {
e.preventDefault();
const target = link.dataset.section;
links.forEach((l) => l.classList.remove('active'));
sections.forEach((s) => s.classList.remove('active'));
link.classList.add('active');
const targetSection = document.getElementById(`section-${target}`);
if (targetSection) targetSection.classList.add('active');
});
});
}
async function loadSettings() {
try {
const settings = await _api.storage.sync.get(DEFAULT_SETTINGS);
TOGGLE_IDS.forEach((id) => {
const el = document.getElementById(id);
if (el) el.checked = !!settings[id];
});
INPUT_IDS.forEach((id) => {
const el = document.getElementById(id);
if (!el) return;
if (el.tagName === 'SELECT') {
el.value = settings[id] || DEFAULT_SETTINGS[id];
} else {
el.value = settings[id] || '';
}
});
} catch (err) {
console.error('[YouTube Filter] Error loading settings:', err);
showStatus('Error loading settings', true);
}
}
function collectSettings() {
const settings = {};
TOGGLE_IDS.forEach((id) => {
const el = document.getElementById(id);
if (el) settings[id] = el.checked;
});
INPUT_IDS.forEach((id) => {
const el = document.getElementById(id);
if (el) settings[id] = el.value.trim ? el.value.trim() : el.value;
});
return settings;
}
function showStatus(message, isError = false) {
const el = document.getElementById('saveStatus');
el.textContent = message;
el.className = `save-status show${isError ? ' error' : ''}`;
setTimeout(() => el.classList.remove('show'), 2500);
}
async function notifyTabs() {
try {
const tabs = await _api.tabs.query({ url: 'https://www.youtube.com/*' });
tabs.forEach((tab) =>
_api.tabs.sendMessage(tab.id, { action: 'refilter' }).catch(() => {})
);
} catch (e) {}
}
async function saveSettings() {
try {
const settings = collectSettings();
await _api.storage.sync.set(settings);
showStatus('✓ Settings saved');
await notifyTabs();
} catch (err) {
console.error('[YouTube Filter] Error saving settings:', err);
showStatus('Error saving settings', true);
}
}
async function resetSettings() {
if (!confirm('Reset all settings to default?')) return;
try {
await _api.storage.sync.set(DEFAULT_SETTINGS);
await loadSettings();
showStatus('✓ Reset to defaults');
await notifyTabs();
} catch (err) {
console.error('[YouTube Filter] Error resetting settings:', err);
showStatus('Error resetting', true);
}
}
function initAutoSave() {
TOGGLE_IDS.forEach((id) => {
const el = document.getElementById(id);
if (el) el.addEventListener('change', () => saveSettings());
});
let debounceTimer = null;
INPUT_IDS.forEach((id) => {
const el = document.getElementById(id);
if (el) {
el.addEventListener('input', () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => saveSettings(), 600);
});
el.addEventListener('change', () => saveSettings());
}
});
}
function initEnterKey() {
document.querySelectorAll('input[type="text"]').forEach((input) => {
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') saveSettings();
});
});
}
document.addEventListener('DOMContentLoaded', () => {
initNav();
loadSettings();
initAutoSave();
initEnterKey();
document.getElementById('saveBtn').addEventListener('click', saveSettings);
document.getElementById('resetBtn').addEventListener('click', resetSettings);
});