2019-03-13 12:20:24 +00:00
|
|
|
/*
|
2019-04-11 14:40:48 +00:00
|
|
|
* ClearURLs
|
2020-01-06 16:14:34 +00:00
|
|
|
* Copyright (c) 2017-2020 Kevin Röbert
|
2019-04-11 14:40:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-03-13 12:20:24 +00:00
|
|
|
|
2018-05-22 16:40:05 +00:00
|
|
|
var settings = [];
|
|
|
|
|
2020-06-05 22:02:03 +00:00
|
|
|
const pickr = Pickr.create({
|
|
|
|
el: '#badged-color-picker',
|
|
|
|
theme: 'nano',
|
|
|
|
components: {
|
|
|
|
preview: true,
|
|
|
|
opacity: true,
|
|
|
|
hue: true,
|
|
|
|
default: '#FFA500',
|
|
|
|
comparison: false,
|
|
|
|
interaction: {
|
|
|
|
hex: true,
|
|
|
|
rgba: false,
|
|
|
|
hsla: false,
|
|
|
|
hsva: false,
|
|
|
|
cmyk: false,
|
|
|
|
input: true,
|
|
|
|
clear: false,
|
|
|
|
save: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-05-22 16:40:05 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Load only when document is ready
|
|
|
|
*/
|
|
|
|
(function () {
|
|
|
|
pickr.on('init', () => {
|
|
|
|
getData();
|
|
|
|
setText();
|
|
|
|
document.getElementById('reset_settings_btn').onclick = reset;
|
|
|
|
document.getElementById('export_settings_btn').onclick = exportSettings;
|
|
|
|
document.getElementById('importSettings').onchange = importSettings;
|
|
|
|
document.getElementById('save_settings_btn').onclick = save;
|
2020-01-04 00:14:03 +00:00
|
|
|
});
|
2020-06-05 22:02:03 +00:00
|
|
|
})();
|
2018-05-22 16:40:05 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Reset everything.
|
|
|
|
* Set everthing to the default values.
|
|
|
|
*/
|
|
|
|
function reset() {
|
2019-02-13 17:02:08 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "initSettings",
|
|
|
|
params: []
|
|
|
|
}).then(handleResponse, handleError);
|
|
|
|
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "saveOnExit",
|
|
|
|
params: []
|
|
|
|
}).then(handleResponse, handleError);
|
|
|
|
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "reload",
|
|
|
|
params: []
|
|
|
|
}).then(handleResponse, handleError);
|
2018-05-22 16:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Saves the settings.
|
|
|
|
*/
|
|
|
|
function save() {
|
|
|
|
saveData("badged_color", pickr.getColor().toHEXA().toString())
|
|
|
|
.then(() => saveData("ruleURL", document.querySelector('input[name=ruleURL]').value))
|
|
|
|
.then(() => saveData("hashURL", document.querySelector('input[name=hashURL]').value))
|
|
|
|
.then(() => saveData("types", document.querySelector('input[name=types]').value))
|
|
|
|
.then(() => saveData("logLimit", Math.max(0, Math.min(5000, document.querySelector('input[name=logLimit]').value))))
|
2019-11-24 17:05:42 +00:00
|
|
|
.then(() => browser.runtime.sendMessage({
|
|
|
|
function: "setBadgedStatus",
|
|
|
|
params: []
|
|
|
|
}), handleError)
|
|
|
|
.then(() => browser.runtime.sendMessage({
|
|
|
|
function: "saveOnExit",
|
|
|
|
params: []
|
|
|
|
}), handleError)
|
|
|
|
.then(() => browser.runtime.sendMessage({
|
|
|
|
function: "reload",
|
|
|
|
params: []
|
|
|
|
}), handleError);
|
2018-05-22 16:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-22 23:16:12 +00:00
|
|
|
* Translate a string with the i18n API.
|
|
|
|
*
|
|
|
|
* @param {string} string Name of the attribute used for localization
|
|
|
|
* @param {string[]} placeholders Array of placeholders
|
2020-06-05 22:02:03 +00:00
|
|
|
*/
|
|
|
|
function translate(string, ...placeholders) {
|
2019-10-22 23:16:12 +00:00
|
|
|
return browser.i18n.getMessage(string, placeholders);
|
2018-05-22 16:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Get the data.
|
|
|
|
*/
|
|
|
|
function getData() {
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
|
|
|
params: ["badged_color"]
|
|
|
|
}).then(data => {
|
|
|
|
settings["badged_color"] = data.response;
|
|
|
|
pickr.setColor(data.response, false);
|
|
|
|
}).catch(handleError);
|
|
|
|
|
|
|
|
loadData("ruleURL")
|
2019-11-24 17:05:42 +00:00
|
|
|
.then(() => loadData("hashURL"))
|
|
|
|
.then(() => loadData("types"))
|
|
|
|
.then(() => loadData("logLimit"))
|
|
|
|
.then(logData => {
|
2020-06-05 22:02:03 +00:00
|
|
|
if (logData.response === undefined) {
|
|
|
|
document.getElementById('logLimit_label').textContent = translate('setting_log_limit_label', "0");
|
2019-11-24 17:05:42 +00:00
|
|
|
} else {
|
2020-06-05 22:02:03 +00:00
|
|
|
document.getElementById('logLimit_label').textContent = translate('setting_log_limit_label', logData.response);
|
2019-11-24 17:05:42 +00:00
|
|
|
}
|
2020-02-20 13:12:06 +00:00
|
|
|
}).catch(handleError);
|
2019-04-11 14:40:48 +00:00
|
|
|
|
2019-11-24 17:05:42 +00:00
|
|
|
loadData("contextMenuEnabled")
|
|
|
|
.then(() => loadData("historyListenerEnabled"))
|
|
|
|
.then(() => loadData("localHostsSkipping"))
|
|
|
|
.then(() => loadData("referralMarketing"))
|
|
|
|
.then(() => loadData("domainBlocking"))
|
2020-01-06 15:22:37 +00:00
|
|
|
.then(() => loadData("pingBlocking"))
|
2020-03-20 00:50:22 +00:00
|
|
|
.then(() => loadData("eTagFiltering"))
|
2019-11-24 17:05:42 +00:00
|
|
|
.then(() => {
|
|
|
|
changeSwitchButton("localHostsSkipping", "localHostsSkipping");
|
|
|
|
changeSwitchButton("historyListenerEnabled", "historyListenerEnabled");
|
|
|
|
changeSwitchButton("contextMenuEnabled", "contextMenuEnabled");
|
|
|
|
changeSwitchButton("referralMarketing", "referralMarketing");
|
|
|
|
changeSwitchButton("domainBlocking", "domainBlocking");
|
2020-01-06 15:22:37 +00:00
|
|
|
changeSwitchButton("pingBlocking", "pingBlocking");
|
2020-03-20 00:50:22 +00:00
|
|
|
changeSwitchButton("eTagFiltering", "eTagFiltering");
|
2020-02-20 13:12:06 +00:00
|
|
|
}).catch(handleError);
|
2019-11-24 17:05:42 +00:00
|
|
|
}
|
2019-10-22 23:16:12 +00:00
|
|
|
|
2019-11-24 17:05:42 +00:00
|
|
|
/**
|
|
|
|
* Loads data from storage and saves into local variable.
|
|
|
|
*
|
|
|
|
* @param name data/variable name
|
|
|
|
* @returns {Promise<data>} requested data
|
|
|
|
*/
|
|
|
|
async function loadData(name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-04-11 14:40:48 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "getData",
|
2019-11-24 17:05:42 +00:00
|
|
|
params: [name]
|
|
|
|
}).then(data => {
|
|
|
|
settings[name] = data.response;
|
2020-06-05 22:02:03 +00:00
|
|
|
if (document.querySelector('input[id=' + name + ']') == null) {
|
|
|
|
console.debug(name)
|
|
|
|
}
|
|
|
|
document.querySelector('input[id=' + name + ']').value = data.response;
|
2019-11-24 17:05:42 +00:00
|
|
|
resolve(data);
|
2019-04-11 14:40:48 +00:00
|
|
|
}, handleError);
|
2019-11-24 17:05:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves data to storage.
|
|
|
|
*
|
|
|
|
* @param key key of the data that should be saved
|
|
|
|
* @param data data that should be saved
|
|
|
|
* @returns {Promise<message>} message from background script
|
|
|
|
*/
|
|
|
|
async function saveData(key, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "setData",
|
|
|
|
params: [key, data]
|
|
|
|
}).then(message => {
|
|
|
|
handleResponse(message);
|
|
|
|
resolve(message);
|
|
|
|
}, handleError);
|
|
|
|
});
|
2018-05-22 16:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Set the text for the UI.
|
|
|
|
*/
|
|
|
|
function setText() {
|
2018-05-22 16:40:05 +00:00
|
|
|
document.title = translate('settings_html_page_title');
|
2020-06-05 22:02:03 +00:00
|
|
|
document.getElementById('page_title').textContent = translate('settings_html_page_title');
|
|
|
|
document.getElementById('badged_color_label').textContent = translate('badged_color_label');
|
|
|
|
document.getElementById('reset_settings_btn').textContent = translate('setting_html_reset_button');
|
|
|
|
document.getElementById('reset_settings_btn').setAttribute('title', translate('setting_html_reset_button_title'));
|
|
|
|
document.getElementById('rule_url_label').textContent = translate('setting_rule_url_label');
|
|
|
|
document.getElementById('hash_url_label').textContent = translate('setting_hash_url_label');
|
|
|
|
document.getElementById('types_label').innerHTML = translate('setting_types_label');
|
|
|
|
document.getElementById('save_settings_btn').textContent = translate('settings_html_save_button');
|
|
|
|
document.getElementById('save_settings_btn').setAttribute('title', translate('settings_html_save_button_title'));
|
2019-04-11 14:40:48 +00:00
|
|
|
injectText("context_menu_enabled", "context_menu_enabled");
|
2020-06-05 22:02:03 +00:00
|
|
|
document.getElementById('history_listener_enabled').innerHTML = translate('history_listener_enabled');
|
2019-09-11 16:08:41 +00:00
|
|
|
injectText("local_hosts_skipping", "local_hosts_skipping");
|
2020-06-05 22:02:03 +00:00
|
|
|
document.getElementById('export_settings_btn_text').textContent = translate('setting_html_export_button');
|
|
|
|
document.getElementById('export_settings_btn').setAttribute('title', translate('setting_html_export_button_title'));
|
|
|
|
document.getElementById('import_settings_btn_text').textContent = translate('setting_html_import_button');
|
|
|
|
document.getElementById('importSettings').setAttribute('title', translate('setting_html_import_button_title'));
|
2019-10-22 23:16:12 +00:00
|
|
|
injectText("referral_marketing_enabled", "referral_marketing_enabled");
|
2019-11-24 17:05:42 +00:00
|
|
|
injectText("domain_blocking_enabled", "domain_blocking_enabled");
|
2020-06-05 22:02:03 +00:00
|
|
|
document.getElementById('ping_blocking_enabled').innerHTML = translate('ping_blocking_enabled');
|
|
|
|
document.getElementById('ping_blocking_enabled').setAttribute('title', translate('ping_blocking_enabled_title'));
|
|
|
|
document.getElementById('eTag_filtering_enabled').innerHTML = translate('eTag_filtering_enabled');
|
|
|
|
document.getElementById('eTag_filtering_enabled').setAttribute('title', translate('eTag_filtering_enabled_title'));
|
2019-02-13 17:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-22 23:16:12 +00:00
|
|
|
* This function exports all ClearURLs settings with statistics and rules.
|
|
|
|
*/
|
|
|
|
function exportSettings() {
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "storageAsJSON",
|
|
|
|
params: []
|
|
|
|
}).then((data) => {
|
|
|
|
let blob = new Blob([JSON.stringify(data.response)], {type: 'application/json'});
|
|
|
|
|
|
|
|
browser.downloads.download({
|
|
|
|
'url': URL.createObjectURL(blob),
|
|
|
|
'filename': 'ClearURLs.conf',
|
|
|
|
'saveAs': true
|
2020-02-20 13:12:06 +00:00
|
|
|
}).catch(handleError);
|
|
|
|
}).catch(handleError);
|
2019-10-22 23:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function imports an exported ClearURLs setting and overwrites the old one.
|
|
|
|
*/
|
|
|
|
function importSettings(evt) {
|
|
|
|
let file = evt.target.files[0];
|
|
|
|
let fileReader = new FileReader();
|
|
|
|
|
2020-06-05 22:02:03 +00:00
|
|
|
fileReader.onload = function (e) {
|
2019-10-22 23:16:12 +00:00
|
|
|
let data = JSON.parse(e.target.result);
|
|
|
|
const length = Object.keys(data).length;
|
2020-06-05 22:02:03 +00:00
|
|
|
let i = 0;
|
2019-10-22 23:16:12 +00:00
|
|
|
|
|
|
|
Object.entries(data).forEach(([key, value]) => {
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "setData",
|
|
|
|
params: [key, value]
|
|
|
|
}).then(() => {
|
|
|
|
i++;
|
2020-06-05 22:02:03 +00:00
|
|
|
if (i === length) {
|
2019-10-22 23:16:12 +00:00
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
}, handleError);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
fileReader.readAsText(file);
|
|
|
|
}
|
|
|
|
|
2019-02-13 17:02:08 +00:00
|
|
|
function handleResponse(message) {
|
2019-04-11 14:40:48 +00:00
|
|
|
console.log(`Message from the background script: ${message.response}`);
|
2019-02-13 17:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(error) {
|
2019-04-11 14:40:48 +00:00
|
|
|
console.log(`Error: ${error}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Change the value of a switch button.
|
|
|
|
* @param {string} id HTML id
|
|
|
|
* @param {string} storageID storage internal id
|
|
|
|
*/
|
|
|
|
function changeSwitchButton(id, storageID) {
|
|
|
|
let element = document.getElementById(id);
|
2019-04-11 14:40:48 +00:00
|
|
|
|
2020-06-05 22:02:03 +00:00
|
|
|
element.onchange = function () {
|
2019-04-11 14:40:48 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "setData",
|
2020-06-05 22:02:03 +00:00
|
|
|
params: [storageID, element.checked]
|
|
|
|
}).then(() => {
|
|
|
|
if (storageID === "globalStatus") {
|
2019-04-11 14:40:48 +00:00
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "changeIcon",
|
|
|
|
params: []
|
2020-02-20 13:12:06 +00:00
|
|
|
}).catch(handleError);
|
2019-04-11 14:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
browser.runtime.sendMessage({
|
|
|
|
function: "saveOnExit",
|
|
|
|
params: []
|
2020-02-20 13:12:06 +00:00
|
|
|
}).catch(handleError);
|
|
|
|
}).catch(handleError);
|
2020-06-05 22:02:03 +00:00
|
|
|
};
|
2019-04-11 14:40:48 +00:00
|
|
|
setSwitchButton(id, storageID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Helper function to inject the translated text and tooltip.
|
|
|
|
*
|
|
|
|
* @param {string} id ID of the HTML element
|
|
|
|
* @param {string} attribute Name of the attribute used for localization
|
|
|
|
* @param {string} tooltip
|
|
|
|
*/
|
|
|
|
function injectText(id, attribute, tooltip = "") {
|
|
|
|
let object = document.getElementById(id);
|
|
|
|
object.textContent = translate(attribute);
|
2019-04-11 14:40:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This function will throw an error if no translation
|
|
|
|
is found for the tooltip. This is a planned error.
|
|
|
|
*/
|
2020-06-05 22:02:03 +00:00
|
|
|
tooltip = translate(attribute + "_title");
|
2019-04-11 14:40:48 +00:00
|
|
|
|
2020-06-05 22:02:03 +00:00
|
|
|
if (tooltip !== "") {
|
|
|
|
object.setAttribute('title', tooltip);
|
2019-04-11 14:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-05 22:02:03 +00:00
|
|
|
* Set the value of a switch button.
|
|
|
|
* @param {string} id HTML id
|
|
|
|
* @param {string} varname js internal variable name
|
|
|
|
*/
|
|
|
|
function setSwitchButton(id, varname) {
|
|
|
|
let element = document.getElementById(id);
|
|
|
|
element.checked = settings[varname];
|
2018-05-22 16:40:05 +00:00
|
|
|
}
|