mirror of
https://gitlab.com/magnolia1234/bypass-paywalls-firefox-clean.git
synced 2024-11-10 10:47:49 +01:00
9cde15f19d
Fix json-bug when not saving options at start Further some maintenance on background.js: combine enabledSites and initialize allow/remove_cookies and sort blockedRegexes.
102 lines
3.2 KiB
JavaScript
102 lines
3.2 KiB
JavaScript
// defaultSites are loaded from sites(_custom).json at installation extension (and are saved to local storage)
|
|
var defaultSites = {};
|
|
|
|
// Saves options to browser.storage
|
|
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;
|
|
}, {});
|
|
|
|
browser.storage.sync.set({
|
|
sites: sites
|
|
}, function() {
|
|
// Update status to let user know options were saved.
|
|
var status = document.getElementById('status');
|
|
status.textContent = 'Options saved.';
|
|
setTimeout(function() {
|
|
// status.textContent = '';
|
|
window.close();
|
|
}, 500);
|
|
});
|
|
}
|
|
|
|
//Fetch sites.json & sites_custom.json
|
|
function renderOptions() {
|
|
const url_sites = browser.runtime.getURL('sites.json');
|
|
fetch(url_sites)
|
|
.then(response => {
|
|
if (response.ok) {
|
|
response.json().then(json => {
|
|
var defaultSites_merge = {...defaultSites, ...json};
|
|
defaultSites = defaultSites_merge;
|
|
// add custom sites
|
|
const url_sites_custom = 'https://raw.githubusercontent.com/magnolia1234/bypass-paywalls-firefox-clean/master/sites_custom.json';
|
|
fetch(url_sites_custom)
|
|
.then(response => {
|
|
if (response.ok) {
|
|
response.json().then(json => {
|
|
var defaultSites_merge = {...defaultSites, ...json};
|
|
defaultSites = defaultSites_merge;
|
|
renderOptions_default();
|
|
})
|
|
} else { renderOptions_default(); }
|
|
} );
|
|
})
|
|
} else { renderOptions_default(); }
|
|
} );
|
|
}
|
|
|
|
// Restores checkbox input states using the preferences
|
|
// stored in browser.storage.
|
|
function renderOptions_default() {
|
|
browser.storage.sync.get({
|
|
sites: {}
|
|
}, function(items) {
|
|
var sites = items.sites;
|
|
var sitesEl = document.getElementById('bypass_sites');
|
|
for (var key in defaultSites) {
|
|
if (!defaultSites.hasOwnProperty(key)) {
|
|
continue;
|
|
}
|
|
|
|
var value = defaultSites[key];
|
|
var labelEl = document.createElement('label');
|
|
var inputEl = document.createElement('input');
|
|
inputEl.type = 'checkbox';
|
|
inputEl.dataset.key = key;
|
|
inputEl.dataset.value = value;
|
|
inputEl.checked = (key in sites) || (key.replace(/\s\(.*\)/, '') in sites);
|
|
if (value !=='###') {
|
|
labelEl.appendChild(inputEl);
|
|
}
|
|
labelEl.appendChild(document.createTextNode(' '+key));
|
|
sitesEl.appendChild(labelEl);
|
|
}
|
|
});
|
|
}
|
|
|
|
function selectAll() {
|
|
var inputEls = Array.from(document.querySelectorAll('input'));
|
|
inputEls.forEach(function(inputEl) {
|
|
inputEl.checked = true;
|
|
});
|
|
}
|
|
|
|
function selectNone() {
|
|
var inputEls = Array.from(document.querySelectorAll('input'));
|
|
inputEls.forEach(function(inputEl) {
|
|
inputEl.checked = false;
|
|
});
|
|
}
|
|
|
|
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);
|