From 124514c44e094281ebc58a6ff6334a20a82b7912 Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Thu, 19 Sep 2024 20:06:46 +0530 Subject: [PATCH 1/3] add exclusion setting for domains ( related issue - https://github.com/ClearURLs/Addon/issues/353 ) --- core_js/pureCleaning.js | 22 ++++++++++++++++++++++ core_js/settings.js | 12 ++++++++++-- core_js/storage.js | 5 +++++ html/settings.html | 16 +++++++++++++++- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/core_js/pureCleaning.js b/core_js/pureCleaning.js index d26a6a2..666b680 100644 --- a/core_js/pureCleaning.js +++ b/core_js/pureCleaning.js @@ -26,9 +26,31 @@ * @return {String} cleaned URL */ function pureCleaning(url, quiet = false) { + let exclude_domains = storage.excludeDomains.split('\n'); let before = url; let after = url; + if (exclude_domains.length > 0) { + let hostname = new URL(url).hostname; + for (let i = 0; i < exclude_domains.length; i++) { + let domain = exclude_domains[i]; + + // if regex + if (domain.startsWith('^') && domain.endsWith('$') && new RegExp(domain.slice(1, -1)).test(hostname)) { + return url; + } + // if exclude with subdomain + else if (domain.startsWith('*') && hostname.endsWith(domain.slice(1))) { + return url; + } + // if exclude exact domain + else if (hostname.split('.').length === 2 && hostname === domain) { + return url; + } + } + } + + do { before = after; after = _cleaning(before, quiet); diff --git a/core_js/settings.js b/core_js/settings.js index 3e6162d..cf066b2 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("excludeDomains", document.querySelector('textarea[name=excludeDomains]').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("excludeDomains")) .then(() => loadData("types")) .then(() => loadData("logLimit")) .then(logData => { @@ -177,10 +179,16 @@ async function loadData(name) { params: [name] }).then(data => { settings[name] = data.response; - if (document.querySelector('input[id=' + name + ']') == null) { + if(document.querySelector('textarea[id=' + name + ']')) { + document.querySelector('textarea[id=' + name + ']').value = data.response; + } + else if (document.querySelector('input[id=' + name + ']') == null) { console.debug(name) } - document.querySelector('input[id=' + name + ']').value = data.response; + else{ + document.querySelector('input[id=' + name + ']').value = data.response; + } + resolve(data); }, handleError); }); diff --git a/core_js/storage.js b/core_js/storage.js index ccfb980..fb6630a 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -160,6 +160,10 @@ function setData(key, value) { case "ruleURL": storage[key] = replaceOldURLs(value); break; + case "excludeDomains": + console.log(`excludeDomains: '${value}'`); + storage[key] = value; + break; case "types": storage[key] = value.split(','); break; @@ -225,6 +229,7 @@ function initSettings() { storage.pingBlocking = true; storage.eTagFiltering = false; storage.watchDogErrorCount = 0; + storage.excludeDomains = ""; if (getBrowser() === "Firefox") { storage.types = ["font", "image", "imageset", "main_frame", "media", "object", "object_subrequest", "other", "script", "stylesheet", "sub_frame", "websocket", "xml_dtd", "xmlhttprequest", "xslt"]; diff --git a/html/settings.html b/html/settings.html index f28ded9..5b509e5 100644 --- a/html/settings.html +++ b/html/settings.html @@ -104,7 +104,21 @@

-
+

+
+ +

+ Note : it will be applied on hostname which includes domain and subdomains. nothing after / will be filtered.
+ Please specify domains you want to exclude from filtering:
+ - Use a simple domain (e.g., google.com) for exact matches.
+ - Use a wildcard for subdomains (e.g., *.google.com) to exclude all subdomains.
+ - Use regex for advanced matching (e.g., ^.*google\.com$ to match anything ending with google.com or google.co using ? as a wildcard).
+ - For more about regex, visit regexone.com, and test your regex at regex101.com or regexr.com.
+ - Each entry should be on a new line. +
+

+
+


From d559056d2162ad716a81ae6bba3bed13cac4a824 Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Fri, 4 Oct 2024 17:08:32 +0530 Subject: [PATCH 2/3] Fix mistake in regex domain exclude explanation --- html/settings.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/settings.html b/html/settings.html index 5b509e5..91d957d 100644 --- a/html/settings.html +++ b/html/settings.html @@ -112,7 +112,7 @@ Please specify domains you want to exclude from filtering:
- Use a simple domain (e.g., google.com) for exact matches.
- Use a wildcard for subdomains (e.g., *.google.com) to exclude all subdomains.
- - Use regex for advanced matching (e.g., ^.*google\.com$ to match anything ending with google.com or google.co using ? as a wildcard).
+ - Use regex for advanced matching (e.g., ^.*google\.com?$ to match anything ending with google.com or google.co using ? as a wildcard).
- For more about regex, visit regexone.com, and test your regex at regex101.com or regexr.com.
- Each entry should be on a new line. From 4e314242b0dffd91d1140b58b7e9c97a7348c092 Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:01:27 +0000 Subject: [PATCH 3/3] fix: 5 issues reported by sonarcloud (https://github.com/ClearURLs/Addon/pull/383#issuecomment-2402006872) 1. using variable to store boolean and if any boolean is true, then return url 2. using for-of for list 3. we are using "storage" variable to store dictionary data not array, so made it "{}" from "[]" 4. "value" parameter in setData was string when case was "excludeDomains", still removed its console.log (not needed to console log it now so) --- core_js/pureCleaning.js | 19 ++++++------------- core_js/storage.js | 9 ++++----- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/core_js/pureCleaning.js b/core_js/pureCleaning.js index 666b680..6c81485 100644 --- a/core_js/pureCleaning.js +++ b/core_js/pureCleaning.js @@ -32,20 +32,13 @@ function pureCleaning(url, quiet = false) { if (exclude_domains.length > 0) { let hostname = new URL(url).hostname; - for (let i = 0; i < exclude_domains.length; i++) { - let domain = exclude_domains[i]; + for (const domain of exclude_domains) { + let isRegexMatch = domain.startsWith('^') && domain.endsWith('$') && new RegExp(domain.slice(1, -1)).test(hostname) + let isSubdomainExcludeMatch = domain.startsWith('*') && hostname.endsWith(domain.slice(1)) + let isExactDomainExcludeMatch = hostname.split('.').length === 2 && hostname === domain - // if regex - if (domain.startsWith('^') && domain.endsWith('$') && new RegExp(domain.slice(1, -1)).test(hostname)) { - return url; - } - // if exclude with subdomain - else if (domain.startsWith('*') && hostname.endsWith(domain.slice(1))) { - return url; - } - // if exclude exact domain - else if (hostname.split('.').length === 2 && hostname === domain) { - return url; + if (isRegexMatch || isSubdomainExcludeMatch || isExactDomainExcludeMatch) { + return url } } } diff --git a/core_js/storage.js b/core_js/storage.js index fb6630a..72f88c3 100644 --- a/core_js/storage.js +++ b/core_js/storage.js @@ -20,7 +20,7 @@ /* * This script is responsible for the storage. */ -var storage = []; +var storage = {}; var hasPendingSaves = false; var pendingSaves = new Set(); @@ -77,7 +77,7 @@ function deleteFromDisk(key) { function saveOnDisk(keys) { let json = {}; - keys.forEach(function (key) { + keys.forEach(function(key) { json[key] = storageDataAsString(key); }); @@ -95,7 +95,7 @@ function deferSaveOnDisk(key) { return; } - setTimeout(function () { + setTimeout(function() { saveOnDisk(Array.from(pendingSaves)); pendingSaves.clear(); hasPendingSaves = false; @@ -161,7 +161,6 @@ function setData(key, value) { storage[key] = replaceOldURLs(value); break; case "excludeDomains": - console.log(`excludeDomains: '${value}'`); storage[key] = value; break; case "types": @@ -215,7 +214,7 @@ function initSettings() { storage.cleanedCounter = 0; storage.hashStatus = "error"; storage.loggingStatus = false; - storage.log = {"log": []}; + storage.log = { "log": [] }; storage.statisticsStatus = true; storage.badged_color = "#ffa500"; storage.hashURL = "https://rules2.clearurls.xyz/rules.minify.hash";