2023-12-20 09:06:44 +01:00
|
|
|
var ext_api = (typeof browser === 'object') ? browser : chrome;
|
2017-07-08 19:40:29 +02:00
|
|
|
|
2020-03-25 19:31:28 +01:00
|
|
|
// Saves options to ext_api.storage
|
2024-02-19 13:13:06 +01:00
|
|
|
function save_options(event) {
|
2017-07-08 19:40:29 +02:00
|
|
|
var inputEls = document.querySelectorAll('#bypass_sites input');
|
|
|
|
var sites = {};
|
2024-03-13 19:44:25 +01:00
|
|
|
|
|
|
|
var sites = Array.from(inputEls).reduce(function (memo, inputEl) {
|
2017-07-08 19:40:29 +02:00
|
|
|
if (inputEl.checked) {
|
|
|
|
memo[inputEl.dataset.key] = inputEl.dataset.value;
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}, {});
|
2024-03-13 19:44:25 +01:00
|
|
|
|
2020-10-31 20:06:38 +01:00
|
|
|
ext_api.storage.local.set({
|
2017-07-08 19:40:29 +02:00
|
|
|
sites: sites
|
2024-03-13 19:44:25 +01:00
|
|
|
}, function () {
|
2017-07-08 19:40:29 +02:00
|
|
|
// Update status to let user know options were saved.
|
2024-03-13 19:44:25 +01:00
|
|
|
if (event) {
|
|
|
|
var status_label = document.querySelectorAll('[id^="status"]');
|
|
|
|
for (let status of status_label) {
|
|
|
|
status.textContent = 'Options saved.';
|
|
|
|
setTimeout(function () {
|
|
|
|
status.textContent = '';
|
|
|
|
}, 800);
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 18:19:35 +01:00
|
|
|
});
|
2017-07-08 19:40:29 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 19:31:28 +01:00
|
|
|
// Restores checkbox input states using the preferences stored in ext_api.storage.
|
2020-02-25 22:47:05 +01:00
|
|
|
function renderOptions() {
|
2020-11-24 17:12:47 +01:00
|
|
|
var labelEl;
|
2020-10-31 20:06:38 +01:00
|
|
|
ext_api.storage.local.get({
|
2021-11-13 11:43:09 +01:00
|
|
|
sites: {},
|
|
|
|
sites_updated: {},
|
|
|
|
sites_custom: {},
|
|
|
|
sites_excluded: []
|
|
|
|
}, function (items) {
|
2017-07-08 19:40:29 +02:00
|
|
|
var sites = items.sites;
|
2024-02-26 13:02:53 +01:00
|
|
|
var sites_updated = filterObject(items.sites_updated, function (val, key) {
|
|
|
|
return !val.nofix
|
|
|
|
});
|
2023-11-05 18:48:46 +01:00
|
|
|
var sites_updated_domains_new = Object.values(sites_updated).filter(x => (x.domain && !defaultSites_domains.includes(x.domain) || x.group)).map(x => x.group ? x.group.filter(y => !defaultSites_domains.includes(y)) : x.domain).flat();
|
2024-03-01 17:42:36 +01:00
|
|
|
var sites_updated_perm_domains_new = Object.values(sites_updated).filter(x => x.block_host_perm_add).map(x => x.block_host_perm_add.split(',').filter(x => x).map(x => x.trim())).flat();
|
2021-11-13 11:43:09 +01:00
|
|
|
var sites_custom = items.sites_custom;
|
2023-11-05 18:48:46 +01:00
|
|
|
var sites_custom_domains_new = Object.values(sites_custom).filter(x => x.domain && !defaultSites_domains.includes(x.domain)).map(x => x.group ? x.group.split(',').map(x => x.trim()) : x.domain).flat();
|
2024-03-01 17:42:36 +01:00
|
|
|
var sites_custom_perm_domains_new = Object.values(sites_custom).filter(x => x.block_host_perm_add).map(x => x.block_host_perm_add.split(',').filter(x => x).map(x => x.trim())).flat();
|
2021-12-15 17:44:52 +01:00
|
|
|
|
2024-03-01 17:42:36 +01:00
|
|
|
var perm_origins = sites_custom_domains_new.concat(sites_updated_domains_new, sites_custom_perm_domains_new, sites_updated_perm_domains_new).filter(x => !x.includes('###')).map(x => '*://*.' + x + '/*');
|
2021-12-15 17:44:52 +01:00
|
|
|
var perm_custom = document.getElementById('perm-custom');
|
|
|
|
ext_api.permissions.contains({
|
|
|
|
origins: perm_origins
|
|
|
|
}, function (result) {
|
|
|
|
if (result) {
|
|
|
|
perm_custom.innerText = '';
|
|
|
|
} else {
|
2024-03-01 17:42:36 +01:00
|
|
|
perm_custom.textContent = ">> check host (domain) permissions for custom/updated sites";
|
2021-12-15 17:44:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-05 18:53:59 +01:00
|
|
|
var sites_excluded = items.sites_excluded;
|
2017-07-08 19:40:29 +02:00
|
|
|
var sitesEl = document.getElementById('bypass_sites');
|
2021-11-13 11:43:09 +01:00
|
|
|
var site_types = {
|
|
|
|
"updated": {
|
|
|
|
sites: sites_updated,
|
2022-11-06 17:02:23 +01:00
|
|
|
title: '* Updated (new) sites (opt-in to custom sites)',
|
2021-11-13 11:43:09 +01:00
|
|
|
default_sites: false
|
|
|
|
},
|
|
|
|
"default": {
|
|
|
|
sites: defaultSites,
|
|
|
|
default_sites: true
|
|
|
|
},
|
|
|
|
"custom": {
|
|
|
|
sites: sites_custom,
|
|
|
|
default_sites: false
|
2017-07-08 19:40:29 +02:00
|
|
|
}
|
2021-11-13 11:43:09 +01:00
|
|
|
};
|
|
|
|
for (let site_type in site_types) {
|
2020-11-24 17:12:47 +01:00
|
|
|
labelEl = document.createElement('label');
|
2021-11-13 11:43:09 +01:00
|
|
|
labelEl.setAttribute('style', ' font-weight: bold;');
|
2024-02-19 13:13:06 +01:00
|
|
|
if (site_types[site_type].title)
|
|
|
|
labelEl.appendChild(document.createTextNode(site_types[site_type].title));
|
2020-02-28 16:23:17 +01:00
|
|
|
sitesEl.appendChild(labelEl);
|
2022-04-13 08:13:00 +02:00
|
|
|
let sites_arr = site_types[site_type].sites;
|
|
|
|
for (let key in sites_arr) {
|
|
|
|
let domain = sites_arr[key]['domain'];
|
2023-09-18 08:20:00 +02:00
|
|
|
if (!domain || (key === '###_remove_sites') || (!site_types[site_type].default_sites && (defaultSites.hasOwnProperty(key) || defaultSites_domains.includes(domain))))
|
2022-04-13 08:13:00 +02:00
|
|
|
continue;
|
|
|
|
labelEl = document.createElement('label');
|
|
|
|
let inputEl = document.createElement('input');
|
|
|
|
inputEl.type = 'checkbox';
|
|
|
|
inputEl.dataset.key = key;
|
|
|
|
inputEl.dataset.value = domain;
|
|
|
|
inputEl.checked = Object.keys(sites).some(title => compareKey(title, key)) && !sites_excluded.includes(domain);
|
|
|
|
if (domain !== '###') {
|
|
|
|
labelEl.appendChild(inputEl);
|
|
|
|
} else {
|
|
|
|
labelEl.appendChild(document.createElement('hr'));
|
|
|
|
labelEl.setAttribute('style', ' font-weight: bold;');
|
2021-11-13 11:43:09 +01:00
|
|
|
}
|
2022-04-13 08:13:00 +02:00
|
|
|
labelEl.appendChild(document.createTextNode(' ' + key));
|
|
|
|
sitesEl.appendChild(labelEl);
|
|
|
|
}
|
2020-07-02 12:43:12 +02:00
|
|
|
}
|
2021-01-05 18:53:59 +01:00
|
|
|
// excluded
|
|
|
|
labelEl.appendChild(document.createElement('hr'));
|
|
|
|
labelEl = document.createElement('label');
|
|
|
|
labelEl.setAttribute('style', ' font-weight: bold;');
|
2024-02-19 13:13:06 +01:00
|
|
|
labelEl.appendChild(document.createTextNode('* Excluded Sites (domain(s) ignored when checked in list)'));
|
2021-11-13 11:43:09 +01:00
|
|
|
sitesEl.appendChild(labelEl);
|
2021-01-05 18:53:59 +01:00
|
|
|
labelEl = document.createElement('label');
|
|
|
|
labelEl.appendChild(document.createTextNode(sites_excluded.join()));
|
|
|
|
sitesEl.appendChild(labelEl);
|
|
|
|
save_options();
|
2017-07-08 19:40:29 +02:00
|
|
|
});
|
2021-02-17 19:11:27 +01:00
|
|
|
}
|
2017-07-08 19:40:29 +02:00
|
|
|
|
2024-03-13 19:44:25 +01:00
|
|
|
function handleSearch() {
|
|
|
|
let search = document.getElementById('search').value.toLowerCase().replace('www.', '');
|
|
|
|
let listItems = document.querySelectorAll('#bypass_sites > label');
|
|
|
|
grouped_sites = filterObject(grouped_sites, function (val, key) {
|
|
|
|
return val.length
|
|
|
|
});
|
|
|
|
ext_api.storage.local.get({
|
|
|
|
sites_updated: {},
|
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
let sites_updated_groups = filterObject(items.sites_updated, function (val, key) {
|
|
|
|
return val.group
|
|
|
|
}, function (val, key) {
|
|
|
|
return [val.domain, val.group]
|
|
|
|
});
|
|
|
|
for (let site in sites_updated_groups) {
|
|
|
|
let site_default = Object.keys(defaultSites).find(key => compareKey(key, site)) || site;
|
|
|
|
grouped_sites[site_default] = sites_updated_groups[site];
|
|
|
|
}
|
|
|
|
let sites_custom_groups = filterObject(items.sites_custom, function (val, key) {
|
|
|
|
return val.group
|
|
|
|
}, function (val, key) {
|
|
|
|
return [val.domain, val.group.split(',')]
|
|
|
|
});
|
|
|
|
for (let site in sites_custom_groups)
|
|
|
|
grouped_sites[site] = sites_custom_groups[site];
|
|
|
|
for (let item of listItems) {
|
|
|
|
let itemText = item.textContent.toLowerCase();
|
|
|
|
let itemInput = item.querySelector('input[data-value]');
|
|
|
|
let itemDomain = itemInput ? itemInput.getAttribute('data-value') : '';
|
|
|
|
let itemGroup = itemDomain ? grouped_sites[itemDomain] : '';
|
|
|
|
if (itemText.includes(search) || !itemDomain || (itemDomain && (itemDomain.match(/^(###$|#options_[^d])/) || itemDomain.includes(search) || (itemGroup && itemGroup.includes(search)))))
|
|
|
|
item.style.display = 'block';
|
|
|
|
else
|
|
|
|
item.style.display = 'none';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let selectButtons = document.querySelectorAll('#select-all, #select-none');
|
|
|
|
for (let elem of selectButtons) {
|
|
|
|
if (search == '')
|
|
|
|
elem.style.display = 'block';
|
|
|
|
else
|
|
|
|
elem.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:40:29 +02:00
|
|
|
function selectAll() {
|
2024-03-13 19:44:25 +01:00
|
|
|
var inputEls = Array.from(document.querySelectorAll('input[data-key]'));
|
2021-03-26 13:05:20 +01:00
|
|
|
inputEls = inputEls.filter(function (input) {
|
2021-11-07 08:47:42 +01:00
|
|
|
return (!input.dataset.value.match(/^#options_(disable|optin)_/));
|
2021-03-26 13:05:20 +01:00
|
|
|
});
|
2021-01-05 18:53:59 +01:00
|
|
|
inputEls.forEach(function (inputEl) {
|
2017-07-08 19:40:29 +02:00
|
|
|
inputEl.checked = true;
|
|
|
|
});
|
2021-01-05 18:53:59 +01:00
|
|
|
// Update status to let user know all sites are selected.
|
|
|
|
var status = document.getElementById('status');
|
|
|
|
status.textContent = 'All sites selected.';
|
|
|
|
setTimeout(function () {
|
|
|
|
status.textContent = '';
|
|
|
|
}, 800);
|
2017-07-08 19:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectNone() {
|
|
|
|
var inputEls = Array.from(document.querySelectorAll('input'));
|
|
|
|
inputEls.forEach(function(inputEl) {
|
|
|
|
inputEl.checked = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:15:18 +01:00
|
|
|
function closeButton() {
|
2022-01-10 19:08:33 +01:00
|
|
|
window.close();
|
2020-11-10 18:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:02:07 +01:00
|
|
|
function check_sites_updated() {
|
|
|
|
ext_api.runtime.sendMessage({request: 'check_sites_updated'});
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
|
2023-07-25 07:21:11 +02:00
|
|
|
function clear_sites_updated() {
|
|
|
|
ext_api.runtime.sendMessage({request: 'clear_sites_updated'});
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:59:07 +02:00
|
|
|
function compareKey(firstStr, secondStr) {
|
|
|
|
return firstStr.toLowerCase().replace(/\s\(.*\)/, '') === secondStr.toLowerCase().replace(/\s\(.*\)/, '');
|
2021-08-29 15:15:40 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 13:02:53 +01:00
|
|
|
function filterObject(obj, filterFn, mapFn = function (val, key) {
|
|
|
|
return [key, val];
|
|
|
|
}) {
|
|
|
|
return Object.fromEntries(Object.entries(obj).
|
|
|
|
filter(([key, val]) => filterFn(val, key)).map(([key, val]) => mapFn(val, key)));
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:40:29 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', renderOptions);
|
|
|
|
document.getElementById('save').addEventListener('click', save_options);
|
2024-02-19 13:13:06 +01:00
|
|
|
document.getElementById('save_top').addEventListener('click', save_options);
|
2017-07-08 19:40:29 +02:00
|
|
|
document.getElementById('select-all').addEventListener('click', selectAll);
|
|
|
|
document.getElementById('select-none').addEventListener('click', selectNone);
|
2024-03-13 19:44:25 +01:00
|
|
|
document.getElementById('button-close').addEventListener('click', closeButton);
|
2023-07-25 07:21:11 +02:00
|
|
|
document.getElementById('check_sites_updated').addEventListener('click', check_sites_updated);
|
|
|
|
document.getElementById('clear_sites_updated').addEventListener('click', clear_sites_updated);
|
2024-03-13 19:44:25 +01:00
|
|
|
document.getElementById('search').addEventListener('input', handleSearch);
|