Refactor tab creation to improve errors (#1110)

This commit is contained in:
toasted-nutbread 2020-12-13 16:09:11 -05:00 committed by GitHub
parent 05e213674c
commit 96e68be0fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -206,7 +206,7 @@ class Backend {
const options = this.getOptions({current: true}); const options = this.getOptions({current: true});
if (options.general.showGuide) { if (options.general.showGuide) {
chrome.tabs.create({url: chrome.runtime.getURL('/bg/welcome.html')}); this._openWelcomeGuidePage();
} }
this._clipboardMonitor.on('change', this._onClipboardTextChange.bind(this)); this._clipboardMonitor.on('change', this._onClipboardTextChange.bind(this));
@ -782,41 +782,21 @@ class Backend {
} catch (e) { } catch (e) {
// NOP // NOP
} }
chrome.tabs.create({url}); await this._createTab(url);
return; return;
case 'newTab': case 'newTab':
chrome.tabs.create({url}); await this._createTab(url);
return; return;
} }
} }
_onCommandHelp() { async _onCommandHelp() {
const url = chrome.runtime.getURL('/bg/info.html'); await this._openInfoPage();
chrome.tabs.create({url});
} }
_onCommandOptions(params) { async _onCommandOptions(params) {
const {useSettingsV2} = this._options.global;
const {mode='existingOrNewTab'} = params || {}; const {mode='existingOrNewTab'} = params || {};
if (mode === 'existingOrNewTab') { await this._openSettingsPage(mode);
if (useSettingsV2) {
(async () => {
const url = chrome.runtime.getURL('/bg/settings2.html');
const tab = await this._findTab(1000, (url2) => url2.startsWith(url));
if (tab !== null) {
await this._focusTab(tab);
} else {
chrome.tabs.create({url});
}
})();
return;
}
chrome.runtime.openOptionsPage();
} else if (mode === 'newTab') {
const manifest = chrome.runtime.getManifest();
const url = chrome.runtime.getURL(useSettingsV2 ? '/bg/settings2.html' : manifest.options_ui.page);
chrome.tabs.create({url});
}
} }
async _onCommandToggle() { async _onCommandToggle() {
@ -1735,4 +1715,57 @@ class Backend {
} }
return enabledDictionaryMap; return enabledDictionaryMap;
} }
async _openWelcomeGuidePage() {
await this._createTab(chrome.runtime.getURL('/bg/welcome.html'));
}
async _openInfoPage() {
await this._createTab(chrome.runtime.getURL('/bg/info.html'));
}
async _openSettingsPage(mode) {
const {useSettingsV2} = this._options.global;
const manifest = chrome.runtime.getManifest();
const url = chrome.runtime.getURL(useSettingsV2 ? '/bg/settings2.html' : manifest.options_ui.page);
switch (mode) {
case 'existingOrNewTab':
if (useSettingsV2) {
const tab = await this._findTab(1000, (url2) => url2.startsWith(url));
if (tab !== null) {
await this._focusTab(tab);
} else {
await this._createTab(url);
}
} else {
await new Promise((resolve, reject) => {
chrome.runtime.openOptionsPage(() => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve();
}
});
});
}
break;
case 'newTab':
await this._createTab(url);
break;
}
}
_createTab(url) {
return new Promise((resolve, reject) => {
chrome.tabs.create({url}, (tab) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(tab);
}
});
});
}
} }