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

Commit

Permalink
move ports higher in file and return true in the correct place to kee…
Browse files Browse the repository at this point in the history
…p connections open
  • Loading branch information
Kayla Fitzsimmons authored and Kayla Fitzsimmons committed Sep 20, 2024
1 parent 52be27e commit 2a8aea6
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 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 @@ -259,11 +255,10 @@ chrome.runtime.onMessageExternal.addListener(
}

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

return true;
},
);

0 comments on commit 2a8aea6

Please sign in to comment.