-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
41 lines (37 loc) · 1.27 KB
/
Copy pathbackground.js
File metadata and controls
41 lines (37 loc) · 1.27 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
// Background service worker: toggles the 'enabled' state when the extension icon is clicked.
// Stores state in chrome.storage.local and updates the action badge/title.
// Helper to update badge and tooltip
function updateAction(newState) {
const text = newState ? "ON" : "";
try {
chrome.action.setBadgeText({ text });
chrome.action.setTitle({
title: `YouTube Title Hider: ${newState ? "ON" : "OFF"}`,
});
} catch (e) {
// ignore in environments where action APIs are unavailable
}
}
// Toggle when icon is clicked
chrome.action.onClicked.addListener(() => {
chrome.storage.local.get({ enabled: false }, (res) => {
const newState = !res.enabled;
chrome.storage.local.set({ enabled: newState }, () => {
updateAction(newState);
});
});
});
// Initialize default on install
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get({ enabled: false }, (res) => {
if (typeof res.enabled === "undefined") {
chrome.storage.local.set({ enabled: false }, () => updateAction(false));
} else {
updateAction(!!res.enabled);
}
});
});
// Set badge/title at service worker startup (some browsers reinitialize the worker)
chrome.storage.local.get({ enabled: false }, (res) => {
updateAction(!!res.enabled);
});