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

Improve logging around contentScript loading/deduplication code #5025

Merged
merged 8 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 9 additions & 237 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
"webext-messenger": "^0.22.0",
"webext-patterns": "^1.1.1",
"webext-polyfill-kinda": "^0.10.0",
"webext-tools": "^1.1.1",
"webext-tools": "^1.1.2",
"webextension-polyfill": "^0.10.0",
"whatwg-mimetype": "^3.0.0",
"yup": "^0.32.11"
Expand Down
2 changes: 1 addition & 1 deletion src/background/browserAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function _toggleSidebar(tabId: number, tabUrl: string): Promise<void> {
// want to catch each error separately.
const sidebarTogglePromise = executeScript({
tabId,
frameId: 0,
frameId: TOP_LEVEL_FRAME_ID,
files: ["browserActionInstantHandler.js"],
});
const contentScriptPromise = ensureContentScript({
Expand Down
22 changes: 7 additions & 15 deletions src/contentScript/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import "@/development/visualInjection";
import { uuidv4 } from "@/types/helpers";
import {
isInstalledInThisSession,
isReadyInThisDocument,
setInstalledInThisSession,
setReadyInThisDocument,
unsetReadyInThisDocument,
Expand All @@ -30,22 +29,17 @@ import { onContextInvalidated } from "@/errors/contextInvalidated";

// See note in `@/contentScript/ready.ts` for further details about the lifecycle of content scripts
async function initContentScript() {
const context = top === self ? "" : `in frame ${location.href}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before

Without this piece of information, it seems that there are multiple injections in the same page:

Screenshot 3

After

This makes it clear that there are multiple frames. I'm only logging this piece of information one to avoid too much noise. The console already allows filtering by context, should you wish to debug it further.

Screenshot 4

const uuid = uuidv4();

if (isInstalledInThisSession()) {
console.error(
"contentScript: was requested twice in the same context, aborting injection"
`contentScript: was requested twice in the same context, aborting injection ${context}`
);
return;
}

if (isReadyInThisDocument()) {
console.warn(
"contentScript: injecting again because the previous context was invalidated"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was no longer reached because at the end of initContentScript we're already marking the document as not ready

);
} else {
console.debug(`contentScript: injecting ${uuid}`);
}
console.debug(`contentScript: importing ${uuid} ${context}`);

setInstalledInThisSession();

Expand All @@ -58,7 +52,7 @@ async function initContentScript() {
void logPromiseDuration("contentScript: imported", contentScript);

const { init } = await contentScript;
await init(uuid);
await logPromiseDuration("contentScript: ready", init());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of duplicate injection, the previous setup was logging "ready" after an error. Moving the logging fixes it.

Screenshot 5

setReadyInThisDocument(uuid);

// eslint-disable-next-line promise/prefer-await-to-then -- It's an unrelated event listener
Expand All @@ -70,11 +64,9 @@ async function initContentScript() {

if (location.protocol === "https:") {
// eslint-disable-next-line promise/prefer-await-to-then -- Top-level await isn't available
void logPromiseDuration("contentScript: ready", initContentScript()).catch(
(error) => {
throw new Error("Error initializing contentScript", { cause: error });
}
);
void initContentScript().catch((error) => {
throw new Error("Error initializing contentScript", { cause: error });
});
} else {
console.warn("Unsupported protocol", location.protocol);
}
3 changes: 1 addition & 2 deletions src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
notifyContextInvalidated,
} from "@/errors/contextInvalidated";
import { onUncaughtError } from "@/errors/errorHelpers";
import { type UUID } from "@/core";
import initSandbox from "@/sandbox/messenger/api";

// Must come before the default handler for ignoring errors. Otherwise, this handler might not be run
Expand All @@ -49,7 +48,7 @@ onUncaughtError((error) => {
}
});

export async function init(uuid: UUID): Promise<void> {
export async function init(): Promise<void> {
registerMessenger();
registerExternalMessenger();
registerBuiltinBlocks();
Expand Down
Loading