Skip to content

Commit

Permalink
Merge branch 'fix/safari-18' into feat/safari-support
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Oct 9, 2024
2 parents e8b650c + b3928fe commit d1bd5a5
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions packages/extension/src/libs/window-promise/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ class WindowPromise {
private async getRawResponse(
url: string,
msg: string,
tabId: number
tabInfo: { tabId: number }
): Promise<InternalOnMessageResponse> {
return new Promise((resolve) => {
Browser.tabs.onUpdated.addListener(function listener(_tabId, info, tab) {
if (info.status === "complete" && _tabId === tabId && tab.url === url) {
if (
info.status === "complete" &&
_tabId === tabInfo.tabId &&
tab.url === url
) {
resolve(
sendToNewWindowFromBackground(
{
provider: ProviderName.enkrypt,
message: msg,
},
tabId
tabInfo.tabId
)
);
Browser.tabs.onUpdated.removeListener(listener);
}
});
Browser.tabs.update(tabId, { url });
Browser.tabs.update(tabInfo.tabId, { url });
});
}

Expand All @@ -47,36 +51,49 @@ class WindowPromise {
msg: string,
unlockKeyring = false
): Promise<InternalOnMessageResponse> {
const loadingURL = "/index.html#/enkrypt/loading";
const tabInfo = {
tabId: -1,
};
const windowInfo = await Browser.windows.create({
url: "/index.html#/enkrypt/loading",
url: loadingURL,
type: "popup",
focused: true,
height: 600,
width: 460,
});
const tabId: number | undefined = windowInfo.tabs?.length
? windowInfo.tabs[0].id
: 0;
if (typeof tabId === "undefined") {
const validTabs = await Browser.tabs.query({
windowId: windowInfo.id,
});
tabInfo.tabId = validTabs.length && validTabs[0].id ? validTabs[0].id : -1;
if (typeof tabInfo.tabId === "undefined" || tabInfo.tabId === -1) {
return Promise.resolve({
error: getCustomError("unknown error, no tabId"),
});
}
const waitForWindow = async (): Promise<void> => {
// eslint-disable-next-line no-empty
while ((await Browser.tabs.get(tabId)).status !== "complete") {}
while ((await Browser.tabs.get(tabInfo.tabId)).status !== "complete") {}
};
await waitForWindow();
const monitorTabs = (): Promise<InternalOnMessageResponse> => {
return new Promise((resolve) => {
Browser.tabs.onRemoved.addListener(function tabListener(_tabId) {
if (_tabId === tabId) {
const tabReplacedListener = (addedId: number, replacedId: number) => {
if (replacedId === tabInfo.tabId) {
tabInfo.tabId = addedId;
}
};
const tabListener = (_tabId: number) => {
if (_tabId === tabInfo.tabId) {
Browser.tabs.onRemoved.removeListener(tabListener);
Browser.tabs.onReplaced.removeListener(tabReplacedListener);
resolve({
error: getError(ErrorCodes.userRejected),
});
}
});
};
Browser.tabs.onRemoved.addListener(tabListener);
Browser.tabs.onReplaced.addListener(tabReplacedListener);
});
};
const executePromise = async (): Promise<InternalOnMessageResponse> => {
Expand All @@ -91,28 +108,28 @@ class WindowPromise {
const unlockKeyring = await this.getRawResponse(
Browser.runtime.getURL(UNLOCK_PATH),
msg,
tabId
tabInfo
);
if (unlockKeyring.error) {
this.removeTab(tabId);
this.removeTab(tabInfo.tabId);
return unlockKeyring;
} else {
return await this.getRawResponse(
Browser.runtime.getURL(url),
msg,
tabId
tabInfo
).then((res) => {
this.removeTab(tabId);
this.removeTab(tabInfo.tabId);
return res;
});
}
}
return await this.getRawResponse(
Browser.runtime.getURL(url),
msg,
tabId
tabInfo
).then((res) => {
this.removeTab(tabId);
this.removeTab(tabInfo.tabId);
return res;
});
};
Expand Down

0 comments on commit d1bd5a5

Please sign in to comment.