-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
114 lines (94 loc) · 2.84 KB
/
background.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
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
addContentScriptToTabs();
// dinamically add content scripts on install.
async function addContentScriptToTabs() {
const youtubeUrlPattern = '*://*.youtube.com/*';
const contentScripts = chrome.runtime.getManifest().content_scripts;
if (!contentScripts || !contentScripts.length) return;
const ytContentScript = contentScripts.find((cs) =>
cs.matches.includes(youtubeUrlPattern),
);
if (!ytContentScript) return;
const cssFiles = ytContentScript.css || [];
const jsFiles = ytContentScript.js || [];
try {
const tabs = await chrome.tabs.query({ url: youtubeUrlPattern });
for (const tab of tabs) {
// check if already has content script.
const isTabConnected = await chrome.tabs
.sendMessage(tab.id, 'connected')
.catch((err) => false);
if (isTabConnected) continue; // tab has content script, skip it.
if (cssFiles.length) {
// add css.
await chrome.scripting.insertCSS({
target: { tabId: tab.id },
files: cssFiles,
});
}
if (jsFiles.length) {
// add js.
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: jsFiles,
});
}
}
} catch (err) {
console.log('Could not inject script into youtube tabs', err);
}
}
// Send a message for a tab when it's url update.
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (!changeInfo.url) return;
chrome.tabs
.sendMessage(tabId, {
type: 'url update',
newUrl: changeInfo.url,
})
.catch((err) => {});
});
chrome.storage.sync.get({ enabled: true }).then((items) => {
updateAllTabIcons(items.enabled);
});
chrome.storage.sync.onChanged.addListener((changes) => {
if (!changes.enabled) return;
updateAllTabIcons(changes.enabled.newValue);
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (!changeInfo.url) return;
chrome.storage.sync.get({ enabled: true }).then((items) => {
updateTabIcon(tab, items.enabled);
});
});
function updateAllTabIcons(enabled) {
chrome.tabs
.query({})
.then((tabs) => {
for (const tab of tabs) {
updateTabIcon(tab, enabled);
}
})
.catch((err) => {
console.log('Could not query tabs', err);
});
}
/** @param {chrome.tabs.Tab} tab @param {boolean} enabled */
function updateTabIcon(tab, enabled = true) {
if (!tab || !tab.id) return;
const tabId = tab.id;
const setIcon = (iconName) => {
chrome.action
.setIcon({
path: {
16: `images/icons/${iconName}-16.png`,
48: `images/icons/${iconName}-48.png`,
128: `images/icons/${iconName}-128.png`,
},
tabId,
})
.catch((err) => {
console.log(`Could not add icon to the tab ${tabId}`, err);
});
};
setIcon(enabled ? 'normal/icon' : 'paused/icon_pause');
}