webext-helpers
Version:
Cross-browser helper utility functions for MV3 & MV2 web extensions.
55 lines (52 loc) • 1.91 kB
JavaScript
const $82cbb5a2f3a1bcd0$export$47972b4b516318c6 = ()=>{
if (!("browser" in self)) self.browser = self.chrome;
};
const $82cbb5a2f3a1bcd0$export$23fe9b5e06a2f955 = async ()=>{
const tabs = await browser.tabs.query({
active: true,
currentWindow: true
});
return tabs[0];
};
const $82cbb5a2f3a1bcd0$export$f563f5bfeb72a90b = (func, args)=>{
return new Promise((resolve, reject)=>{
// Call the func with this promise's resolve as the callback.
const p = func(...args, resolve);
// Func returned a promise, resolve this func's promise with the data.
typeof p.then === "function" && p.then((data)=>resolve(data)).catch((err)=>reject(err));
});
};
const $82cbb5a2f3a1bcd0$export$c73e2280af4fe8c5 = async (url)=>{
const splitUrl = url.split("#");
const hashSegment = splitUrl[1] ? `#${splitUrl[1]}` : "";
const queryUrl = splitUrl[0];
const targetUrl = queryUrl + hashSegment // Isn't this just the same as the original url arg?
;
const tabs = await browser.tabs.query({
url: queryUrl,
currentWindow: true
});
if (tabs.length) {
await browser.tabs.update(tabs[0].id, {
active: true,
url: targetUrl
});
return {
result: "switched",
tab: tabs[0]
};
} else {
const tab = await browser.tabs.create({
url: targetUrl
});
await browser.windows.update(tab.windowId, {
focussed: true
});
return {
result: "opened",
tab: tab
};
}
};
$82cbb5a2f3a1bcd0$export$47972b4b516318c6();
export {$82cbb5a2f3a1bcd0$export$47972b4b516318c6 as setBrowserNamespace, $82cbb5a2f3a1bcd0$export$23fe9b5e06a2f955 as getCurrentTab, $82cbb5a2f3a1bcd0$export$f563f5bfeb72a90b as promback, $82cbb5a2f3a1bcd0$export$c73e2280af4fe8c5 as goToTab};