diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index a33ff94f..7b806e27 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -144,28 +144,30 @@ async function apiTemplateRender(template, data, dynamic) { } } -async function apiCommandExec(command) { +async function apiCommandExec(command, params) { const handlers = apiCommandExec.handlers; if (handlers.hasOwnProperty(command)) { const handler = handlers[command]; - handler(); + handler(params); } } apiCommandExec.handlers = { - search: async () => { + search: async (params) => { const url = chrome.extension.getURL('/bg/search.html'); - try { - const tab = await apiFindTab(1000, (url2) => ( - url2 !== null && - url2.startsWith(url) && - (url2.length === url.length || url2[url.length] === '?' || url2[url.length] === '#') - )); - if (tab !== null) { - await apiFocusTab(tab); - return; + if (!(params && params.newTab)) { + try { + const tab = await apiFindTab(1000, (url2) => ( + url2 !== null && + url2.startsWith(url) && + (url2.length === url.length || url2[url.length] === '?' || url2[url.length] === '#') + )); + if (tab !== null) { + await apiFocusTab(tab); + return; + } + } catch (e) { + // NOP } - } catch (e) { - // NOP } chrome.tabs.create({url}); }, diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 7560f39e..f29230a2 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -181,7 +181,7 @@ Backend.messageHandlers = { definitionsAddable: ({definitions, modes, optionsContext}) => apiDefinitionsAddable(definitions, modes, optionsContext), noteView: ({noteId}) => apiNoteView(noteId), templateRender: ({template, data, dynamic}) => apiTemplateRender(template, data, dynamic), - commandExec: ({command}) => apiCommandExec(command), + commandExec: ({command, params}) => apiCommandExec(command, params), audioGetUrl: ({definition, source, optionsContext}) => apiAudioGetUrl(definition, source, optionsContext), screenshotGet: ({options}, sender) => apiScreenshotGet(options, sender), forward: ({action, params}, sender) => apiForward(action, params, sender), diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js index a29f7aa7..a16c8769 100644 --- a/ext/bg/js/context.js +++ b/ext/bg/js/context.js @@ -25,6 +25,20 @@ function showExtensionInfo() { node.textContent = `${manifest.name} v${manifest.version}`; } +function setupButtonEvents(selector, command) { + $(selector) + .on('click', (e) => { + if (e.button !== 0) { return; } + apiCommandExec(command, {newTab: e.ctrlKey}); + e.preventDefault(); + }) + .on('auxclick', (e) => { + if (e.button !== 1) { return; } + apiCommandExec(command, {newTab: true}); + e.preventDefault(); + }); +} + $(document).ready(utilAsync(() => { showExtensionInfo(); @@ -33,9 +47,9 @@ $(document).ready(utilAsync(() => { document.documentElement.dataset.mode = (browser === 'firefox-mobile' ? 'full' : 'mini'); }); - $('.action-open-search').click(() => apiCommandExec('search')); - $('.action-open-options').click(() => apiCommandExec('options')); - $('.action-open-help').click(() => apiCommandExec('help')); + setupButtonEvents('.action-open-search', 'search'); + setupButtonEvents('.action-open-options', 'options'); + setupButtonEvents('.action-open-help', 'help'); const optionsContext = { depth: 0, diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index 2294cb8b..b0746b85 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -49,8 +49,8 @@ function apiAudioGetUrl(definition, source, optionsContext) { return utilInvoke('audioGetUrl', {definition, source, optionsContext}); } -function apiCommandExec(command) { - return utilInvoke('commandExec', {command}); +function apiCommandExec(command, params) { + return utilInvoke('commandExec', {command, params}); } function apiScreenshotGet(options) {