From e6a7e7539fb21803889a0363a3a8b096a107bc54 Mon Sep 17 00:00:00 2001 From: ablanathtanalba Date: Wed, 20 May 2020 18:09:58 -0700 Subject: [PATCH 01/10] add disqus object to social widgets json --- src/data/socialwidgets.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/data/socialwidgets.json b/src/data/socialwidgets.json index cfeac5d734..847da1160f 100644 --- a/src/data/socialwidgets.json +++ b/src/data/socialwidgets.json @@ -26,6 +26,27 @@ "type": 0 } }, + "Disqus": { + "domains": [ + "disqus.com", + "*.disqus.com", + "www.disqus.com" + ], + "buttonSelectors": [ + "div#disqus_thread" + ], + "scriptSelectors": [ + "script[src*='.disqus.com/embed.js']" + ], + "replacementButton": { + "unblockDomains": [ + "/.*\\.disqus\\.com/", + "disqus.com" + ], + "imagePath": "badger-play.png", + "type": 4 + } + }, "Facebook Comments": { "domain": "www.facebook.com", "buttonSelectors": [ From f3b24115cba1bde3a85d4ba61467aeb4a9c30f41 Mon Sep 17 00:00:00 2001 From: ablanathtanalba Date: Thu, 28 May 2020 10:09:23 -0700 Subject: [PATCH 02/10] allow wildcard widget domain unblocking with regex --- src/js/webrequest.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index 9f44314003..f2a20230eb 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -650,6 +650,17 @@ function allowedOnTab(tab_id, request_host, frame_id) { } let frameData = badger.getFrameData(tab_id, frame_id); + + for (let exception of exceptions) { + if (exception.match(/[\/].*[\/]/)) { + let regex = exceptions.slice(1, -1); + + if (request_host.match(regex) || frameData.host.match(regex)) { + return true; + } + } + } + return frameData && frameData.host && exceptions.includes(frameData.host); } From 6fd2f33b05c54e6b69b265a11cfe269af7fcee0c Mon Sep 17 00:00:00 2001 From: ablanathtanalba Date: Tue, 2 Jun 2020 00:47:58 -0700 Subject: [PATCH 03/10] correct typo on regex assignment --- src/js/webrequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index f2a20230eb..068219d63c 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -653,7 +653,7 @@ function allowedOnTab(tab_id, request_host, frame_id) { for (let exception of exceptions) { if (exception.match(/[\/].*[\/]/)) { - let regex = exceptions.slice(1, -1); + let regex = exception.slice(1, -1); if (request_host.match(regex) || frameData.host.match(regex)) { return true; From 3e5fcb7dd8c513029e49493994f47feaae53c416 Mon Sep 17 00:00:00 2001 From: ablanathtanalba Date: Thu, 11 Jun 2020 10:14:15 -0700 Subject: [PATCH 04/10] add domain wildcard support for getWidgetList --- src/js/webrequest.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index 068219d63c..87a61ef90e 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -607,14 +607,28 @@ let getWidgetList = (function () { continue; } let replace = widget.domains.some(domain => { - if (!tabData.origins.hasOwnProperty(domain)) { - return false; + // checks for wildcard in domain + if (domain[0] == "*") { + // reduce wildcard domain from "*.domain" to "domain" + domain = domain.slice(2); + if (!tabData.origins.hasOwnProperty(domain)) { + return false; + } + const action = tabData.origins[domain]; + return ( + action == constants.BLOCK || + action == constants.USER_BLOCK + ); + } else { + if (!tabData.origins.hasOwnProperty(domain)) { + return false; + } + const action = tabData.origins[domain]; + return ( + action == constants.BLOCK || + action == constants.USER_BLOCK + ); } - const action = tabData.origins[domain]; - return ( - action == constants.BLOCK || - action == constants.USER_BLOCK - ); }); if (replace) { widgetsToReplace[widget.name] = true; @@ -652,7 +666,7 @@ function allowedOnTab(tab_id, request_host, frame_id) { let frameData = badger.getFrameData(tab_id, frame_id); for (let exception of exceptions) { - if (exception.match(/[\/].*[\/]/)) { + if (exception.match(/[/].*[/]/)) { let regex = exception.slice(1, -1); if (request_host.match(regex) || frameData.host.match(regex)) { From d995690b864f0993402d4e0a881edc2ceab69b57 Mon Sep 17 00:00:00 2001 From: ablanathtanalba Date: Thu, 11 Jun 2020 13:38:00 -0700 Subject: [PATCH 05/10] simplified some pattern matching in allowedOnTab method --- src/js/webrequest.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index 87a61ef90e..be07112bb2 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -666,10 +666,11 @@ function allowedOnTab(tab_id, request_host, frame_id) { let frameData = badger.getFrameData(tab_id, frame_id); for (let exception of exceptions) { - if (exception.match(/[/].*[/]/)) { - let regex = exception.slice(1, -1); + if (exception.startsWith('/') && exception.endsWith('/')) { + // remove the `/` character from head and tail of the domain string + exception = exception.slice(1, -1); - if (request_host.match(regex) || frameData.host.match(regex)) { + if (request_host.match(exception) || frameData.host.match(exception)) { return true; } } From 9246104a74bd8ac548ff5462ff1ae6a19158df61 Mon Sep 17 00:00:00 2001 From: ablanathtanalba Date: Tue, 16 Jun 2020 18:49:23 -0700 Subject: [PATCH 06/10] add wildcard to disqus json blob, add support for handling wildcard in getWidgetList --- src/js/webrequest.js | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index be07112bb2..06a8bb85a0 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -607,28 +607,30 @@ let getWidgetList = (function () { continue; } let replace = widget.domains.some(domain => { - // checks for wildcard in domain + // handles wildcard placeholders for subdomains like *.disqus.com if (domain[0] == "*") { - // reduce wildcard domain from "*.domain" to "domain" - domain = domain.slice(2); - if (!tabData.origins.hasOwnProperty(domain)) { - return false; + // reduce wildcard domain from "*.domain" to "domain" to easily compare in tabData + domain = domain.slice(1); + + // returns the actions for any tabData origins that match the wildcard subdomain + for (let origin in tabData.origins) { + if (origin.endsWith(domain)) { + let action = tabData.origins[origin]; + return ( + action == constants.BLOCK || + action == constants.USER_BLOCK + ); + } } - const action = tabData.origins[domain]; - return ( - action == constants.BLOCK || - action == constants.USER_BLOCK - ); - } else { - if (!tabData.origins.hasOwnProperty(domain)) { - return false; - } - const action = tabData.origins[domain]; - return ( - action == constants.BLOCK || - action == constants.USER_BLOCK - ); } + if (!tabData.origins.hasOwnProperty(domain)) { + return false; + } + const action = tabData.origins[domain]; + return ( + action == constants.BLOCK || + action == constants.USER_BLOCK + ); }); if (replace) { widgetsToReplace[widget.name] = true; From e91262b18701932d3b06d855d582a5c09cf799d4 Mon Sep 17 00:00:00 2001 From: Alexei Date: Mon, 22 Jun 2020 17:24:57 -0400 Subject: [PATCH 07/10] Remove unneeded/redundant Disqus widget domains --- src/data/socialwidgets.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/data/socialwidgets.json b/src/data/socialwidgets.json index 847da1160f..36a07b3bd1 100644 --- a/src/data/socialwidgets.json +++ b/src/data/socialwidgets.json @@ -27,11 +27,7 @@ } }, "Disqus": { - "domains": [ - "disqus.com", - "*.disqus.com", - "www.disqus.com" - ], + "domain": "*.disqus.com", "buttonSelectors": [ "div#disqus_thread" ], From 20deb05c5660e5aa87fe0ab6f1da490a40281c94 Mon Sep 17 00:00:00 2001 From: Alexei Date: Mon, 22 Jun 2020 17:27:08 -0400 Subject: [PATCH 08/10] Simplify wildcard allowed widget domains No need for regexes yet. --- src/data/socialwidgets.json | 2 +- src/js/webrequest.js | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/data/socialwidgets.json b/src/data/socialwidgets.json index 36a07b3bd1..648778d1af 100644 --- a/src/data/socialwidgets.json +++ b/src/data/socialwidgets.json @@ -36,7 +36,7 @@ ], "replacementButton": { "unblockDomains": [ - "/.*\\.disqus\\.com/", + "*.disqus.com", "disqus.com" ], "imagePath": "badger-play.png", diff --git a/src/js/webrequest.js b/src/js/webrequest.js index 06a8bb85a0..fa96413f83 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -661,25 +661,35 @@ function allowedOnTab(tab_id, request_host, frame_id) { let exceptions = tempAllowList[tab_id]; - if (exceptions.includes(request_host)) { - return true; + for (let exception of exceptions) { + if (exception == request_host) { + return true; + // leading wildcard + } else if (exception[0] == "*") { + if (request_host.endsWith(exception.slice(1))) { + return true; + } + } } let frameData = badger.getFrameData(tab_id, frame_id); + if (!frameData || !frameData.host) { + return false; + } + let frame_host = frameData.host; for (let exception of exceptions) { - if (exception.startsWith('/') && exception.endsWith('/')) { - // remove the `/` character from head and tail of the domain string - exception = exception.slice(1, -1); - - if (request_host.match(exception) || frameData.host.match(exception)) { + if (exception == frame_host) { + return true; + // leading wildcard + } else if (exception[0] == "*") { + if (frame_host.endsWith(exception.slice(1))) { return true; } } } - return frameData && frameData.host && - exceptions.includes(frameData.host); + return false; } /** From 26a43bc40f6c915b64420e2e611e3ace2a998d72 Mon Sep 17 00:00:00 2001 From: Alexei Date: Mon, 22 Jun 2020 17:36:56 -0400 Subject: [PATCH 09/10] Fix nits in replacement domain matching - If there are multiple wildcard domain matches, make sure all of them were blocked - If we have a wildcard domain that didn't match our criteria, don't keep going onto non-wildcard domain logic --- src/js/webrequest.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/js/webrequest.js b/src/js/webrequest.js index fa96413f83..7a3257c720 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -577,6 +577,7 @@ let getWidgetList = (function () { let widgetsToReplace = {}, widgetList = [], tabData = badger.tabData[tab_id], + tabOrigins = tabData && tabData.origins && Object.keys(tabData.origins), exceptions = badger.getSettings().getItem('widgetReplacementExceptions'); // optimize translation lookups by doing them just once, @@ -603,26 +604,28 @@ let getWidgetList = (function () { widgetList.push(widget); // replace only if at least one of the associated domains was blocked - if (!tabData) { + if (!tabOrigins || !tabOrigins.length) { continue; } let replace = widget.domains.some(domain => { - // handles wildcard placeholders for subdomains like *.disqus.com + // leading wildcard domain if (domain[0] == "*") { - // reduce wildcard domain from "*.domain" to "domain" to easily compare in tabData domain = domain.slice(1); - - // returns the actions for any tabData origins that match the wildcard subdomain - for (let origin in tabData.origins) { - if (origin.endsWith(domain)) { - let action = tabData.origins[origin]; - return ( - action == constants.BLOCK || - action == constants.USER_BLOCK - ); - } - } + // get all domains in tabData.origins that end with this domain + let matches = tabOrigins.filter(origin => { + return origin.endsWith(domain); + }); + // do we have any matches and are they all blocked? + return matches.length && matches.every(origin => { + const action = tabData.origins[origin]; + return ( + action == constants.BLOCK || + action == constants.USER_BLOCK + ); + }); } + + // regular, non-leading wildcard domain if (!tabData.origins.hasOwnProperty(domain)) { return false; } @@ -631,6 +634,7 @@ let getWidgetList = (function () { action == constants.BLOCK || action == constants.USER_BLOCK ); + }); if (replace) { widgetsToReplace[widget.name] = true; From 799e4c218a3b2950186525370b5ff4dca1697e78 Mon Sep 17 00:00:00 2001 From: Alexei Date: Tue, 23 Jun 2020 13:28:52 -0400 Subject: [PATCH 10/10] Fix wildcard domains for dynamic widget elements Domain matching for widget elements created after initial page load needed to be updated to support wildcards. --- src/js/contentscripts/socialwidgets.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/js/contentscripts/socialwidgets.js b/src/js/contentscripts/socialwidgets.js index 3f539f62a1..ac2d648fbb 100644 --- a/src/js/contentscripts/socialwidgets.js +++ b/src/js/contentscripts/socialwidgets.js @@ -338,7 +338,18 @@ function replaceSubsequentTrackerButtonsHelper(tracker_domain) { return; } widgetList.forEach(function (widget) { - if (widget.domains.includes(tracker_domain)) { + let replace = widget.domains.some(domain => { + if (domain == tracker_domain) { + return true; + // leading wildcard + } else if (domain[0] == "*") { + if (tracker_domain.endsWith(domain.slice(1))) { + return true; + } + } + return false; + }); + if (replace) { replaceIndividualButton(widget); } });