-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
32 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
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ adsSkipped: 0 });
chrome.action.setBadgeText({ text: '0' });
chrome.action.setBadgeBackgroundColor({ color: '#FF0000' });
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url && tab.url.includes('youtube.com/watch')) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
}, () => {
if (chrome.runtime.lastError) {
console.error(`Script injection failed: ${chrome.runtime.lastError.message}`);
}
});
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.adsSkipped !== undefined) {
chrome.storage.local.get('adsSkipped', (data) => {
let newCount = (data.adsSkipped || 0) + 1;
chrome.storage.local.set({ adsSkipped: newCount }, () => {
chrome.action.setBadgeText({ text: newCount.toString() });
if (sender.tab.id) {
chrome.action.setBadgeBackgroundColor({ color: '#FF0000', tabId: sender.tab.id });
}
});
});
}
});