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

Only replace widgets that were actually blocked #2484

Merged
merged 2 commits into from
Apr 23, 2020

Conversation

ghostwords
Copy link
Member

Instead of hypothetically blocked (which doesn't account for page context, for instance).

Fixes pointless first-party replacement (for example).

Should follow #2267.

@ghostwords ghostwords added the widgets Click-to-activate placeholders for blocked but potentially useful social buttons/widgets label Oct 21, 2019
@ghostwords ghostwords force-pushed the only-replace-actually-blocked-widgets branch 2 times, most recently from 9e14a35 to 6adfcf5 Compare October 22, 2019 16:48
Instead of hypothetically blocked (which doesn't account
for page context, for instance).
@ghostwords ghostwords force-pushed the only-replace-actually-blocked-widgets branch from 6adfcf5 to 8cd0215 Compare October 22, 2019 17:13
As we only actually need domains.
@ghostwords
Copy link
Member Author

ghostwords commented Nov 1, 2019

Should adapt 94863c7 following #2438. Needs to be updated to accommodate new users of widgetList.

94863c7.patch
Move replacement widget data loading.

To the place it's used.
---
 src/js/background.js |  6 ------
 src/js/webrequest.js | 31 ++++++++++++++++++++++++-------
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/js/background.js b/src/js/background.js
index f79f64851..49dfe6187 100644
--- a/src/js/background.js
+++ b/src/js/background.js
@@ -27,7 +27,6 @@ var pbStorage = require("storage");
 var HeuristicBlocking = require("heuristicblocking");
 var FirefoxAndroid = require("firefoxandroid");
 var webrequest = require("webrequest");
-var widgetLoader = require("widgetloader");
 
 var Migrations = require("migrations").Migrations;
 var incognito = require("incognito");
@@ -50,11 +49,6 @@ function Badger(from_qunit) {
   self.webRTCAvailable = checkWebRTCBrowserSupport();
   self.firstPartyDomainPotentiallyRequired = testCookiesFirstPartyDomain();
 
-  self.widgetList = [];
-  widgetLoader.loadWidgetsFromFile("data/socialwidgets.json", (response) => {
-    self.widgetList = response;
-  });
-
   self.storage = new pbStorage.BadgerPen(async function (thisStorage) {
     self.initializeDefaultSettings();
     self.heuristicBlocking = new HeuristicBlocking.HeuristicBlocker(thisStorage);
diff --git a/src/js/webrequest.js b/src/js/webrequest.js
index 2099e92a6..d477b04c7 100644
--- a/src/js/webrequest.js
+++ b/src/js/webrequest.js
@@ -32,6 +32,7 @@ var constants = require("constants");
 var getSurrogateURI = require("surrogates").getSurrogateURI;
 var incognito = require("incognito");
 var utils = require("utils");
+var widgetLoader = require("widgetloader");
 
 /************ Local Variables *****************/
 let temporaryWidgetUnblock = {};
@@ -513,7 +514,17 @@ let getWidgetBlockList = (function () {
     { key: "allow_once" },
   ];
 
-  return function () {
+  // cached widget list
+  let widgetList;
+
+  // start loading the widget list from disk
+  let widgetListPromise = new Promise(resolve => {
+    widgetLoader.loadWidgetsFromFile("data/socialwidgets.json", response => {
+      resolve(response);
+    });
+  }).catch(console.error);
+
+  return async function () {
     // A mapping of individual SocialWidget objects to boolean values that determine
     // whether the content script should replace that tracker's button/widget
     var widgetsToReplace = {};
@@ -527,7 +538,9 @@ let getWidgetBlockList = (function () {
       }, {});
     }
 
-    badger.widgetList.forEach(function (widget) {
+    widgetList = await widgetListPromise;
+
+    widgetList.forEach(function (widget) {
       // Only replace blocked and yellowlisted widgets
       widgetsToReplace[widget.name] = constants.BLOCKED_ACTIONS.has(
         badger.storage.getBestAction(widget.domain)
@@ -536,7 +549,7 @@ let getWidgetBlockList = (function () {
 
     return {
       translations,
-      trackers: badger.widgetList,
+      trackers: widgetList,
       trackerButtonsToReplace: widgetsToReplace
     };
   };
@@ -612,8 +625,12 @@ function dispatcher(request, sender, sendResponse) {
 
   } else if (request.checkReplaceButton) {
     if (badger.isPrivacyBadgerEnabled(window.extractHostFromURL(sender.tab.url)) && badger.isWidgetReplacementEnabled()) {
-      let widgetBlockList = getWidgetBlockList();
-      sendResponse(widgetBlockList);
+      getWidgetBlockList().then(widgetBlockList => {
+        sendResponse(widgetBlockList);
+      }).catch(console.error);
+
+      // indicate this is an async response to chrome.runtime.onMessage
+      return true;
     }
 
   } else if (request.unblockWidget) {
@@ -760,7 +777,7 @@ function dispatcher(request, sender, sendResponse) {
         });
       }
     });
-    //indicate this is an async response to chrome.runtime.onMessage
+    // indicate this is an async response to chrome.runtime.onMessage
     return true;
 
   } else if (request.type == "uploadCloud") {
@@ -773,7 +790,7 @@ function dispatcher(request, sender, sendResponse) {
         sendResponse({success: true});
       }
     });
-    //indicate this is an async response to chrome.runtime.onMessage
+    // indicate this is an async response to chrome.runtime.onMessage
     return true;
 
   } else if (request.type == "savePopupToggle") {

@ghostwords ghostwords marked this pull request as ready for review April 23, 2020 16:53
@ghostwords ghostwords merged commit edd9f39 into master Apr 23, 2020
@ghostwords ghostwords deleted the only-replace-actually-blocked-widgets branch April 23, 2020 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
widgets Click-to-activate placeholders for blocked but potentially useful social buttons/widgets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant