-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
33 lines (30 loc) · 1.06 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 redirectUrl = chrome.runtime.getURL("focus.html");
// Site navigation
chrome.tabs.onUpdated.addListener( (tabId, changeInfo, tab) => {
redirectTab(tab);
});
// Tab switch
chrome.tabs.onActivated.addListener( (activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
redirectTab(tab);
});
});
function redirectTab(tab) {
chrome.storage.local.get("focusEnabled").then( (result) => {
let focusEnabled = result.focusEnabled;
chrome.storage.sync.get("restrictedSites").then( (result) => {
if (result?.restrictedSites) {
let aRestrictedSites = JSON.parse(result.restrictedSites);
if (focusEnabled && tab.url)
{
aRestrictedSites.forEach(url => {
let regex = new RegExp(url, "g")
if (tab.url.search(regex) >= 0) {
chrome.tabs.update(tab.id, { url: redirectUrl });
}
});
}
}
});
});
}