Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function toggleSaka(tabId) {
if (SAKA_DEBUG) console.group('toggleSaka');
// Get the specified tab, or the current tab if none is specified
const currentTab =
tabId === undefined
? (await browser.tabs.query({
active: true,
currentWindow: true
}))[0]
: await browser.tabs.get(tabId);
if (currentTab) {
// If the current tab is Saka, switch to the previous tab (if it exists) and close the current tab
if (currentTab.url === browser.runtime.getURL('saka.html')) {
if (lastTabId) {
try {
const lastTab = await browser.tabs.get(lastTabId);
if (lastTab) {
try {
await browser.tabs.update(lastTabId, { active: true });
if (SAKA_DEBUG) console.log(`Switched to tab ${lastTab.url}`);
} catch (e) {
if (SAKA_DEBUG)
const openTabLists = async () => {
// open only one in a window
const window = await browser.runtime.getBackgroundPage()
if (!_.isObject(window.appTabId)) window.appTabId = {}
const currentWindow = await browser.windows.getCurrent()
const windowId = currentWindow.id
const tabListsUrl = browser.runtime.getURL('index.html#/app/')
if (windowId in window.appTabId) {
const tabs = await getAllInWindow(windowId)
const tab = tabs.find(tab => tab.id === window.appTabId[windowId])
if (tab) {
if (tab.url.startsWith(tabListsUrl)) {
return browser.tabs.update(tab.id, { active: true })
}
delete window.appTabId[windowId]
}
}
const createdTab = await browser.tabs.create({url: tabListsUrl})
window.appTabId[windowId] = createdTab.id
}
async function _reloadIpfsClientDependents (instance, opts) {
// online || offline
if (browser.tabs && browser.tabs.query) {
const tabs = await browser.tabs.query({})
if (tabs) {
tabs.forEach((tab) => {
// detect bundled webui in any of open tabs
if (_isWebuiTab(tab.url)) {
browser.tabs.reload(tab.id)
log('reloading bundled webui')
}
})
}
}
// online only
if (client && instance) {
// add important data to local ipfs repo for instant load
precache(instance)
}
}
const reload = async () => {
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
if (tabs[0]) {
console.log('tabs', tabs);
browser.tabs.reload(tabs[0].id);
}
browser.runtime.reload();
};
const openPage = (url: string) => () => browser.tabs.create({ url });
const addListeners = () => {
const handleReplace = () => replacePage();
browser.tabs.onActivated.addListener(handleReplace);
browser.windows.onFocusChanged.addListener(handleReplace);
browser.storage.onChanged.addListener((changes, areaName) => {
handleSettingsChange(changes, areaName);
setAutoSave(changes, areaName);
updateLogLevel();
});
browser.tabs.onUpdated.addListener(handleTabUpdated);
browser.tabs.onRemoved.addListener(handleTabRemoved);
browser.tabs.onCreated.addListener(setUpdateTempTimer);
browser.tabs.onMoved.addListener(setUpdateTempTimer);
browser.windows.onCreated.addListener(setUpdateTempTimer);
browser.windows.onRemoved.addListener(autoSaveWhenWindowClose);
};
function endSelection(tabId) {
UiState.setSelectingTarget(false)
browser.tabs
.sendMessage(tabId, { selectMode: true, selecting: false })
.catch(() => {})
}
if (tab.id !== tempTabId) {
reloadingTabs.push(browser.tabs.reload(tab.id, {bypassCache: true}));
}
}
await Promise.all(reloadingTabs);
} else if (options.reloadTabs === 'active') {
if (['allButActive', 'false'].includes(options.closeTabs)) {
await browser.tabs.reload(activeTabId, {bypassCache: true});
}
} else if (options.reloadTabs === 'allButActive') {
const reloadingTabs = [];
const tabs = await browser.tabs.query({});
for (const tab of tabs) {
if (![activeTabId, tempTabId].includes(tab.id)) {
reloadingTabs.push(browser.tabs.reload(tab.id, {bypassCache: true}));
}
}
await Promise.all(reloadingTabs);
}
}
}
const getTabInfo = async () => {
try {
const tab = (await browser.tabs.query({ currentWindow: true, active: true }))[0];
const tabUrl = browser.tabs.sendMessage(tab.id, { message: "getTabUrl" });
const selectedText = browser.tabs.sendMessage(tab.id, { message: "getSelectedText" });
const isEnabledOnPage = browser.tabs.sendMessage(tab.id, { message: "getEnabled" });
const tabInfo = await Promise.all([tabUrl, selectedText, isEnabledOnPage]);
return {
isConnected: true,
url: tabInfo[0],
selectedText: tabInfo[1],
isEnabledOnPage: tabInfo[2]
};
} catch (e) {
return { isConnected: false, url: "", selectedText: "", isEnabledOnPage: false };
}
};
async function closeTabsExceptCurrent() {
const otherTabs = await browser.tabs.query({'active': false, currentWindow: true})
for(const tab of otherTabs) {
await browser.tabs.remove(tab.id)
}
const popupWindow = await browser.extension.getViews({type: 'popup'})[0]
popupWindow.close()
}