Skip to content

Commit 6d9eedc

Browse files
committed
Merge pull request #2748
Organize popup into expandable sections. Two sections to start with: - Tracker list - Link tracking protection (shown only on websites where link tracking protection is active)
2 parents bdf7f24 + aa4fe37 commit 6d9eedc

File tree

9 files changed

+323
-58
lines changed

9 files changed

+323
-58
lines changed

src/_locales/en_US/messages.json

+16
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,22 @@
417417
"message": "user-controlled",
418418
"description": "Dropdown control setting on the Tracking Domains options page tab."
419419
},
420+
"popup_info_firstparty_protections": {
421+
"message": "Link tracking protection active",
422+
"description": "Clickable status header text in the popup. Shown when Privacy Badger's link tracking protection is active (for example, on facebook.com)."
423+
},
424+
"popup_info_firstparty_description": {
425+
"message": "Some websites $LINK_START$use link tracking to follow you$LINK_END$ whenever you click a link to leave the website. Privacy Badger removes tracking from such links on this website.",
426+
"description": "Text revealed by clicking on the link tracking protection header in the popup",
427+
"placeholders": {
428+
"link_start": {
429+
"content": "<a href='https://privacybadger.org/#What-about-tracking-by-the-sites-I-actively-visit%2c-like-NYTimes.com-or-Facebook.com' target='_blank'>"
430+
},
431+
"link_end": {
432+
"content": "</a>"
433+
}
434+
}
435+
},
420436
"popup_instructions": {
421437
"message": "$COUNT$ potential $LINK_START$trackers$LINK_END$ blocked",
422438
"description": "Popup message shown when at least one tracker was blocked.",

src/js/background.js

+1
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ Badger.prototype = {
815815
seenComic: false,
816816
sendDNTSignal: true,
817817
showCounter: true,
818+
showExpandedTrackingSection: false,
818819
showIntroPage: true,
819820
showNonTrackingDomains: false,
820821
showTrackingDomains: false,

src/js/popup.js

+95-21
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function showNagMaybe() {
127127
if (POPUP_DATA.showLearningPrompt) {
128128
_showLearningPrompt();
129129

130-
} else if (!POPUP_DATA.seenComic) {
130+
} else if (!POPUP_DATA.settings.seenComic) {
131131
chrome.tabs.query({active: true, currentWindow: true}, function (focusedTab) {
132132
// Show the popup instruction if the active tab is not firstRun.html page
133133
if (!focusedTab[0].url.startsWith(intro_page_url)) {
@@ -194,6 +194,43 @@ function init() {
194194
chrome.i18n.getMessage("version", chrome.runtime.getManifest().version)
195195
);
196196

197+
// add event listeners for click-to-expand blocked resources popup section
198+
$('#tracker-list-header').on('click', toggleBlockedResourcesHandler);
199+
200+
// add event listeners for click-to-expand first party protections popup section
201+
$('#firstparty-protections-header').on('click', toggleFirstPartyInfoHandler);
202+
203+
// show sliders when sliders were shown last
204+
// or when there is at least one breakage warning
205+
if (POPUP_DATA.settings.showExpandedTrackingSection || (
206+
POPUP_DATA.cookieblocked && Object.keys(POPUP_DATA.cookieblocked).some(
207+
d => POPUP_DATA.origins[d] == constants.USER_BLOCK)
208+
)) {
209+
$('#expand-blocked-resources').hide();
210+
$('#collapse-blocked-resources').show();
211+
$('#blockedResources').show();
212+
213+
} else {
214+
$('#expand-blocked-resources').show();
215+
$('#collapse-blocked-resources').hide();
216+
// show sliders regardless when the button
217+
// that lets you toggle slider visibility
218+
// isn't shown for whatever reason
219+
// (for ex.: "no trackers blocked" but we need to show
220+
// one or more "don't appear to be tracking you" sliders)
221+
if (!$('#tracker-list-header').is(':visible')) {
222+
$('#blockedResources').show();
223+
} else {
224+
$('#blockedResources').hide();
225+
}
226+
}
227+
228+
// show firstparty protections message if current tab is in our content scripts
229+
if (POPUP_DATA.enabled && POPUP_DATA.isOnFirstParty) {
230+
$("#firstparty-protections-container").show();
231+
$('#expand-firstparty-popup').show();
232+
}
233+
197234
// improve on Firefox's built-in options opening logic
198235
if (typeof browser == "object" && typeof browser.runtime.getBrowserInfo == "function") {
199236
browser.runtime.getBrowserInfo().then(function (info) {
@@ -436,6 +473,48 @@ function share() {
436473
$("#share_output").val(share_msg);
437474
}
438475

476+
/**
477+
* Click handlers for showing/hiding the blocked resources section
478+
*/
479+
function toggleBlockedResourcesHandler(e) {
480+
if (e.target.nodeName.toLowerCase() == 'a') {
481+
// don't toggle contents when clicking links in the header
482+
return;
483+
}
484+
if ($("#expand-blocked-resources").is(":visible")) {
485+
$("#collapse-blocked-resources").show();
486+
$("#expand-blocked-resources").hide();
487+
$("#blockedResources").slideDown();
488+
chrome.runtime.sendMessage({
489+
type: "updateSettings",
490+
data: { showExpandedTrackingSection: true }
491+
});
492+
} else {
493+
$("#collapse-blocked-resources").hide();
494+
$("#expand-blocked-resources").show();
495+
$("#blockedResources").slideUp();
496+
chrome.runtime.sendMessage({
497+
type: "updateSettings",
498+
data: { showExpandedTrackingSection: false }
499+
});
500+
}
501+
}
502+
503+
/**
504+
* Click handler for showing/hiding the firstparty popup info text
505+
*/
506+
function toggleFirstPartyInfoHandler() {
507+
if ($('#collapse-firstparty-popup').is(":visible")) {
508+
$("#collapse-firstparty-popup").hide();
509+
$("#expand-firstparty-popup").show();
510+
$("#instructions-firstparty-description").slideUp();
511+
} else {
512+
$("#collapse-firstparty-popup").show();
513+
$("#expand-firstparty-popup").hide();
514+
$("#instructions-firstparty-description").slideDown();
515+
}
516+
}
517+
439518
/**
440519
* Handler to undo user selection for a tracker
441520
*/
@@ -507,14 +586,10 @@ function refreshPopup() {
507586
}
508587

509588
if (!originsArr.length) {
510-
// hide the number of trackers and slider instructions message
511-
// if no sliders will be displayed
512-
$("#instructions-many-trackers").hide();
513-
514589
// show "no trackers" message
515590
$("#instructions-no-trackers").show();
516591

517-
if (POPUP_DATA.learnLocally && POPUP_DATA.showNonTrackingDomains) {
592+
if (POPUP_DATA.settings.learnLocally && POPUP_DATA.settings.showNonTrackingDomains) {
518593
// show the "no third party resources on this site" message
519594
$("#no-third-parties").show();
520595
}
@@ -527,7 +602,8 @@ function refreshPopup() {
527602
return;
528603
}
529604

530-
let printable = [];
605+
let printable = [],
606+
printableWarningSliders = [];
531607
let unblockedTrackers = [];
532608
let nonTracking = [];
533609
originsArr = htmlUtils.sortDomains(originsArr);
@@ -544,13 +620,19 @@ function refreshPopup() {
544620
action == constants.USER_BLOCK &&
545621
POPUP_DATA.cookieblocked.hasOwnProperty(origin)
546622
);
547-
printable.push(
548-
htmlUtils.getOriginHtml(origin, action, show_breakage_warning)
549-
);
623+
let slider_html = htmlUtils.getOriginHtml(origin, action, show_breakage_warning);
624+
if (show_breakage_warning) {
625+
printableWarningSliders.push(slider_html);
626+
} else {
627+
printable.push(slider_html);
628+
}
550629
}
551630
}
552631

553-
if (POPUP_DATA.learnLocally && unblockedTrackers.length) {
632+
// show breakage warning sliders at the top of the list
633+
printable = printableWarningSliders.concat(printable);
634+
635+
if (POPUP_DATA.settings.learnLocally && unblockedTrackers.length) {
554636
printable.push(
555637
'<div class="clicker tooltip" id="not-yet-blocked-header" title="' +
556638
chrome.i18n.getMessage("intro_not_an_adblocker_paragraph") +
@@ -563,12 +645,9 @@ function refreshPopup() {
563645
htmlUtils.getOriginHtml(domain, constants.ALLOW)
564646
);
565647
});
566-
567-
// reduce margin if we have hasn't-decided-yet-to-block domains to show
568-
$("#instructions-no-trackers").css("margin", "10px 0");
569648
}
570649

571-
if (POPUP_DATA.learnLocally && POPUP_DATA.showNonTrackingDomains && nonTracking.length) {
650+
if (POPUP_DATA.settings.learnLocally && POPUP_DATA.settings.showNonTrackingDomains && nonTracking.length) {
572651
printable.push(
573652
'<div class="clicker tooltip" id="non-trackers-header" title="' +
574653
chrome.i18n.getMessage("non_tracker_tip") +
@@ -581,9 +660,6 @@ function refreshPopup() {
581660
htmlUtils.getOriginHtml(nonTracking[i], constants.NO_TRACKING)
582661
);
583662
}
584-
585-
// reduce margin if we have non-tracking domains to show
586-
$("#instructions-no-trackers").css("margin", "10px 0");
587663
}
588664

589665
if (printable.length) {
@@ -595,13 +671,11 @@ function refreshPopup() {
595671
$('.tooltip').tooltipster();
596672

597673
if (POPUP_DATA.trackerCount === 0) {
598-
// hide multiple trackers message
599-
$("#instructions-many-trackers").hide();
600-
601674
// show "no trackers" message
602675
$("#instructions-no-trackers").show();
603676

604677
} else {
678+
$('#tracker-list-header').show();
605679
$('#instructions-many-trackers').html(chrome.i18n.getMessage(
606680
"popup_instructions", [
607681
POPUP_DATA.trackerCount,

src/js/utils.js

+46
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,51 @@ function isThirdPartyDomain(domain1, domain2) {
488488
return false;
489489
}
490490

491+
/**
492+
* Checks whether a given site hostname matches
493+
* any first party protections content scripts.
494+
*
495+
* @param {String} tab_host
496+
* @return {Boolean}
497+
*/
498+
let firstPartyProtectionsEnabled = (function () {
499+
let firstPartiesList;
500+
501+
function getFirstParties() {
502+
let manifestJson = chrome.runtime.getManifest();
503+
let firstParties = [];
504+
505+
for (let contentScriptObj of manifestJson.content_scripts) {
506+
// only include parts from content scripts that have firstparties entries
507+
if (contentScriptObj.js[0].includes("/firstparties/")) {
508+
let extractedUrls = [];
509+
for (let match of contentScriptObj.matches) {
510+
extractedUrls.push(window.extractHostFromURL(match));
511+
}
512+
firstParties.push(extractedUrls);
513+
}
514+
}
515+
return [].concat.apply([], firstParties);
516+
}
517+
518+
return function (tab_host) {
519+
if (!firstPartiesList) {
520+
firstPartiesList = getFirstParties();
521+
}
522+
523+
for (let url_pattern of firstPartiesList) {
524+
if (url_pattern.startsWith("*")) {
525+
if (tab_host.endsWith(url_pattern.slice(1))) {
526+
return true;
527+
}
528+
} else if (url_pattern == tab_host) {
529+
return true;
530+
}
531+
}
532+
return false;
533+
};
534+
})();
535+
491536
/**
492537
* Checks whether a given URL is a special browser page.
493538
* TODO account for browser-specific pages:
@@ -539,6 +584,7 @@ let exports = {
539584
estimateMaxEntropy,
540585
explodeSubdomains,
541586
findCommonSubstrings,
587+
firstPartyProtectionsEnabled,
542588
getHostFromDomainInput,
543589
invert,
544590
isRestrictedUrl,

src/js/webrequest.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ function dispatcher(request, sender, sendResponse) {
10311031
sendResponse({
10321032
criticalError: badger.criticalError,
10331033
noTabData: true,
1034-
seenComic: true,
1034+
settings: { seenComic: true },
10351035
});
10361036
break;
10371037
}
@@ -1052,12 +1052,11 @@ function dispatcher(request, sender, sendResponse) {
10521052
criticalError: badger.criticalError,
10531053
enabled: badger.isPrivacyBadgerEnabled(tab_host),
10541054
errorText: badger.tabData[tab_id].errorText,
1055-
learnLocally: badger.getSettings().getItem("learnLocally"),
1055+
isOnFirstParty: utils.firstPartyProtectionsEnabled(tab_host),
10561056
noTabData: false,
10571057
origins,
1058-
seenComic: badger.getSettings().getItem("seenComic"),
10591058
showLearningPrompt: badger.getPrivateSettings().getItem("showLearningPrompt"),
1060-
showNonTrackingDomains: badger.getSettings().getItem("showNonTrackingDomains"),
1059+
settings: badger.getSettings().getItemClones(),
10611060
tabHost: tab_host,
10621061
tabId: tab_id,
10631062
tabUrl: request.tabUrl,

0 commit comments

Comments
 (0)