-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbackground.js
More file actions
42 lines (37 loc) · 1.26 KB
/
background.js
File metadata and controls
42 lines (37 loc) · 1.26 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
const defaultWebsites = [
'facebook.com',
'instagram.com',
'twitter.com',
'x.com'
];
chrome.runtime.onInstalled.addListener(async () => {
const { blockedWebsites } = await chrome.storage.sync.get('blockedWebsites');
if (!blockedWebsites) {
await chrome.storage.sync.set({ blockedWebsites: defaultWebsites });
}
});
function isWebsiteBlocked(url, blockedWebsites) {
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname;
return blockedWebsites.some(website => {
const cleanWebsite = website.replace(/^https?:\/\//, '');
console.log({ cleanWebsite, hostname, urlObj })
return hostname === cleanWebsite || hostname.endsWith('.' + cleanWebsite);
});
} catch (e) {
return false;
}
}
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
const { blockedWebsites = [] } = await chrome.storage.sync.get('blockedWebsites');
const shouldBlock = isWebsiteBlocked(tab.url, blockedWebsites);
if (shouldBlock) {
chrome.scripting.executeScript({
target: { tabId },
files: ['content.js']
});
}
}
});