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

Expose logged-in state to ghosterysearch page via content-script. #34

Merged
merged 4 commits into from Dec 4, 2020
Merged
Changes from 1 commit
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

Next

Expose logged-in state to ghosterysearch page via content-script.

  • Loading branch information
sammacbeth committed Dec 3, 2020
commit 027fcfb18cd50974b24a25cc38b4bf85fe60684e
@@ -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 }) => {
@@ -0,0 +1,5 @@
"use strict";

(async function () {
This conversation was marked as resolved by chrmod

This comment has been minimized.

@chrmod

chrmod Dec 4, 2020
Member

why async? does it need one tick? maybe requestAnimationFrame would be faster?

This comment has been minimized.

@sammacbeth

sammacbeth Dec 4, 2020
Author Contributor

async was by mistake. Removed.

document.querySelector('html').classList.add('ghostery-browser');
})()
@@ -13,15 +13,20 @@
"background.js",
"choice-screen.js",
"metrics.js",
"api.js"
"api.js",
"status.js"
]
},
"content_scripts": [{
"matches": [
"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/*",
@@ -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({
This conversation was marked as resolved by chrmod

This comment has been minimized.

@chrmod

chrmod Dec 4, 2020
Member

will this replace the previously registered content script?

This comment has been minimized.

@sammacbeth

sammacbeth Dec 4, 2020
Author Contributor

No, I forgot the deregistration part...

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);
This conversation was marked as resolved by chrmod

This comment has been minimized.

@chrmod

chrmod Dec 4, 2020
Member

Not sure if there is a need for doing that.

This comment has been minimized.

@sammacbeth

sammacbeth Dec 4, 2020
Author Contributor

Removed

ProTip! Use n and p to navigate between commits in a pull request.