Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disqus Comment Widget Replacement #2613

Merged
merged 10 commits into from
Jun 23, 2020
17 changes: 17 additions & 0 deletions src/data/socialwidgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
"type": 0
}
},
"Disqus": {
"domain": "*.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": [
Expand Down
13 changes: 12 additions & 1 deletion src/js/contentscripts/socialwidgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down
52 changes: 47 additions & 5 deletions src/js/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -603,10 +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 => {
// leading wildcard domain
if (domain[0] == "*") {
domain = domain.slice(1);
// 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;
}
Expand All @@ -615,6 +634,7 @@ let getWidgetList = (function () {
action == constants.BLOCK ||
action == constants.USER_BLOCK
);

});
if (replace) {
widgetsToReplace[widget.name] = true;
Expand Down Expand Up @@ -645,13 +665,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);
return frameData && frameData.host &&
exceptions.includes(frameData.host);
if (!frameData || !frameData.host) {
return false;
}

let frame_host = frameData.host;
for (let exception of exceptions) {
if (exception == frame_host) {
return true;
// leading wildcard
} else if (exception[0] == "*") {
if (frame_host.endsWith(exception.slice(1))) {
return true;
}
}
}

return false;
}

/**
Expand Down