-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
33 lines (30 loc) · 1.11 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
const baseYtVidURL = "https://www.youtube.com/watch?v";
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// when a YouTube video webpage has loaded
if (
tab.active &&
tab.url.startsWith(baseYtVidURL) &&
changeInfo.title != null
) {
chrome.scripting.executeScript({
target: { tabId: tabId },
function: incrementYtChannel,
});
}
});
// increment the storage value for the YouTube channel on the current page
function incrementYtChannel() {
// retrieve channel name
const channelNameContainer = document.querySelector(
"ytd-channel-name#channel-name.ytd-video-owner-renderer"
);
if (channelNameContainer != null) {
const channelName = channelNameContainer.innerText;
// get number of times channel has been watched
// or 0 if key does not exist ie it has never been watched
chrome.storage.sync.get({ [channelName]: 0 }, function (result) {
// increment that number
chrome.storage.sync.set({ [channelName]: result[channelName] + 1 });
});
}
}