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

Filter by domain - pass domain to ext in iframe #159

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Changes from 3 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
68 changes: 37 additions & 31 deletions apps/mocksi-lite-next/src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,33 @@ const clearAuth = async (): Promise<void> => {
}
};

const showAuthTab = async (): Promise<void> => {
return new Promise((resolve) => {
chrome.tabs.query({}, function(tabs) {
async function getCurrentTab() {
const queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
const [tab] = await chrome.tabs.query(queryOptions);
return tab;
}

function showAuthTab() {
return new Promise((resolve: (value?: unknown) => void) => {
chrome.tabs.query({}, function (tabs) {
let tabExists = false;
for (let tab of tabs) {
const loadUrl = new URL(import.meta.env.VITE_NEST_APP);
const tabUrl = new URL(tab.url || tab.pendingUrl);
if (loadUrl.href === tabUrl.href) {
tabExists = true;
break;
}
for (const tab of tabs) {
const tabUrlStr = tab.url || tab.pendingUrl || "";
const loadUrl = new URL(import.meta.env.VITE_NEST_APP);
const tabUrl = new URL(tabUrlStr);
if (loadUrl.href === tabUrl.href) {
tabExists = true;
break;
}
}
if (!tabExists) {
chrome.tabs.create({ url: import.meta.env.VITE_NEST_APP }, resolve);
chrome.tabs.create({ url: import.meta.env.VITE_NEST_APP }, resolve);
} else {
resolve();
}
});
})
});
}

addEventListener("install", () => {
Expand Down Expand Up @@ -81,35 +89,33 @@ chrome.runtime.onMessageExternal.addListener(
const auth = await getAuth();
if (auth) {
const { accessToken, email } = auth;
const tab = await getCurrentTab();
sendResponse({
message: { accessToken, email },
// pass the url where the extension is mounted so
// we can filter recordings by domain on the server
message: { accessToken, email, url: tab.url },
status: "ok",
});
} else {
await showAuthTab();
chrome.tabs.create({
url: import.meta.env.VITE_NEST_APP,
});
sendResponse({
message: "authenticating",
status: "ok",
});
}
} else if (request.message === "AUTH_ERROR") {
await clearAuth();
await showAuthTab();
sendResponse({
message: "authenticating",
status: "ok",
});
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
data: request.data,
message: request.message,
});
} else {
console.log("No active tab found, could not send message");
}
});
const tab = await getCurrentTab();
if (tab?.id) {
chrome.tabs.sendMessage(tab?.id, {
data: request.data,
message: request.message,
});
} else {
console.log("No active tab found, could not send message");
}

sendResponse({ message: request.message, status: "ok" });
return true;
}
Expand Down
Loading