-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
44 lines (39 loc) · 1.32 KB
/
Copy pathbackground.js
File metadata and controls
44 lines (39 loc) · 1.32 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
42
43
44
let prevId;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// read changeInfo data and do something with it
// like send the new url to contentscripts.js
if (changeInfo.url) {
const url = new URL(changeInfo.url);
const urlParams = new URLSearchParams(url.search);
const videoId = urlParams.get("v");
if ((videoId && !prevId) || prevId !== videoId) {
chrome.tabs.sendMessage(tabId, {
action: "urlChange",
url: changeInfo.url,
prevId,
urlParams,
});
prevId = videoId;
}
}
});
// stores message from content for popup to use
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.type === "Action") {
// Store the message or handle it as needed
chrome.storage.local.set({ statusMessage: request.data });
}
});
// If tab is closed set to Idle
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
chrome.storage.local.set({ statusMessage: "Idle" });
});
// If user navigates to new url, change to Idle
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url) {
const url = new URL(changeInfo.url);
if (!(url.hostname === "www.youtube.com" && url.pathname === "/watch")) {
chrome.storage.local.set({ statusMessage: "Idle" });
}
}
});