Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

move ports higher in file and return true in the correct place to kee… #190

Merged
merged 2 commits into from
Sep 20, 2024
Merged
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
59 changes: 27 additions & 32 deletions apps/mocksi-lite-next/src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ const MOCKSI_AUTH = "mocksi-auth";
let fallbackTab: null | chrome.tabs.Tab = null;
let prevLayoutEvent = "";

let mainIframeSrcPort: null | chrome.runtime.Port = null;
let topIframeSrcPort: null | chrome.runtime.Port = null;

addEventListener("install", () => {
// TODO test if this works on other browsers
chrome.tabs.create({
url: import.meta.env.VITE_NEST_APP,
});
});

chrome.runtime.onConnectExternal.addListener((port) => {
console.log("connecting...", port);
if (port.name === "extension/main") {
mainIframeSrcPort = port;
}
if (port.name === "extension/top") {
topIframeSrcPort = port;
}
});

const getAuth = async (): Promise<null | {
accessToken: string;
email: string;
Expand Down Expand Up @@ -107,31 +127,11 @@ async function showPlayIcon(tabId: number) {
});
}

addEventListener("install", () => {
// TODO test if this works on other browsers
chrome.tabs.create({
url: import.meta.env.VITE_NEST_APP,
});
});

let mainIframeSrcPort: null | chrome.runtime.Port = null;
let topIframeSrcPort: null | chrome.runtime.Port = null;

chrome.runtime.onConnectExternal.addListener((port) => {
console.log("connecting...", port);
if (port.name === "extension/main") {
mainIframeSrcPort = port;
}
if (port.name === "extension/top") {
topIframeSrcPort = port;
}
});

// when user clicks toolbar mount extension
chrome.action.onClicked.addListener((tab) => {
if (!tab?.id) {
console.log("No tab exits click, could not mount extension");
return;
return true;
}
// store the tab they clicked on to open the extension
// so we can use it as a fallback
Expand All @@ -147,6 +147,7 @@ chrome.action.onClicked.addListener((tab) => {
});
prevLayoutEvent = LayoutEvents.HIDE;
}
return true;
});

chrome.runtime.onMessageExternal.addListener(
Expand Down Expand Up @@ -199,12 +200,7 @@ chrome.runtime.onMessageExternal.addListener(
} else {
const tab = await getCurrentTab();
if (!tab?.id) {
sendResponse({
message: LayoutEvents.NO_TAB,
source: "background",
status: "ok",
});
console.error("No tab found");
console.error("No tab found: ");
return true;
}

Expand Down Expand Up @@ -243,9 +239,9 @@ chrome.runtime.onMessageExternal.addListener(
}
}
if (
request.message === AppEvents.EDIT_DEMO_START ||
request.message === DemoEditEvents.NEW_EDIT ||
request.message === DemoEditEvents.CHAT_RESPONSE
response.message === AppEvents.EDIT_DEMO_START ||
response.message === DemoEditEvents.CHAT_RESPONSE ||
response.message === DemoEditEvents.NEW_EDIT
) {
// notify extension/top # of edits changed
if (topIframeSrcPort) {
Expand All @@ -259,11 +255,10 @@ chrome.runtime.onMessageExternal.addListener(
}

sendResponse(response);
return true;
});
return true;
}
})();

return true;
},
);
Loading