-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
40 lines (34 loc) · 1.16 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
// A helper function to handle the initialization
const initContentScript = (tabId, url) => {
const videoId = new URLSearchParams(new URL(url).search).get("v");
if (videoId) {
chrome.tabs.sendMessage(tabId, { type: "NEW", videoId });
}
};
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab.url.includes("youtube.com/watch")) {
initContentScript(tabId, tab.url);
}
});
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
if (details.url.includes("youtube.com/watch")) {
initContentScript(details.tabId, details.url);
}
});
const showBadge = () => {
chrome.action.setBadgeText({ text: "\u2713" });
chrome.action.setBadgeBackgroundColor({ color: "#32bea6" });
setTimeout(() => {
chrome.action.setBadgeText({ text: "" });
}, 1500);
};
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "SHOW_BADGE") {
showBadge();
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type === 'ADD_BOOKMARK') {
addNewBookmarkEventHandler();
}
});