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
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
66 changes: 39 additions & 27 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 @@ -84,8 +92,9 @@ chrome.runtime.onMessageExternal.addListener(
const auth = await getAuth();
if (auth) {
const { accessToken, email } = auth;
const tab = await getCurrentTab();
sendResponse({
message: { accessToken, email },
message: { accessToken, email, url: tab.url },
status: "ok",
});
} else {
Expand All @@ -103,22 +112,25 @@ chrome.runtime.onMessageExternal.addListener(
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,
}, (response) => {
sendResponse(response);
});
} else {
sendResponse({ message: request.message, status: "no-tab" });
console.log("No active tab found, could not send message");
}
});
const tab = await getCurrentTab();
if (!tab.id) {
sendResponse({ message: request.message, status: "no-tab" });
console.log("No active tab found, could not send message");
return;
}
chrome.tabs.sendMessage(
tab.id,
{
data: request.data,
message: request.message,
},
(response) => {
sendResponse(response);
},
);
}
})();

return true;
},
);
17 changes: 9 additions & 8 deletions apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppliedModifications, Reactor } from "@repo/reactor";
import type { ModificationRequest } from "@repo/reactor";
import { Reactor, AppliedModifications } from "@repo/reactor";
import React from "react";
import ReactDOM from "react-dom";
import { createRoot } from "react-dom/client";
Expand Down Expand Up @@ -63,7 +63,6 @@ chrome.runtime.onMessage.addListener((request) => {
React.useEffect(() => {
chrome.runtime.onMessage.addListener(
(request, _sender, sendResponse) => {

// execute in async block so that we return true
// synchronously, telling chrome to wait for the response
(async () => {
Expand All @@ -76,12 +75,10 @@ chrome.runtime.onMessage.addListener((request) => {
if (request.message === "NEW_EDIT") {
if (request.data) {
const { find, highlightEdits, replace } = request.data;
await findReplaceAll(
find,
replace,
highlightEdits,
await findReplaceAll(find, replace, highlightEdits);
data = Array.from(reactor.getAppliedModifications()).map(
(mod) => mod.modificationRequest,
);
data = Array.from(reactor.getAppliedModifications()).map((mod) => mod.modificationRequest);
}
}
if (request.message === "STOP_EDITING") {
Expand Down Expand Up @@ -122,7 +119,11 @@ chrome.runtime.onMessage.addListener((request) => {
Object.assign(iframeRef.current.style, styles);
}

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