2020-02-28 16:23:17 +01:00
|
|
|
|
2020-03-02 18:40:29 +01:00
|
|
|
// Saves options to browser.storage
|
2020-02-28 16:23:17 +01:00
|
|
|
function save_options() {
|
|
|
|
var gh_url = document.getElementById('bypass_sites').value;
|
|
|
|
var textareaEl = document.querySelector('#bypass_sites textarea');
|
|
|
|
var sites_custom = {};
|
|
|
|
if (textareaEl.value !== '')
|
|
|
|
var sites_custom = JSON.parse(textareaEl.value);
|
2020-03-02 18:40:29 +01:00
|
|
|
browser.storage.sync.set({
|
2020-02-28 16:23:17 +01:00
|
|
|
sites_custom: sites_custom
|
|
|
|
}, function () {
|
2020-03-02 18:36:11 +01:00
|
|
|
// Update status to let user know custom sites were saved.
|
2020-02-28 16:23:17 +01:00
|
|
|
var status = document.getElementById('status');
|
2020-03-02 18:36:11 +01:00
|
|
|
status.textContent = 'Custom sites saved.';
|
2020-02-28 16:23:17 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
status.textContent = '';
|
|
|
|
location.href = 'options.html';
|
|
|
|
//window.close();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:40:29 +01:00
|
|
|
// Add custom site to browser.storage
|
2020-02-28 16:23:17 +01:00
|
|
|
function add_options() {
|
|
|
|
var gh_url = document.getElementById('add_site').value;
|
|
|
|
var inputEls = document.querySelectorAll('#add_site input');
|
|
|
|
var sites_custom = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < inputEls.length; i++) {
|
|
|
|
if (inputEls[i].dataset.key === 'title') {
|
|
|
|
var title = inputEls[i].value;
|
|
|
|
if (title === '')
|
|
|
|
break;
|
|
|
|
sites_custom[title] = {};
|
|
|
|
} else {
|
|
|
|
if (inputEls[i].dataset.value) {
|
|
|
|
if (inputEls[i].checked)
|
|
|
|
sites_custom[title][inputEls[i].dataset.key] = inputEls[i].dataset.value;
|
|
|
|
} else
|
|
|
|
sites_custom[title][inputEls[i].dataset.key] = inputEls[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sites_custom[title]['domain'] === '')
|
|
|
|
sites_custom = {};
|
|
|
|
else
|
|
|
|
sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '');
|
|
|
|
|
|
|
|
// add new site to local storage
|
2020-03-02 18:40:29 +01:00
|
|
|
browser.storage.sync.get({
|
2020-02-28 16:23:17 +01:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var sites_custom_old = items.sites_custom;
|
|
|
|
|
|
|
|
for (var key in sites_custom) {
|
|
|
|
sites_custom_old[key] = sites_custom[key];
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:40:29 +01:00
|
|
|
browser.storage.sync.set({
|
2020-02-28 16:23:17 +01:00
|
|
|
sites_custom: sites_custom_old
|
|
|
|
}, function () {
|
2020-03-02 18:36:11 +01:00
|
|
|
// Update status to let user know new custom site was added.
|
2020-02-28 16:23:17 +01:00
|
|
|
var status = document.getElementById('status');
|
|
|
|
status.textContent = 'Site added.';
|
|
|
|
setTimeout(function () {
|
|
|
|
status.textContent = '';
|
2020-03-02 18:36:11 +01:00
|
|
|
renderOptions();
|
|
|
|
//location.href = 'options.html';
|
2020-02-28 16:23:17 +01:00
|
|
|
//window.close();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:40:29 +01:00
|
|
|
// Delete custom site to browser.storage
|
2020-03-02 18:36:11 +01:00
|
|
|
function delete_options() {
|
|
|
|
var gh_url = document.getElementById('custom_sites').value;
|
|
|
|
var selectEl = document.querySelector('#custom_sites select');
|
|
|
|
var sites_custom = {};
|
|
|
|
var remove_key = selectEl.value;
|
|
|
|
|
|
|
|
// delete site from local storage
|
2020-03-02 18:40:29 +01:00
|
|
|
browser.storage.sync.get({
|
2020-03-02 18:36:11 +01:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var sites_custom_old = items.sites_custom;
|
|
|
|
delete sites_custom_old[remove_key];
|
|
|
|
|
2020-03-02 18:40:29 +01:00
|
|
|
browser.storage.sync.set({
|
2020-03-02 18:36:11 +01:00
|
|
|
sites_custom: sites_custom_old
|
|
|
|
}, function () {
|
|
|
|
// Update status to let user know custom site was deleted.
|
|
|
|
var status = document.getElementById('status');
|
|
|
|
status.textContent = 'Site deleted.';
|
|
|
|
setTimeout(function () {
|
|
|
|
status.textContent = '';
|
|
|
|
renderOptions();
|
|
|
|
//location.href = 'options.html';
|
|
|
|
//window.close();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:40:29 +01:00
|
|
|
// Restores checkbox input states using the preferences stored in browser.storage.
|
2020-02-28 16:23:17 +01:00
|
|
|
function renderOptions() {
|
2020-03-02 18:40:29 +01:00
|
|
|
browser.storage.sync.get({
|
2020-02-28 16:23:17 +01:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var sites_custom = items.sites_custom;
|
|
|
|
var sitesEl = document.getElementById('bypass_sites');
|
2020-03-02 18:36:11 +01:00
|
|
|
sitesEl.innerHTML = '';
|
2020-02-28 16:23:17 +01:00
|
|
|
var labelEl = document.createElement('label');
|
|
|
|
var textareaEl = document.createElement('textarea');
|
|
|
|
textareaEl.value = JSON.stringify(sites_custom);
|
|
|
|
textareaEl.rows = 12;
|
|
|
|
textareaEl.cols = 45;
|
|
|
|
labelEl.appendChild(textareaEl);
|
|
|
|
sitesEl.appendChild(labelEl);
|
|
|
|
|
|
|
|
// add site
|
|
|
|
var add_sitesEl = document.getElementById('add_site');
|
2020-03-02 18:36:11 +01:00
|
|
|
add_sitesEl.innerHTML = '';
|
2020-02-28 16:23:17 +01:00
|
|
|
var inputEl;
|
|
|
|
var add_checkboxes = {
|
|
|
|
'title': 0,
|
|
|
|
'domain': 0,
|
|
|
|
'googlebot': 1,
|
|
|
|
'block_javascript': 1
|
|
|
|
};
|
|
|
|
for (var key in add_checkboxes) {
|
|
|
|
labelEl = document.createElement('label');
|
|
|
|
inputEl = document.createElement('input');
|
|
|
|
inputEl.dataset.key = key;
|
|
|
|
labelEl.appendChild(inputEl);
|
|
|
|
if (add_checkboxes[key]) {
|
|
|
|
inputEl.type = 'checkbox';
|
|
|
|
inputEl.dataset.value = 1;
|
|
|
|
}
|
|
|
|
labelEl.appendChild(document.createTextNode(' ' + key));
|
|
|
|
add_sitesEl.appendChild(labelEl);
|
|
|
|
}
|
2020-03-02 18:36:11 +01:00
|
|
|
|
|
|
|
// list of custom sites
|
|
|
|
var custom_sitesEl = document.getElementById('custom_sites');
|
|
|
|
custom_sitesEl.innerHTML = '';
|
|
|
|
labelEl = document.createElement('label');
|
|
|
|
var selectEl = document.createElement('select');
|
|
|
|
selectEl.id = 'sites';
|
|
|
|
selectEl.size = 6;
|
|
|
|
var optionEl;
|
|
|
|
for (var key in sites_custom) {
|
|
|
|
optionEl = document.createElement('option');
|
|
|
|
optionEl.text = key + ': ' + sites_custom[key]['domain'] +
|
|
|
|
(sites_custom[key]['googlebot'] ? ' | googlebot' : '') + (sites_custom[key]['block_javascript'] ? ' | block javascript' : '');
|
|
|
|
optionEl.value = key;
|
|
|
|
selectEl.add(optionEl);
|
|
|
|
}
|
|
|
|
labelEl.appendChild(selectEl);
|
|
|
|
//labelEl.appendChild(document.createTextNode(''));
|
|
|
|
custom_sitesEl.appendChild(labelEl);
|
|
|
|
|
2020-02-28 16:23:17 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', renderOptions);
|
|
|
|
document.getElementById('save').addEventListener('click', save_options);
|
2020-03-02 18:36:11 +01:00
|
|
|
document.getElementById('add').addEventListener('click', add_options);
|
|
|
|
document.getElementById('delete').addEventListener('click', delete_options);
|