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

Prev

Unregister old content-script before registering the new one

  • Loading branch information
sammacbeth committed Dec 4, 2020
commit 756d9d649e3021401026c52c055b1686892518e2
@@ -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);
ProTip! Use n and p to navigate between commits in a pull request.