-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
executable file
·58 lines (52 loc) · 1.43 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
let extension_enabled;
chrome.storage.local.get({ extension_enabled: 0 }, function (result) {
if (result.extension_enabled === 0) {
extension_enabled = false;
} else {
extension_enabled = true;
}
});
chrome.runtime.onMessage.addListener(function (message) {
if (message.activateExtension) {
extension_enabled = true;
getTabsInfo().then((tabs) => {
tabs.forEach(({ tabId, status, tab }) => {
newYoutubeVid(tabId, status, tab);
});
});
} else if (message.deactivateExtension) {
extension_enabled = false;
}
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
newYoutubeVid(tabId, changeInfo.status, tab);
});
function getTabsInfo() {
return new Promise((resolve) => {
chrome.tabs.query({}, function (tabs) {
const tabsInfo = tabs.map((tab) => {
return {
tabId: tab.id,
status: tab.status,
tab: tab
};
});
resolve(tabsInfo);
});
});
}
function newYoutubeVid(tabId, status, tab) {
if (
status === "complete" &&
tab.url &&
tab.url.includes("youtube.com/watch")
) {
const queryParameters = tab.url.split("?")[1];
const urlParameters = new URLSearchParams(queryParameters);
chrome.tabs.sendMessage(tabId, {
type: "NEW",
videoId: urlParameters.get("v"),
extension_value: extension_enabled
});
}
}