Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for tabs api as an alternate in prompt opening logi… #2882

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Changes from 1 commit
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
161 changes: 87 additions & 74 deletions src/common/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const utils = {
openUrl: (url: string) => {
browser.tabs.create({ url });
},

openPrompt: async <Type>(message: {
args: Record<string, unknown>;
origin: OriginData | OriginDataInternal;
Expand Down Expand Up @@ -85,89 +86,101 @@ const utils = {
const { top, left } = await getWindowPosition(windowWidth, windowHeight);

return new Promise((resolve, reject) => {
browser.windows
.create({
url: url,
type: "popup",
width: windowWidth,
height: windowHeight,
top: top,
left: left,
})
.then((window) => {
let closeWindow = true; // by default we call remove.window (except the browser forces this prompt to open in a tab)
let tabId: number | undefined;
if (window.tabs) {
tabId = window.tabs[0].id;
}
const createPopup = (options: browser.Windows.CreateCreateDataType) => {
return browser.windows.create(options);
};

// Kiwi Browser opens the prompt in the same window (there are only tabs on mobile browsers)
// Find the currently active tab to validate messages
if (window.tabs && window.tabs?.length > 1) {
tabId = window.tabs?.find((x) => x.active)?.id;
closeWindow = false; // we'll only remove the tab and not the window further down
}
const createTab = (options: browser.Tabs.CreateCreatePropertiesType) => {
return browser.tabs.create(options);
};

// Re-focus the popup after 2 seconds to mitigate the problem of lost popups
// (e.g. when a user clicks the website)
setTimeout(() => {
if (!window.id) return;
const optionsPopup: browser.Windows.CreateCreateDataType = {
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
url: url,
type: "popup",
width: windowWidth,
height: windowHeight,
top: top,
left: left,
};

browser.windows.update(window.id, {
focused: true,
});
}, 2100);

const onMessageListener = (
responseMessage: {
response?: unknown;
error?: string;
data: Type;
},
sender: Runtime.MessageSender
) => {
if (
responseMessage &&
responseMessage.response &&
sender.tab &&
sender.tab.id === tabId &&
sender.tab.windowId
) {
browser.tabs.onRemoved.removeListener(onRemovedListener);
// if the window was opened as tab we remove the tab
// otherwise if a window was opened we have to remove the window.
// Opera fails to close the window with tabs.remove - it fails with: "Tabs cannot be edited right now (user may be dragging a tab)"
let closePromise;
if (closeWindow) {
closePromise = browser.windows.remove(sender.tab.windowId);
} else {
closePromise = browser.tabs.remove(sender.tab.id as number); // as number only for TS - we check for sender.tab.id in the if above
}
const optionsTab: browser.Tabs.CreateCreatePropertiesType = {
url: url,
};

return closePromise.then(() => {
// in the future actual "remove" (closing prompt) will be moved to component for i.e. budget flow
// https://github.com/getAlby/lightning-browser-extension/issues/1197
if (responseMessage.error) {
return reject(new Error(responseMessage.error));
} else {
return resolve(responseMessage);
}
});
}
};
const createPromise = browser.windows
? createPopup(optionsPopup)
: createTab(optionsTab);

return createPromise.then(async (windowOrTab) => {
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
let tabId: number | undefined;

if (windowOrTab && "tabs" in windowOrTab && windowOrTab.tabs) {
tabId = windowOrTab.tabs[0].id;
} else {
const tabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
tabId = tabs[0].id;
}

// Re-focus the popup after 2 seconds to mitigate the problem of lost popups
// (e.g. when a user clicks the website)

const onRemovedListener = (tid: number) => {
if (tabId === tid) {
browser.runtime.onMessage.removeListener(onMessageListener);
reject(new Error(ABORT_PROMPT_ERROR));
const onMessageListener = (
responseMessage: {
response?: unknown;
error?: string;
data: Type;
},
sender: Runtime.MessageSender
) => {
responseMessage && responseMessage.response;

if (
responseMessage &&
responseMessage.response &&
sender.tab &&
sender.tab.id === tabId &&
sender.tab.windowId
) {
browser.tabs.onRemoved.removeListener(onRemovedListener);
// if the window was opened as tab we remove the tab
// otherwise if a window was opened we have to remove the window.
// Opera fails to close the window with tabs.remove - it fails with: "Tabs cannot be edited right now (user may be dragging a tab)"

let closePromise;
if (browser.windows !== undefined) {
closePromise = browser.windows.remove(sender.tab.windowId);
} else {
closePromise = browser.tabs.remove(sender.tab.id as number); // as number only for TS - we check for sender.tab.id in the if above
}
};

browser.runtime.onMessage.addListener(onMessageListener);
browser.tabs.onRemoved.addListener(onRemovedListener);
});
return closePromise.then(() => {
// in the future actual "remove" (closing prompt) will be moved to component for i.e. budget flow
// https://github.com/getAlby/lightning-browser-extension/issues/1197
if (responseMessage.error) {
return reject(new Error(responseMessage.error));
} else {
return resolve(responseMessage);
}
});
}
};

const onRemovedListener = (tid: number) => {
if (tabId === tid) {
browser.runtime.onMessage.removeListener(onMessageListener);
reject(new Error(ABORT_PROMPT_ERROR));
}
};

browser.runtime.onMessage.addListener(onMessageListener);
browser.tabs.onRemoved.addListener(onRemovedListener);
});
});
},

getBoostagramFromInvoiceCustomRecords: (
custom_records: ConnectorTransaction["custom_records"] | undefined
) => {
Expand Down
Loading