Skip to content

Commit

Permalink
Refactor to use promises (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
tilfin authored Nov 7, 2023
1 parent f8fe990 commit a1f85d4
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,27 @@ import { SessionMemory, SyncStorageRepository } from './lib/storage_repository.j
const sessionMemory = new SessionMemory(chrome || browser);

function openOptions() {
if (window.chrome) {
chrome.runtime.openOptionsPage(err => {
if (err) console.error(`Error: ${err}`);
});
} else if (window.browser) {
window.browser.runtime.openOptionsPage().catch(err => {
if (err) console.error(`Error: ${err}`);
});
}
(chrome || browser).runtime.openOptionsPage().catch(err => {
console.error(`Error: ${err}`);
});
}

function getCurrentTab() {
if (window.chrome) {
return new Promise((resolve) => {
chrome.tabs.query({ currentWindow:true, active:true }, tabs => {
resolve(tabs[0])
})
})
} else if (window.browser) {
return browser.tabs.query({ currentWindow:true, active:true }).then(tabs => tabs[0])
}
function openPage(pageUrl) {
const brw = chrome || browser;
const url = brw.runtime.getURL(pageUrl);
brw.tabs.create({ url }).catch(err => {
console.error(`Error: ${err}`);
});
}

function executeAction(tabId, action, data) {
if (window.chrome) {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tabId, { action, data }, {}, resolve)
})
} else if (window.browser) {
return browser.tabs.sendMessage(tabId, { action, data })
}
async function getCurrentTab() {
const brw = chrome || browser;
const [tab] = await brw.tabs.query({ currentWindow:true, active:true });
return tab;
}

async function executeAction(tabId, action, data) {
return (chrome || browser).tabs.sendMessage(tabId, { action, data });
}

window.onload = function() {
Expand All @@ -48,17 +38,17 @@ window.onload = function() {
}

document.getElementById('openUpdateNoticeLink').onclick = function(e) {
chrome.tabs.create({ url: chrome.runtime.getURL('updated.html')}, function(tab){});
openPage('updated.html');
return false;
}

document.getElementById('openCreditsLink').onclick = function(e) {
chrome.tabs.create({ url: chrome.runtime.getURL('credits.html')}, function(tab){});
openPage('credits.html');
return false;
}

document.getElementById('openSupportersLink').onclick = document.getElementById('openSupportMe').onclick = function(e) {
chrome.tabs.create({ url: chrome.runtime.getURL('supporters.html')}, function(tab){});
openPage('supporters.html');
return false;
}

Expand Down

0 comments on commit a1f85d4

Please sign in to comment.