2022-03-18 20:07:05 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
const returnToOnboardingInitiatorTab = async (onboardingInitiator) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const tab = await new Promise((resolve) => {
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.tabs.update(
|
2020-11-03 00:41:28 +01:00
|
|
|
onboardingInitiator.tabId,
|
|
|
|
{ active: true },
|
|
|
|
// eslint-disable-next-line no-shadow
|
|
|
|
(tab) => {
|
|
|
|
if (tab) {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve(tab);
|
2020-11-03 00:41:28 +01:00
|
|
|
} else {
|
|
|
|
// silence console message about unchecked error
|
2022-03-18 20:07:05 +01:00
|
|
|
if (browser.runtime.lastError) {
|
|
|
|
log.debug(browser.runtime.lastError);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve();
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2019-11-22 18:03:51 +01:00
|
|
|
|
2020-08-14 13:47:43 +02:00
|
|
|
if (tab) {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.close();
|
2020-08-14 13:47:43 +02:00
|
|
|
} else {
|
2022-03-18 20:07:05 +01:00
|
|
|
// this case can happen if the tab was closed since being checked with `browser.tabs.get`
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
`Setting current tab to onboarding initiator has failed; falling back to redirect`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
window.location.assign(onboardingInitiator.location);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
export const returnToOnboardingInitiator = async (onboardingInitiator) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const tab = await new Promise((resolve) => {
|
2020-08-18 18:36:45 +02:00
|
|
|
// eslint-disable-next-line no-shadow
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.tabs.get(onboardingInitiator.tabId, (tab) => {
|
2019-11-22 18:03:51 +01:00
|
|
|
if (tab) {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve(tab);
|
2019-11-22 18:03:51 +01:00
|
|
|
} else {
|
|
|
|
// silence console message about unchecked error
|
2022-03-18 20:07:05 +01:00
|
|
|
if (browser.runtime.lastError) {
|
|
|
|
log.debug(browser.runtime.lastError);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve();
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
if (tab) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await returnToOnboardingInitiatorTab(onboardingInitiator);
|
2019-11-22 18:03:51 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.location.assign(onboardingInitiator.location);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|