aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authorsiikamiika <siikamiika@users.noreply.github.com>2020-01-25 17:54:07 +0200
committersiikamiika <siikamiika@users.noreply.github.com>2020-02-09 21:51:33 +0200
commitddc7c71e4f9da861d9d2bd56e60804ee9e70f621 (patch)
tree69d58fdbde44bfd20279bc87a7b6632138b4553c /ext/bg/js
parentd5708de4eed15567e14e0c1fd4998561eee1680e (diff)
add support for native popup windows
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/backend.js54
-rw-r--r--ext/bg/js/context.js4
2 files changed, 41 insertions, 17 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 43ee81c3..23811e9d 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -33,6 +33,7 @@ class Backend {
this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve));
this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target');
+ this.popupWindow = null;
this.apiForwarder = new BackendApiForwarder();
}
@@ -565,23 +566,46 @@ class Backend {
// Command handlers
async _onCommandSearch(params) {
- const url = chrome.runtime.getURL('/bg/search.html');
- if (!(params && params.newTab)) {
- try {
- const tab = await Backend._findTab(1000, (url2) => (
- url2 !== null &&
- url2.startsWith(url) &&
- (url2.length === url.length || url2[url.length] === '?' || url2[url.length] === '#')
- ));
- if (tab !== null) {
- await Backend._focusTab(tab);
- return;
+ const {mode, query} = params || {};
+
+ const optionsContext = {depth: 0};
+ const options = await this.getOptions(optionsContext);
+ const {popupWidth, popupHeight} = options.general;
+
+ const baseUrl = chrome.runtime.getURL('/bg/search.html');
+ const queryString = (query && query.length > 0) ? `?query=${encodeURIComponent(query)}` : '';
+ const url = baseUrl + queryString;
+
+ switch (mode) {
+ case 'sameTab':
+ try {
+ const tab = await Backend._findTab(1000, (url2) => (
+ url2 !== null &&
+ url2.startsWith(url) &&
+ (url2.length === url.length || url2[url.length] === '?' || url2[url.length] === '#')
+ ));
+ if (tab !== null) {
+ await Backend._focusTab(tab);
+ return;
+ }
+ } catch (e) {
+ // NOP
}
- } catch (e) {
- // NOP
- }
+ chrome.tabs.create({url});
+ return;
+ case 'newTab':
+ chrome.tabs.create({url});
+ return;
+ case 'popup':
+ if (this.popupWindow !== null) {
+ chrome.windows.remove(this.popupWindow.id);
+ }
+ chrome.windows.create(
+ {url, width: popupWidth, height: popupHeight, type: 'popup'},
+ (popupWindow) => { this.popupWindow = popupWindow; }
+ );
+ return;
}
- chrome.tabs.create({url});
}
_onCommandHelp() {
diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js
index 834174bf..a0e5adc9 100644
--- a/ext/bg/js/context.js
+++ b/ext/bg/js/context.js
@@ -30,12 +30,12 @@ function setupButtonEvents(selector, command, url) {
for (const node of nodes) {
node.addEventListener('click', (e) => {
if (e.button !== 0) { return; }
- apiCommandExec(command, {newTab: e.ctrlKey});
+ apiCommandExec(command, {mode: e.ctrlKey ? 'newTab' : 'sameTab'});
e.preventDefault();
}, false);
node.addEventListener('auxclick', (e) => {
if (e.button !== 1) { return; }
- apiCommandExec(command, {newTab: true});
+ apiCommandExec(command, {mode: 'newTab'});
e.preventDefault();
}, false);