Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
59 lines (52 loc) · 1.91 KB
/
Copy pathbackground.js
File metadata and controls
59 lines (52 loc) · 1.91 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
51
52
53
54
55
56
57
58
59
// function that injects code to a specific tab
function injectScript(tabId, url) {
console.log("Injecting script into tab: ", tabId, " with url ",url);
if (url.startsWith('https://www.youtube.com/watch') || url.startsWith('https://www.youtube.com/shorts/') || url.startsWith('https://www.youtube.com/embed/')) {
chrome.scripting.executeScript(
{
target: {tabId: tabId},
files: ['inject.js'],
},
() => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
}
);
}
}
chrome.storage.local.get('disabled', (disabled) => {
if (disabled.disabled === undefined) {
chrome.storage.local.set({'disabled': false})
}
})
chrome.storage.local.get('blacklist', (disabled) => {
if (disabled.blacklist === undefined) {
chrome.storage.local.set({'blacklist': []})
}
})
chrome.storage.local.get('blacklisttitles', (disabled) => {
if (disabled.blacklisttitle === undefined) {
chrome.storage.local.set({'blacklisttitles': []})
}
})
// adds a listener to tab change
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log("Tab updated: ", tabId, " ChangeInfo: ", changeInfo);
chrome.storage.local.get('disabled', (disabled) => {
if (disabled.disabled === true) {
return
}
// check for a URL in the changeInfo parameter (url is only added when it is changed)
if (changeInfo.url && changeInfo.status === 'loading') {
console.log("URL changed: ", changeInfo.url);
// calls the inject function
injectScript(tabId, changeInfo.url);
}
else if (changeInfo.status === 'loading') {
url = tab.url
console.log('Reloaded: ', url)
injectScript(tabId, url)
}
})
});