From 858863ddb33e59979920a8cc3605d8e08b788b4b Mon Sep 17 00:00:00 2001 From: Banaanae <83927639+Banaanae@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:15:14 +1000 Subject: [PATCH 1/5] first commit for adding whitelist base functionality --- _locales/en/messages.json | 4 ++++ clearurls.js | 13 +++++++++++++ core_js/popup.js | 25 +++++++++++++++++++++++++ core_js/storage.js | 1 + html/popup.html | 1 + 5 files changed, 44 insertions(+) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 37393d5..2fe7ddb 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -87,6 +87,10 @@ "message": "Show numbers of cleaned urls", "description": "This string is used as title for the badges switch button on the popup page." }, + "popup_html_configs_whitelist_button": { + "message": "Whitelist Site", + "description": "This string is used as name for the whitelist button on the popup page." + }, "popup_html_statistics_head": { "message": "Statistics", "description": "This string is used as title for the statistics on the popup page." diff --git a/clearurls.js b/clearurls.js index 66a4aee..047869d 100644 --- a/clearurls.js +++ b/clearurls.js @@ -47,6 +47,19 @@ function removeFieldsFormURL(provider, pureUrl, quiet = false, request = null) { let rawRules = provider.getRawRules(); let urlObject = new URL(url); + /* + * Skip whitelisted sites + */ + for (const site of storage.whitelist) { + if (url.indexOf(site) != -1) { + return { + "changes": false, + "url": url, + "cancel": false + } + } + } + if (storage.localHostsSkipping && checkLocalURL(urlObject)) { return { "changes": false, diff --git a/core_js/popup.js b/core_js/popup.js index 1ebc0f3..62d02ee 100644 --- a/core_js/popup.js +++ b/core_js/popup.js @@ -155,6 +155,29 @@ function setSwitchButton(id, varname) element.checked = this[varname]; } +/** +* Adds the site the user is on to the whitelist +* Whitelisted sites do not get filtered +* @param {string} site Site url to add to whitelist +*/ +function addToWhitelist() { + let site; + browser.tabs.query({active: true, currentWindow: true}, function(tabs) { // Couldn't figure out how to access currentUrl var + site = tabs[0].url; // So this is used instead + }); + browser.runtime.sendMessage({ + function: "getData", + params: ['whitelist'] + }).then((data) => { + let domain = site.replace(/.*?:(?:\/\/)?(.*?\/).*/, '$1') + data.response.push(domain) + browser.runtime.sendMessage({ + function: "setData", + params: ['whitelist', data.response] + }).catch(handleError); + }).catch(handleError); +} + /** * Reset the global statistic */ @@ -191,6 +214,7 @@ function resetGlobalCounter(){ .then(() => loadData("getCurrentURL", "currentURL")) .then(() => { init(); + document.getElementById('whitelist_btn').onclick = addToWhitelist; document.getElementById('reset_counter_btn').onclick = resetGlobalCounter; changeSwitchButton("globalStatus", "globalStatus"); changeSwitchButton("tabcounter", "badgedStatus"); @@ -220,6 +244,7 @@ function setText() injectText('configs_switch_filter','popup_html_configs_switch_filter'); injectText('configs_head','popup_html_configs_head'); injectText('configs_switch_statistics','configs_switch_statistics'); + injectText('whitelist_btn','popup_html_configs_whitelist_button'); document.getElementById('donate').title = translate('donate_button'); } diff --git a/core_js/storage.js b/core_js/storage.js index ccfb980..f6e210c 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -216,6 +216,7 @@ function initSettings() { storage.badged_color = "#ffa500"; storage.hashURL = "https://rules2.clearurls.xyz/rules.minify.hash"; storage.ruleURL = "https://rules2.clearurls.xyz/data.minify.json"; + storage.whitelist = []; // TODO: If we do whitelist per rule, this needs to be obj storage.contextMenuEnabled = true; storage.historyListenerEnabled = true; storage.localHostsSkipping = true; diff --git a/html/popup.html b/html/popup.html index e0e3f1b..8246f4d 100644 --- a/html/popup.html +++ b/html/popup.html @@ -87,6 +87,7 @@

+ From 56ccb0c1c8e279aaabc10f64d5013c9ad03f352a Mon Sep 17 00:00:00 2001 From: Banaanae <83927639+Banaanae@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:58:36 +1000 Subject: [PATCH 2/5] allowing editing of whitelisted sites in settings just show data in a text input no fancy formatting needed! --- _locales/en/messages.json | 4 ++++ core_js/popup.js | 1 - core_js/settings.js | 3 +++ html/settings.html | 5 +++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 2fe7ddb..6fa6fea 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -183,6 +183,10 @@ "message": "The url to the rules.hash file (hash)", "description": "This string is used as name for the rule.hash url label." }, + "setting_whitelist_list_label": { + "message": "Whitelisted sites", + "description": "This string is used as name for the whitelisted sites list label." + }, "setting_types_label": { "message": "Request types (expert level)", "description": "This string is used as name for the types label." diff --git a/core_js/popup.js b/core_js/popup.js index 62d02ee..128b253 100644 --- a/core_js/popup.js +++ b/core_js/popup.js @@ -158,7 +158,6 @@ function setSwitchButton(id, varname) /** * Adds the site the user is on to the whitelist * Whitelisted sites do not get filtered -* @param {string} site Site url to add to whitelist */ function addToWhitelist() { let site; diff --git a/core_js/settings.js b/core_js/settings.js index 3e6162d..dbb2493 100644 --- a/core_js/settings.js +++ b/core_js/settings.js @@ -82,6 +82,7 @@ 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("whitelist", document.querySelector('input[name=whitelist]').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)))) .then(() => browser.runtime.sendMessage({ @@ -122,6 +123,7 @@ function getData() { loadData("ruleURL") .then(() => loadData("hashURL")) + .then(() => loadData("whitelist")) .then(() => loadData("types")) .then(() => loadData("logLimit")) .then(logData => { @@ -216,6 +218,7 @@ function setText() { 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('whitelist_list_label').textContent = translate('setting_whitelist_list_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')); diff --git a/html/settings.html b/html/settings.html index f28ded9..055f403 100644 --- a/html/settings.html +++ b/html/settings.html @@ -105,6 +105,11 @@


+

+
+ +

+


From af9cbe52ae31a05ef6619065737c77985c61c9f2 Mon Sep 17 00:00:00 2001 From: Banaanae <83927639+Banaanae@users.noreply.github.com> Date: Tue, 25 Jun 2024 18:54:19 +1000 Subject: [PATCH 3/5] handle url better; fix saving when edited ... via settings --- core_js/popup.js | 3 ++- core_js/settings.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core_js/popup.js b/core_js/popup.js index 128b253..561d95b 100644 --- a/core_js/popup.js +++ b/core_js/popup.js @@ -168,7 +168,8 @@ function addToWhitelist() { function: "getData", params: ['whitelist'] }).then((data) => { - let domain = site.replace(/.*?:(?:\/\/)?(.*?\/).*/, '$1') + let siteUrl = new URL(site) + let domain = siteUrl.hostname data.response.push(domain) browser.runtime.sendMessage({ function: "setData", diff --git a/core_js/settings.js b/core_js/settings.js index dbb2493..bf97b84 100644 --- a/core_js/settings.js +++ b/core_js/settings.js @@ -82,7 +82,7 @@ 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("whitelist", document.querySelector('input[name=whitelist]').value)) + .then(() => saveData("whitelist", document.querySelector('input[name=whitelist]').value.split(','))) .then(() => saveData("types", document.querySelector('input[name=types]').value)) .then(() => saveData("logLimit", Math.max(0, Math.min(5000, document.querySelector('input[name=logLimit]').value)))) .then(() => browser.runtime.sendMessage({ From 19d3448a1e23f628736df90cdd38ba80059b4424 Mon Sep 17 00:00:00 2001 From: Banaanae <83927639+Banaanae@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:09:14 +1000 Subject: [PATCH 4/5] fix styling; allow removing of sites still needs to update after press --- _locales/en/messages.json | 6 ++++- core_js/popup.js | 54 +++++++++++++++++++++++++++++++++++---- html/popup.html | 4 ++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 6fa6fea..ebb949f 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -87,10 +87,14 @@ "message": "Show numbers of cleaned urls", "description": "This string is used as title for the badges switch button on the popup page." }, - "popup_html_configs_whitelist_button": { + "popup_html_configs_whitelist_button_add": { "message": "Whitelist Site", "description": "This string is used as name for the whitelist button on the popup page." }, + "popup_html_configs_whitelist_button_remove": { + "message": "Remove from Whitelist", + "description": "This string is used as name for the whitelist button on the popup page." + }, "popup_html_statistics_head": { "message": "Statistics", "description": "This string is used as title for the statistics on the popup page." diff --git a/core_js/popup.js b/core_js/popup.js index 561d95b..7fa904c 100644 --- a/core_js/popup.js +++ b/core_js/popup.js @@ -62,6 +62,40 @@ function changeStatistics() elTotal.textContent = totalCounter.toLocaleString(); } +/** +* Set the whitelist button text +*/ +function setWhitelistText() +{ + let element = document.getElementById('whitelist_btn'); + let currentSite; + browser.tabs.query({active: true, currentWindow: true}, function(tabs) { + currentSite = tabs[0].url; + }); + + browser.runtime.sendMessage({ + function: "getData", + params: ['whitelist'] + }).then((data) => { + data.response.forEach(site => { + console.log(currentSite.indexOf(site)) + if (currentSite.indexOf(site) == -1) { + element.textContent = translate('popup_html_configs_whitelist_button_add') + document.getElementById('whitelist_btn').onclick = changeWhitelist; + element.classList.replace('btn-danger', 'btn-primary') + } else { + element.textContent = translate('popup_html_configs_whitelist_button_remove') + document.getElementById('whitelist_btn').onclick = () => {changeWhitelist(true)}; + element.classList.replace('btn-primary', 'btn-danger') + } + }); + if (data.response.length == 0) { + element.textContent = translate('popup_html_configs_whitelist_button_add') + document.getElementById('whitelist_btn').onclick = changeWhitelist; + } + }).catch(handleError); +} + /** * Set the value for the hashStatus on startUp. */ @@ -156,10 +190,15 @@ function setSwitchButton(id, varname) } /** -* Adds the site the user is on to the whitelist +* Adds (or removes) the site the user is on to the whitelist * Whitelisted sites do not get filtered +* @param {boolean} remove If true remove current site instead of adding */ -function addToWhitelist() { +function changeWhitelist(removeWl = false) { + if (removeWl != true) { // Handle click obj + removeWl = false + } + console.log('call') let site; browser.tabs.query({active: true, currentWindow: true}, function(tabs) { // Couldn't figure out how to access currentUrl var site = tabs[0].url; // So this is used instead @@ -170,12 +209,18 @@ function addToWhitelist() { }).then((data) => { let siteUrl = new URL(site) let domain = siteUrl.hostname - data.response.push(domain) + console.log(removeWl) + if (removeWl == false) { + data.response.push(domain) + } else { + data.response = data.response.filter(wlSite => wlSite != domain) + } browser.runtime.sendMessage({ function: "setData", params: ['whitelist', data.response] }).catch(handleError); }).catch(handleError); + setWhitelistText(); } /** @@ -214,7 +259,6 @@ function resetGlobalCounter(){ .then(() => loadData("getCurrentURL", "currentURL")) .then(() => { init(); - document.getElementById('whitelist_btn').onclick = addToWhitelist; document.getElementById('reset_counter_btn').onclick = resetGlobalCounter; changeSwitchButton("globalStatus", "globalStatus"); changeSwitchButton("tabcounter", "badgedStatus"); @@ -244,7 +288,7 @@ function setText() injectText('configs_switch_filter','popup_html_configs_switch_filter'); injectText('configs_head','popup_html_configs_head'); injectText('configs_switch_statistics','configs_switch_statistics'); - injectText('whitelist_btn','popup_html_configs_whitelist_button'); + setWhitelistText(); document.getElementById('donate').title = translate('donate_button'); } diff --git a/html/popup.html b/html/popup.html index 8246f4d..1d4d044 100644 --- a/html/popup.html +++ b/html/popup.html @@ -86,8 +86,10 @@

+
+ +

- From 5dbb1166513c003d106824f62b0fbea29cff3e58 Mon Sep 17 00:00:00 2001 From: Banaanae <83927639+Banaanae@users.noreply.github.com> Date: Thu, 27 Jun 2024 09:41:57 +1000 Subject: [PATCH 5/5] Fix updating whitelist button - Button now reflects whitelist status instantly - Fixed bug where only last site whitelisted displayed properly - No longer breaks when spammed - Removed test code --- core_js/popup.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/core_js/popup.js b/core_js/popup.js index 7fa904c..c669031 100644 --- a/core_js/popup.js +++ b/core_js/popup.js @@ -68,7 +68,7 @@ function changeStatistics() function setWhitelistText() { let element = document.getElementById('whitelist_btn'); - let currentSite; + let currentSite, siteFound; browser.tabs.query({active: true, currentWindow: true}, function(tabs) { currentSite = tabs[0].url; }); @@ -78,20 +78,20 @@ function setWhitelistText() params: ['whitelist'] }).then((data) => { data.response.forEach(site => { - console.log(currentSite.indexOf(site)) - if (currentSite.indexOf(site) == -1) { - element.textContent = translate('popup_html_configs_whitelist_button_add') - document.getElementById('whitelist_btn').onclick = changeWhitelist; - element.classList.replace('btn-danger', 'btn-primary') - } else { - element.textContent = translate('popup_html_configs_whitelist_button_remove') - document.getElementById('whitelist_btn').onclick = () => {changeWhitelist(true)}; - element.classList.replace('btn-primary', 'btn-danger') + if (currentSite.indexOf(site) != -1) { + siteFound = true } }); - if (data.response.length == 0) { + if (!siteFound) { + if (data.response.length != 0) { + element.classList.replace('btn-danger', 'btn-primary') + } element.textContent = translate('popup_html_configs_whitelist_button_add') document.getElementById('whitelist_btn').onclick = changeWhitelist; + } else { + element.classList.replace('btn-primary', 'btn-danger') + element.textContent = translate('popup_html_configs_whitelist_button_remove') + document.getElementById('whitelist_btn').onclick = () => {changeWhitelist(true)}; } }).catch(handleError); } @@ -198,7 +198,6 @@ function changeWhitelist(removeWl = false) { if (removeWl != true) { // Handle click obj removeWl = false } - console.log('call') let site; browser.tabs.query({active: true, currentWindow: true}, function(tabs) { // Couldn't figure out how to access currentUrl var site = tabs[0].url; // So this is used instead @@ -209,7 +208,6 @@ function changeWhitelist(removeWl = false) { }).then((data) => { let siteUrl = new URL(site) let domain = siteUrl.hostname - console.log(removeWl) if (removeWl == false) { data.response.push(domain) } else { @@ -218,9 +216,10 @@ function changeWhitelist(removeWl = false) { browser.runtime.sendMessage({ function: "setData", params: ['whitelist', data.response] + }).then(() => { + setWhitelistText(); }).catch(handleError); }).catch(handleError); - setWhitelistText(); } /**