Skip to content

Commit

Permalink
fix(webextension): add boot event
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 2, 2020
1 parent a86ebd2 commit 34babc9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/webextension/app/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ browser.runtime.onConnect.addListener(async (port) => {
const blob = new Blob([script.code], { type: "application/javascript" });
return createTextlintWorker(URL.createObjectURL(blob));
});
console.log("[Background] workers", workers);
console.log("[Background] workers started", workers);
// Support multiple workers
const ext = ".md";
const lintEngine: LintEngineAPI = {
Expand Down Expand Up @@ -128,6 +128,7 @@ browser.runtime.onConnect.addListener(async (port) => {
});
});
console.log("[Background] content port", port);
await Promise.all(workers.map((worker) => worker.ready()));
Comlink.expose(backgroundExposedObject, createBackgroundEndpoint(port));
await Promise.all(workers.map((worker) => worker.ready()));
port.postMessage("textlint-editor-boot");
});
13 changes: 9 additions & 4 deletions packages/webextension/app/scripts/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { createEndpoint } from "comlink-extension";
import * as Comlink from "comlink";
import type { backgroundExposedObject } from "./background";

const port = Comlink.wrap<backgroundExposedObject>(createEndpoint(browser.runtime.connect()));
const rawPort = browser.runtime.connect();
const port = Comlink.wrap<backgroundExposedObject>(createEndpoint(rawPort));
const targetElement = document.querySelectorAll("textarea");

async function contentScriptMain() {
Expand All @@ -26,7 +27,11 @@ async function contentScriptMain() {
});
}

console.log("[ContentScript]", "main");
contentScriptMain().catch((error) => {
console.error("[texlint editor ContentScriptError]", error);
console.log("[ContentScript]", "main loaded");
rawPort.onMessage.addListener((event) => {
if (event === "textlint-editor-boot") {
contentScriptMain().catch((error) => {
console.error("[texlint editor ContentScriptError]", error);
});
}
});

0 comments on commit 34babc9

Please sign in to comment.