From 027fcfb18cd50974b24a25cc38b4bf85fe60684e Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Thu, 3 Dec 2020 14:45:51 +0100 Subject: [PATCH 1/4] Expose logged-in state to ghosterysearch page via content-script. --- src/background.js | 28 ++------------------ src/content/is-ghostery-browser.js | 5 ++++ src/manifest.json | 10 ++++++-- src/status.js | 41 ++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 src/content/is-ghostery-browser.js create mode 100644 src/status.js diff --git a/src/background.js b/src/background.js index f091105..1d63641 100644 --- a/src/background.js +++ b/src/background.js @@ -9,6 +9,7 @@ class AccessToken { } AccessToken.TOKEN = value; tokenPool.generateTokens(); + injectLoggedInStatus(!!AccessToken.TOKEN); } static get() { @@ -31,6 +32,7 @@ class AccessToken { static destroy() { console.warn("ACCESS_TOKEN removed") AccessToken.TOKEN = null; + injectLoggedInStatus(false); } static async refresh() { @@ -113,32 +115,6 @@ async function start() { requestHeaders, }; }, { urls: [`${SERP_BASE_URL}/search*`]}, ["blocking", "requestHeaders"]); - - browser.webRequest.onBeforeSendHeaders.addListener((details) => { - const { requestHeaders } = details; - - requestHeaders.push({ - name: "X-Ghostery-Browser", - value: "true", - }); - - requestHeaders.push({ - name: "X-Ghostery-Login", - value: String(!!AccessToken.TOKEN), - }); - - if (AccessToken.TOKEN) { - const scopes = AccessToken.parse().scopes || []; - requestHeaders.push({ - name: "X-Ghostery-Scopes", - value: scopes.join(','), - }); - } - - return { - requestHeaders, - }; - }, { urls: [`${SERP_BASE_URL}/*`]}, ["blocking", "requestHeaders"]); } browser.runtime.onMessage.addListener(async ({ action, args }, { tab }) => { diff --git a/src/content/is-ghostery-browser.js b/src/content/is-ghostery-browser.js new file mode 100644 index 0000000..0526f35 --- /dev/null +++ b/src/content/is-ghostery-browser.js @@ -0,0 +1,5 @@ +"use strict"; + +(async function () { + document.querySelector('html').classList.add('ghostery-browser'); +})() diff --git a/src/manifest.json b/src/manifest.json index ffa933d..2c1480a 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -13,7 +13,8 @@ "background.js", "choice-screen.js", "metrics.js", - "api.js" + "api.js", + "status.js" ] }, "content_scripts": [{ @@ -21,7 +22,11 @@ "https://*.ghosterysearch.com/", "http://localhost/*" ], - "js": ["content/top-sites.js", "content/search-bar.js"] + "js": [ + "content/top-sites.js", + "content/search-bar.js", + "content/is-ghostery-browser.js" + ] }, { "matches": [ "https://*.ghosterysearch.com/search*", @@ -38,6 +43,7 @@ "webRequest", "webRequestBlocking", "topSites", + "tabs", "https://www.ghostery.com/*", "https://consumerapi.ghostery.com/*", "https://www.ghosterystage.com/*", diff --git a/src/status.js b/src/status.js new file mode 100644 index 0000000..ebf1318 --- /dev/null +++ b/src/status.js @@ -0,0 +1,41 @@ +let loggedIn = false; +let statusContentScript = Promise.resolve(); + +const loggedInScript = ` +document.querySelector('html').classList.remove('logged-out'); +document.querySelector('html').classList.add('logged-in'); +`; +const loggedOutScript = ` +document.querySelector('html').classList.remove('logged-in'); +document.querySelector('html').classList.add('logged-out'); +`; + +function injectLoggedInStatus(status) { + statusContentScript = statusContentScript.then(async () => { + console.log("set logged in", status); + if (status === loggedIn) { + return; + } + loggedIn = status; + // register a content script to inject status classes in the future + await browser.contentScripts.register({ + allFrames: false, + js: [ + { + code: status ? loggedInScript : loggedOutScript, + }, + ], + matches: [`${SERP_BASE_URL}/*`], + runAt: "document_start", + }); + // trigger the log in state change in existing browser tabs + const activeTabs = await browser.tabs.query({ url: `${SERP_BASE_URL}/*` }); + activeTabs.forEach(({ id }) => { + browser.tabs.executeScript(id, { + code: status ? loggedInScript : loggedOutScript, + }); + }); + }); +} + +injectLoggedInStatus(false); From c1f8dfda0ee9f0e7f0f16a99b384d851a6b3ce84 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Thu, 3 Dec 2020 17:38:01 +0100 Subject: [PATCH 2/4] Remove logging --- src/status.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/status.js b/src/status.js index ebf1318..d134131 100644 --- a/src/status.js +++ b/src/status.js @@ -12,7 +12,6 @@ document.querySelector('html').classList.add('logged-out'); function injectLoggedInStatus(status) { statusContentScript = statusContentScript.then(async () => { - console.log("set logged in", status); if (status === loggedIn) { return; } From c0ca2463b392c88cc998ac17bd7aa8907fe14b2f Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 4 Dec 2020 10:09:38 +0100 Subject: [PATCH 3/4] Remove async from is-ghostery-browser function. --- src/content/is-ghostery-browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/is-ghostery-browser.js b/src/content/is-ghostery-browser.js index 0526f35..a37d0d1 100644 --- a/src/content/is-ghostery-browser.js +++ b/src/content/is-ghostery-browser.js @@ -1,5 +1,5 @@ "use strict"; -(async function () { +(function () { document.querySelector('html').classList.add('ghostery-browser'); })() From 756d9d649e3021401026c52c055b1686892518e2 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 4 Dec 2020 10:11:28 +0100 Subject: [PATCH 4/4] Unregister old content-script before registering the new one --- src/status.js | 55 +++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/status.js b/src/status.js index d134131..0dc3c80 100644 --- a/src/status.js +++ b/src/status.js @@ -11,30 +11,37 @@ document.querySelector('html').classList.add('logged-out'); `; function injectLoggedInStatus(status) { - statusContentScript = statusContentScript.then(async () => { - if (status === loggedIn) { - return; - } - loggedIn = status; - // register a content script to inject status classes in the future - await browser.contentScripts.register({ - allFrames: false, - js: [ - { + statusContentScript = statusContentScript.then( + async (previousRegistration) => { + if (status === loggedIn) { + return; + } + // unregister previous content script + if (previousRegistration && previousRegistration.unregister) { + previousRegistration.unregister(); + } + loggedIn = status; + // register a content script to inject status classes in the future + const registration = await browser.contentScripts.register({ + allFrames: false, + js: [ + { + code: status ? loggedInScript : loggedOutScript, + }, + ], + matches: [`${SERP_BASE_URL}/*`], + runAt: "document_start", + }); + // trigger the log in state change in existing browser tabs + const activeTabs = await browser.tabs.query({ + url: `${SERP_BASE_URL}/*`, + }); + activeTabs.forEach(({ id }) => { + browser.tabs.executeScript(id, { code: status ? loggedInScript : loggedOutScript, - }, - ], - matches: [`${SERP_BASE_URL}/*`], - runAt: "document_start", - }); - // trigger the log in state change in existing browser tabs - const activeTabs = await browser.tabs.query({ url: `${SERP_BASE_URL}/*` }); - activeTabs.forEach(({ id }) => { - browser.tabs.executeScript(id, { - code: status ? loggedInScript : loggedOutScript, + }); }); - }); - }); + return registration; + } + ); } - -injectLoggedInStatus(false);