2020-03-25 18:31:28 +00:00
|
|
|
var ext_api = chrome || browser;
|
2020-02-28 15:23:17 +00:00
|
|
|
|
2021-02-06 20:37:52 +00:00
|
|
|
var referer_options = ['', 'facebook', 'google', 'twitter'];
|
|
|
|
|
2020-04-11 17:40:12 +00:00
|
|
|
function capitalize(str) {
|
|
|
|
if (typeof str !== 'string') return '';
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortJson(json) {
|
|
|
|
return Object.keys(json)
|
|
|
|
.sort().reduce(function (Obj, key) {
|
|
|
|
Obj[key] = json[key];
|
|
|
|
return Obj;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:31:28 +00:00
|
|
|
// Saves options to ext_api.storage
|
2020-02-28 15:23:17 +00:00
|
|
|
function save_options() {
|
|
|
|
var textareaEl = document.querySelector('#bypass_sites textarea');
|
|
|
|
var sites_custom = {};
|
|
|
|
if (textareaEl.value !== '')
|
|
|
|
var sites_custom = JSON.parse(textareaEl.value);
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.set({
|
2020-02-28 15:23:17 +00:00
|
|
|
sites_custom: sites_custom
|
|
|
|
}, function () {
|
2020-03-02 17:36:11 +00:00
|
|
|
// Update status to let user know custom sites were saved.
|
2020-02-28 15:23:17 +00:00
|
|
|
var status = document.getElementById('status');
|
2020-03-02 17:36:11 +00:00
|
|
|
status.textContent = 'Custom sites saved.';
|
2020-02-28 15:23:17 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
status.textContent = '';
|
|
|
|
location.href = 'options.html';
|
|
|
|
//window.close();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-11 17:40:12 +00:00
|
|
|
// Sort json by key in textarea
|
|
|
|
function sort_options() {
|
|
|
|
var textareaEl = document.querySelector('#bypass_sites textarea');
|
|
|
|
var sites_custom = {};
|
|
|
|
if (textareaEl.value !== '') {
|
|
|
|
var sites_custom = JSON.parse(textareaEl.value);
|
|
|
|
var sites_custom_sorted = sortJson(sites_custom);
|
|
|
|
textareaEl.value = JSON.stringify(sites_custom_sorted);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:45:19 +00:00
|
|
|
// Export custom sites to file
|
|
|
|
function export_options() {
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.get({
|
2020-03-08 18:45:19 +00:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var result = JSON.stringify(items.sites_custom);
|
2020-03-12 18:27:19 +00:00
|
|
|
var a = document.createElement("a");
|
|
|
|
var file = new Blob([result], {type: "text/plain"});
|
|
|
|
a.href = window.URL.createObjectURL(file);
|
|
|
|
let date = new Date();
|
|
|
|
let dateStr = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().split("T")[0];
|
|
|
|
a.download = 'bypass_paywalls_clean_custom_' + dateStr + '.txt';
|
|
|
|
a.click();
|
2020-03-08 18:45:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import custom sites from file
|
|
|
|
function import_options(e) {
|
|
|
|
var files = e.target.files;
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = _imp;
|
|
|
|
reader.readAsText(files[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _imp() {
|
|
|
|
let sites_custom = JSON.parse(this.result);
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.set({
|
2020-03-08 18:45:19 +00:00
|
|
|
sites_custom: sites_custom
|
|
|
|
}, function () {
|
|
|
|
// Update status to let user know custom sites were imported.
|
|
|
|
var status = document.getElementById('status');
|
|
|
|
status.textContent = 'Custom sites imported.';
|
|
|
|
setTimeout(function () {
|
|
|
|
//status.textContent = '';
|
|
|
|
importInput.value = '';
|
|
|
|
renderOptions();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:31:28 +00:00
|
|
|
// Add custom site to ext_api.storage
|
2020-02-28 15:23:17 +00:00
|
|
|
function add_options() {
|
2021-02-06 20:37:52 +00:00
|
|
|
var inputEls = document.querySelectorAll('#add_site input, #add_site select');
|
2020-02-28 15:23:17 +00:00
|
|
|
var sites_custom = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < inputEls.length; i++) {
|
|
|
|
if (inputEls[i].dataset.key === 'title') {
|
2020-04-11 17:40:12 +00:00
|
|
|
var title = capitalize(inputEls[i].value);
|
2020-02-28 15:23:17 +00:00
|
|
|
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;
|
2021-04-03 07:37:45 +00:00
|
|
|
} else if (inputEls[i].value)
|
2020-02-28 15:23:17 +00:00
|
|
|
sites_custom[title][inputEls[i].dataset.key] = inputEls[i].value;
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 11:52:43 +00:00
|
|
|
|
|
|
|
if (title && sites_custom[title]['domain']) {
|
2020-04-11 17:40:12 +00:00
|
|
|
sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '').toLowerCase();
|
2020-02-28 15:23:17 +00:00
|
|
|
|
2020-08-22 11:52:43 +00:00
|
|
|
// add new site to local storage
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.get({
|
2020-08-22 11:52:43 +00:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var sites_custom_old = items.sites_custom;
|
2020-02-28 15:23:17 +00:00
|
|
|
|
2020-08-22 11:52:43 +00:00
|
|
|
for (var key in sites_custom) {
|
|
|
|
sites_custom_old[key] = sites_custom[key];
|
|
|
|
}
|
2020-02-28 15:23:17 +00:00
|
|
|
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.set({
|
2020-08-22 11:52:43 +00:00
|
|
|
sites_custom: sites_custom_old
|
|
|
|
}, function () {
|
|
|
|
// Update status to let user know new custom site was added.
|
|
|
|
var status_add = document.getElementById('status_add');
|
|
|
|
status_add.textContent = 'Site added.';
|
|
|
|
setTimeout(function () {
|
|
|
|
//status.textContent = '';
|
|
|
|
renderOptions();
|
|
|
|
}, 800);
|
|
|
|
});
|
2020-02-28 15:23:17 +00:00
|
|
|
});
|
2020-08-22 11:52:43 +00:00
|
|
|
}
|
2020-02-28 15:23:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 18:31:28 +00:00
|
|
|
// Delete custom site from ext_api.storage
|
2020-03-02 17:36:11 +00:00
|
|
|
function delete_options() {
|
|
|
|
var selectEl = document.querySelector('#custom_sites select');
|
|
|
|
var sites_custom = {};
|
|
|
|
var remove_key = selectEl.value;
|
|
|
|
|
|
|
|
// delete site from local storage
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.get({
|
2020-03-02 17:36:11 +00:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var sites_custom_old = items.sites_custom;
|
|
|
|
delete sites_custom_old[remove_key];
|
|
|
|
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.set({
|
2020-03-02 17:36:11 +00:00
|
|
|
sites_custom: sites_custom_old
|
|
|
|
}, function () {
|
|
|
|
// Update status to let user know custom site was deleted.
|
2020-03-12 18:27:19 +00:00
|
|
|
var status_delete = document.getElementById('status_delete');
|
2020-08-16 07:27:33 +00:00
|
|
|
status_delete.textContent = 'Site deleted.';
|
2020-03-02 17:36:11 +00:00
|
|
|
setTimeout(function () {
|
2020-03-12 18:27:19 +00:00
|
|
|
//status.textContent = '';
|
2020-03-02 17:36:11 +00:00
|
|
|
renderOptions();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:19:15 +00:00
|
|
|
// Edit custom site (copy to add)
|
|
|
|
function edit_options() {
|
|
|
|
var selectEl = document.querySelector('#custom_sites select');
|
|
|
|
var sites_custom = {};
|
|
|
|
var title = selectEl.value;
|
|
|
|
|
|
|
|
// copy site to add-fields
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.get({
|
2020-08-18 18:19:15 +00:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
sites_custom = items.sites_custom;
|
|
|
|
var edit_site = sites_custom[title];
|
|
|
|
document.querySelector('input[data-key="title"]').value = title;
|
|
|
|
document.querySelector('input[data-key="domain"]').value = edit_site.domain;
|
|
|
|
document.querySelector('input[data-key="googlebot"]').checked = (edit_site.googlebot > 0);
|
2020-11-10 17:15:18 +00:00
|
|
|
document.querySelector('input[data-key="allow_cookies"]').checked = (edit_site.allow_cookies > 0);
|
2020-08-18 18:19:15 +00:00
|
|
|
document.querySelector('input[data-key="block_javascript"]').checked = (edit_site.block_javascript > 0);
|
|
|
|
document.querySelector('input[data-key="block_javascript_ext"]').checked = (edit_site.block_javascript_ext > 0);
|
2021-02-06 20:37:52 +00:00
|
|
|
document.querySelector('select[data-key="referer"]').selectedIndex = referer_options.indexOf(edit_site.referer);
|
2020-08-18 18:19:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:35:59 +00:00
|
|
|
// request permissions for custom sites (in list only)
|
|
|
|
function request_permissions() {
|
|
|
|
var perm_custom = document.getElementById('perm-custom');
|
|
|
|
ext_api.permissions.request({
|
|
|
|
origins: perm_origins
|
|
|
|
}, function (granted) {
|
|
|
|
if (granted) {
|
|
|
|
perm_custom.innerText = 'YES';
|
|
|
|
} else {
|
|
|
|
perm_custom.innerText = 'NO';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove permissions for custom sites
|
|
|
|
function remove_permissions() {
|
|
|
|
var perm_custom = document.getElementById('perm-custom');
|
|
|
|
var custom_enabled = document.getElementById('custom-enabled');
|
|
|
|
ext_api.permissions.remove({
|
2021-05-02 05:51:25 +00:00
|
|
|
origins: perm_origins
|
2021-04-29 17:35:59 +00:00
|
|
|
}, function (removed) {
|
|
|
|
if (removed) {
|
|
|
|
perm_custom.innerText = 'NO';
|
|
|
|
custom_enabled.innerText = 'NO';
|
|
|
|
ext_api.storage.local.set({
|
|
|
|
"customOptIn": false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var perm_origins;
|
2020-03-25 18:31:28 +00:00
|
|
|
// Restores checkbox input states using the preferences stored in ext_api.storage.
|
2020-02-28 15:23:17 +00:00
|
|
|
function renderOptions() {
|
2020-10-31 19:06:38 +00:00
|
|
|
ext_api.storage.local.get({
|
2020-02-28 15:23:17 +00:00
|
|
|
sites_custom: {}
|
|
|
|
}, function (items) {
|
|
|
|
var sites_custom = items.sites_custom;
|
|
|
|
var sitesEl = document.getElementById('bypass_sites');
|
2020-03-02 17:36:11 +00:00
|
|
|
sitesEl.innerHTML = '';
|
2020-02-28 15:23:17 +00:00
|
|
|
var labelEl = document.createElement('label');
|
|
|
|
var textareaEl = document.createElement('textarea');
|
|
|
|
textareaEl.value = JSON.stringify(sites_custom);
|
|
|
|
textareaEl.rows = 12;
|
2020-03-17 11:16:32 +00:00
|
|
|
textareaEl.cols = 40;
|
2020-02-28 15:23:17 +00:00
|
|
|
labelEl.appendChild(textareaEl);
|
|
|
|
sitesEl.appendChild(labelEl);
|
|
|
|
|
|
|
|
// add site
|
|
|
|
var add_sitesEl = document.getElementById('add_site');
|
2020-03-02 17:36:11 +00:00
|
|
|
add_sitesEl.innerHTML = '';
|
2020-02-28 15:23:17 +00:00
|
|
|
var inputEl;
|
|
|
|
var add_checkboxes = {
|
|
|
|
'title': 0,
|
|
|
|
'domain': 0,
|
|
|
|
'googlebot': 1,
|
2020-11-10 17:15:18 +00:00
|
|
|
'allow_cookies': 1,
|
2020-06-04 07:04:58 +00:00
|
|
|
'block_javascript': 1,
|
|
|
|
'block_javascript_ext': 1
|
2020-02-28 15:23:17 +00:00
|
|
|
};
|
|
|
|
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;
|
2020-03-08 18:45:19 +00:00
|
|
|
} else if (key === 'title') {
|
|
|
|
inputEl.placeholder = 'Example';
|
|
|
|
} else if (key === 'domain')
|
|
|
|
inputEl.placeholder = 'example.com';
|
2020-02-28 15:23:17 +00:00
|
|
|
labelEl.appendChild(document.createTextNode(' ' + key));
|
|
|
|
add_sitesEl.appendChild(labelEl);
|
|
|
|
}
|
2020-03-02 17:36:11 +00:00
|
|
|
|
2021-02-06 20:37:52 +00:00
|
|
|
labelEl = document.createElement('label');
|
|
|
|
labelEl.appendChild(document.createTextNode('referer '));
|
|
|
|
inputEl = document.createElement('select');
|
|
|
|
inputEl.dataset.key = 'referer';
|
|
|
|
labelEl.appendChild(inputEl);
|
|
|
|
|
|
|
|
for (var i = 0; i < referer_options.length; i++) {
|
|
|
|
var option = document.createElement("option");
|
|
|
|
option.value = referer_options[i];
|
|
|
|
option.text = referer_options[i];
|
|
|
|
inputEl.appendChild(option);
|
|
|
|
}
|
|
|
|
add_sitesEl.appendChild(labelEl);
|
|
|
|
|
2020-03-02 17:36:11 +00: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;
|
2021-04-29 17:35:59 +00:00
|
|
|
perm_origins = [];
|
2020-03-02 17:36:11 +00:00
|
|
|
for (var key in sites_custom) {
|
|
|
|
optionEl = document.createElement('option');
|
2020-08-20 17:01:12 +00:00
|
|
|
let domain = sites_custom[key]['domain'];
|
2021-04-29 17:35:59 +00:00
|
|
|
perm_origins.push('*://*.' + domain + '/*');
|
2021-02-17 18:11:27 +00:00
|
|
|
let isDefaultSite = defaultSites_domains.includes(domain);
|
2020-08-20 17:01:12 +00:00
|
|
|
optionEl.text = isDefaultSite ? '*' : '';
|
|
|
|
optionEl.text += key + ': ' + domain +
|
2020-06-04 07:04:58 +00:00
|
|
|
(sites_custom[key]['googlebot']>0 ? ' | googlebot' : '') +
|
2020-11-10 17:15:18 +00:00
|
|
|
(sites_custom[key]['allow_cookies']>0 ? ' | allow_cookies' : '') +
|
2020-06-04 07:04:58 +00:00
|
|
|
(sites_custom[key]['block_javascript']>0 ? ' | block javascript' : '') +
|
2021-02-06 20:37:52 +00:00
|
|
|
(sites_custom[key]['block_javascript_ext']>0 ? ' | block javascript ext' : '') +
|
|
|
|
(sites_custom[key]['referer'] ? ' | referer: ' + sites_custom[key]['referer'] : '');
|
2020-03-02 17:36:11 +00:00
|
|
|
optionEl.value = key;
|
|
|
|
selectEl.add(optionEl);
|
|
|
|
}
|
|
|
|
labelEl.appendChild(selectEl);
|
|
|
|
custom_sitesEl.appendChild(labelEl);
|
2021-04-29 17:35:59 +00:00
|
|
|
|
|
|
|
var perm_custom = document.getElementById('perm-custom');
|
|
|
|
ext_api.permissions.contains({
|
|
|
|
origins: perm_origins
|
|
|
|
}, function (result) {
|
|
|
|
if (result) {
|
|
|
|
perm_custom.innerText = 'YES';
|
|
|
|
} else {
|
|
|
|
perm_custom.innerText = 'NO';
|
|
|
|
}
|
|
|
|
});
|
2020-10-13 15:11:35 +00:00
|
|
|
});
|
2020-03-02 17:36:11 +00:00
|
|
|
|
2020-10-13 15:11:35 +00:00
|
|
|
var custom_enabled = document.getElementById('custom-enabled');
|
|
|
|
ext_api.permissions.contains({
|
|
|
|
origins: ["<all_urls>"]
|
|
|
|
}, function (result) {
|
|
|
|
if (result) {
|
|
|
|
custom_enabled.innerText = 'YES';
|
|
|
|
} else {
|
|
|
|
custom_enabled.innerText = 'NO';
|
|
|
|
}
|
2020-02-28 15:23:17 +00:00
|
|
|
});
|
2021-02-17 18:11:27 +00:00
|
|
|
}
|
2020-02-28 15:23:17 +00:00
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', renderOptions);
|
|
|
|
document.getElementById('save').addEventListener('click', save_options);
|
2020-04-11 17:40:12 +00:00
|
|
|
document.getElementById('sort').addEventListener('click', sort_options);
|
2020-03-08 18:45:19 +00:00
|
|
|
document.getElementById('export').addEventListener('click', export_options);
|
|
|
|
document.getElementById('import').onclick = function () {importInput.click()}
|
|
|
|
document.getElementById('importInput').addEventListener("change", import_options, false);
|
2020-03-02 17:36:11 +00:00
|
|
|
document.getElementById('add').addEventListener('click', add_options);
|
2020-08-18 18:19:15 +00:00
|
|
|
document.getElementById('delete').addEventListener('click', delete_options);
|
2021-04-29 17:35:59 +00:00
|
|
|
document.getElementById('edit').addEventListener('click', edit_options);
|
|
|
|
document.getElementById('perm_request').addEventListener('click', request_permissions);
|
|
|
|
document.getElementById('perm_remove').addEventListener('click', remove_permissions);
|