Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
101 lines (92 loc) · 3.08 KB
/
Copy pathbackground.js
File metadata and controls
101 lines (92 loc) · 3.08 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
let extensionsToDisable = [];
let selectedSites = [];
// Get list of extensions to disable
const getExtensionsToDisable = () => {
console.log('Getting the list of extensions to disable');
// Get all the items in Chrome storage
chrome.storage.sync.get((items) => {
// Keep the list of extensions we don't want
extensionsToDisable = items.excludedExtensions;
console.log('Selected Extensions: ', extensionsToDisable);
});
}
// Get list of sites to disable extensions for
const getSitesToDisableExtensionsFor = () => {
console.log('Getting the list of sites to disable on');
chrome.storage.sync.get((items) => {
selectedSites = items.selectedWebsites;
});
}
// Triggered whenever a tab is updated
const processTabUpdated = async (changeInfo, tab) => {
console.log('A tab update has occurred, processing...');
if (tab.url) {
actOnExtensions(tab);
}
}
// Triggered whenever tab in a window changes
const processTabActivated = async (activeInfo) => {
console.log('A tab has been activated, processing...');
chrome.tabs.get(activeInfo.tabId, (tab) => {
actOnExtensions(tab);
})
}
// If domain is prohibited, disable it, otherwise enable
const actOnExtensions = async (tab) => {
// Check if site is in our list of sites to disable extensions for
if (isDomainProhibited(tab.url)) {
// Get list of extensions to disable
extensionsToDisable.forEach((extension) => {
disableExtension(extension, false);
});
} else {
extensionsToDisable.forEach((extension) => {
disableExtension(extension, true);
});
}
}
// Check if a specific url is in list of domains to disable extensions for
const isDomainProhibited = (url) => {
console.log('Checking if domain is on prohibited list for select extensions');
if (!url) {
return;
}
// Get hostname from url
url = new URL(url).hostname;
console.log(url);
// Compare current tab url to urls in selected sites
for (const selectedSite of selectedSites) {
if (selectedSite.startsWith('*.')) {
let trimmedSelectedSite = selectedSite.replace(/^\*\./, '');
if (url.endsWith(trimmedSelectedSite)) {
console.log('Domain is on prohibited list');
return true;
}
} else {
if (url == selectedSite) {
console.log('Domain is on prohibited list');
return true;
}
}
}
}
// Disable or enable a specific extension
const disableExtension = (extensionId, enabled) => {
console.log('Extension ID: ', extensionId);
chrome.management.setEnabled(extensionId, enabled, () => {
// TODO: Add error handling
if (enabled) {
console.log("Extension Enabled");
} else if (enabled === false) {
console.log("Extension Disabled");
} else {
console.log("Well, this is a bit embarrassing.");
}
});
}
getExtensionsToDisable();
getSitesToDisableExtensionsFor();
// Fires when a tab is updated, e.g. its title or url change
chrome.tabs.onUpdated.addListener(processTabUpdated);
// Fires when the active tab in a window changes
chrome.tabs.onActivated.addListener(processTabActivated);