-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
91 lines (76 loc) · 3.44 KB
/
popup.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
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
const BLOCKLISTS = [
{ name: ' - oisd big', url: 'https://big.oisd.nl/' },
{ name: ' - oisd small', url: 'https://small.oisd.nl/' },
{ name: " - HaGeZi's Pro DNS Blocklist", url: 'https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/pro.txt' },
{ name: " - HaGeZi's Normal DNS Blocklist", url: 'https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/multi.txt' },
{ name: ' - notracking', url: 'https://raw.githubusercontent.com/notracking/hosts-blocklists/master/adblock/adblock.txt' },
{ name: ' - 1Hosts (Pro)', url: 'https://o0.pages.dev/Pro/adblock.txt' },
{ name: ' - 1Hosts (Lite)', url: 'https://o0.pages.dev/Lite/adblock.txt' },
{ name: ' - hBlock', url: 'https://hblock.molinero.dev/hosts_adblock.txt' },
{ name: ' - NoTrack Tracker Blocklist', url: 'https://gitlab.com/quidsup/notrack-blocklists/-/raw/master/trackers.hosts' },
];
const blocklistContainer = document.getElementById('blocklist-container');
BLOCKLISTS.forEach((blocklist, index) => {
const listItem = document.createElement('li');
listItem.classList.add('blocklist-item');
const input = document.createElement('input');
input.type = 'checkbox';
input.id = `blocklist-${index}`;
const label = document.createElement('label');
label.htmlFor = `blocklist-${index}`;
label.textContent = blocklist.name;
const link = document.createElement('a');
link.href = blocklist.url;
link.target = '_blank';
link.rel = 'noopener noreferrer';
link.className = 'external-link';
const icon = document.createElement('i');
icon.className = 'fas fa-up-right-from-square';
link.appendChild(icon);
listItem.appendChild(input);
listItem.appendChild(label);
listItem.appendChild(link);
blocklistContainer.appendChild(listItem);
});
document.getElementById('generate-filter').addEventListener('click', () => {
const fileInput = document.getElementById('file-input');
const files = fileInput.files;
const selectedBlocklists = BLOCKLISTS.filter((_, index) => {
return document.getElementById(`blocklist-${index}`).checked;
});
if (files.length === 0 && selectedBlocklists.length === 0) {
alert('Please select at least one file or blocklist to generate the filter.');
return;
}
const blocklistFetchPromises = selectedBlocklists.map((blocklist) => fetch(blocklist.url).then((response) => response.text()));
const fileContentsPromises = Array.from(files).map((file) => readFileAsText(file));
Promise.all([...blocklistFetchPromises, ...fileContentsPromises])
.then((contents) => {
chrome.runtime.sendMessage(
{ action: 'generateFilter', fileContents: contents },
(response) => {
if (response && response.error) {
alert(response.error);
}
}
);
})
.catch((error) => {
alert('Error reading files. Try again.');
});
});
function readFileAsText(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsText(file);
});
}
document.getElementById('file-input').addEventListener('change', (event) => {
const fileInput = event.target;
const fileNames = Array.from(fileInput.files)
.map((file) => file.name)
.join(', ');
document.getElementById('file-selected-label').textContent = fileNames || 'No file chosen';
});