-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_script.js
More file actions
50 lines (45 loc) · 1.79 KB
/
background_script.js
File metadata and controls
50 lines (45 loc) · 1.79 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
45
46
47
48
49
50
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action == "activatePopup") {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.pageAction.show(tabs[0].id);
})
}
});
function sendPageMessage(details) {
if (!details)
return;
let url = details.url;
if (details.url === 'https://www.youtube.com/') {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "mainPage" });
});
}
else if (url.startsWith('https://www.youtube.com/watch')) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "videoPage" });
});
}
else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "others" });
});
}
}
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.local.set({ youtube_state: 1 });
});
chrome.webNavigation.onHistoryStateUpdated.addListener(function (details) {
sendPageMessage(details);
}, { url: [{ urlMatches: 'https://www.youtube.com/*' }] });
chrome.webNavigation.onDOMContentLoaded.addListener(function (details) {
sendPageMessage(details);
}, { url: [{ urlMatches: 'https://www.youtube.com/*' }] });
function storageChanger(items, areaName) {
const { youtube_state } = items;
if (youtube_state && youtube_state.newValue) {
chrome.tabs.query({ active: true, currentWindow: true, url: ['https://www.youtube.com/*'] }, function (tabs) {
sendPageMessage(tabs[0]);
})
}
}
chrome.storage.onChanged.addListener(storageChanger);