-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
43 lines (40 loc) · 1.26 KB
/
service-worker.js
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
const shortsURL = 'https://www.youtube.com/shorts/';
let extensionStatus = 'ON';
let statusColor = '#777';
function toggleExtensionStatus(tabId) {
extensionStatus = extensionStatus === 'ON' ? 'OFF' : 'ON';
statusColor = extensionStatus === 'ON' ? '#777' : '#FF0000';
chrome.action.setBadgeText({
tabId,
text: extensionStatus,
});
chrome.action.setBadgeBackgroundColor({
tabId,
color: statusColor,
});
}
// Listen for the extension icon (action) being clicked
chrome.action.onClicked.addListener(async (tab) => {
toggleExtensionStatus(tab.id);
});
// Listen for changes in navigation history state (page changes)
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
if (extensionStatus === 'ON' && details.url.startsWith(shortsURL)) {
// Inject a script to redirect the page to the main YouTube homepage
chrome.scripting.executeScript({
target: { tabId: details.tabId },
function: () => {
window.location.href = 'https://www.youtube.com/';
},
});
}
});
// Listen for the extension being installed or updated
chrome.runtime.onInstalled.addListener(() => {
chrome.action.setBadgeText({
text: extensionStatus,
});
chrome.action.setBadgeBackgroundColor({
color: statusColor,
});
});