bypass-paywalls-firefox-clean/options/options.js

141 lines
4.8 KiB
JavaScript
Raw Normal View History

// defaultSites are loaded from sites.js at installation extension (and are saved to local storage)
// var defaultSites = {};
var ext_api = chrome || browser;
2017-07-08 19:40:29 +02:00
// Saves options to ext_api.storage
2017-07-08 19:40:29 +02:00
function save_options() {
var gh_url = document.getElementById('bypass_sites').value;
var inputEls = document.querySelectorAll('#bypass_sites input');
var sites = {};
var sites = Array.from(inputEls).reduce(function(memo, inputEl) {
if (inputEl.checked) {
memo[inputEl.dataset.key] = inputEl.dataset.value;
}
return memo;
}, {});
ext_api.storage.local.set({
2017-07-08 19:40:29 +02:00
sites: sites
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
2021-01-05 18:53:59 +01:00
setTimeout(function () {
status.textContent = '';
}, 800);
});
2017-07-08 19:40:29 +02:00
}
// Restores checkbox input states using the preferences stored in ext_api.storage.
function renderOptions() {
var labelEl;
ext_api.storage.local.get({
sites: {},
sites_updated: {},
sites_custom: {},
sites_excluded: []
}, function (items) {
2017-07-08 19:40:29 +02:00
var sites = items.sites;
var sites_updated = items.sites_updated;
var sites_custom = items.sites_custom;
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');
var site_types = {
"updated": {
sites: sites_updated,
title: '* Updated (new) Sites (opt-in to custom sites)',
default_sites: false
},
"default": {
sites: defaultSites,
title: '* Default Sites',
default_sites: true
},
"custom": {
sites: sites_custom,
title: '* Custom (new) Sites',
default_sites: false
2017-07-08 19:40:29 +02:00
}
};
var first = true;
for (let site_type in site_types) {
if (!first)
labelEl.appendChild(document.createElement('hr'));
else
first = false;
labelEl = document.createElement('label');
labelEl.setAttribute('style', ' font-weight: bold;');
labelEl.appendChild(document.createTextNode(site_types[site_type].title));
sitesEl.appendChild(labelEl);
let sites_arr = site_types[site_type].sites
for (let key in sites_arr) {
let domain = sites_arr[key]['domain'];
2021-11-14 13:40:46 +01:00
if (!domain || (!site_types[site_type].default_sites && (defaultSites.hasOwnProperty(key) || defaultSites_domains.includes(domain))))
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);
2021-11-14 13:40:46 +01:00
if (domain !== '###') {
labelEl.appendChild(inputEl);
} else {
labelEl.appendChild(document.createElement('hr'));
labelEl.setAttribute('style', ' font-weight: bold;');
}
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;');
labelEl.appendChild(document.createTextNode('* Excluded Sites (ignored when checked in list)'));
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
function selectAll() {
var inputEls = Array.from(document.querySelectorAll('input'));
inputEls = inputEls.filter(function (input) {
return (!input.dataset.value.match(/^#options_(disable|optin)_/));
});
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() {
2021-02-17 19:11:27 +01:00
open(location).close();
2020-11-10 18:15:18 +01:00
}
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
}
2017-07-08 19:40:29 +02:00
document.addEventListener('DOMContentLoaded', renderOptions);
document.getElementById('save').addEventListener('click', save_options);
document.getElementById('select-all').addEventListener('click', selectAll);
document.getElementById('select-none').addEventListener('click', selectNone);
2020-11-10 18:15:18 +01:00
document.getElementById("button-close").addEventListener('click', closeButton);