update popup search with chrome.tabs.sendMessage

This commit is contained in:
siikamiika 2020-02-10 01:16:06 +02:00
parent 89729d8c20
commit 460d306f60
2 changed files with 23 additions and 4 deletions

View File

@ -619,7 +619,9 @@ class Backend {
if (tab !== null) { if (tab !== null) {
await Backend._focusTab(tab); await Backend._focusTab(tab);
if (queryParams.query) { if (queryParams.query) {
await new Promise((resolve) => chrome.tabs.update(tab.id, {url}, resolve)); await new Promise((resolve) => chrome.tabs.sendMessage(
tab.id, {action: 'searchQueryUpdate', params: {query: queryParams.query}}, resolve
));
} }
return true; return true;
} }

View File

@ -109,10 +109,12 @@ class DisplaySearch extends Display {
}); });
} }
chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
window.addEventListener('popstate', (e) => this.onPopState(e)); window.addEventListener('popstate', (e) => this.onPopState(e));
window.addEventListener('copy', (e) => this.onCopy(e)); window.addEventListener('copy', (e) => this.onCopy(e));
this.clipboardMonitor.onClipboardText = (text) => this.onClipboardText(text); this.clipboardMonitor.onClipboardText = (text) => this.onExternalSearchUpdate(text);
this.updateSearchButton(); this.updateSearchButton();
} catch (e) { } catch (e) {
@ -163,6 +165,15 @@ class DisplaySearch extends Display {
this.onSearchQueryUpdated(this.query.value, false); this.onSearchQueryUpdated(this.query.value, false);
} }
onRuntimeMessage({action, params}, sender, callback) {
const handler = DisplaySearch._runtimeMessageHandlers.get(action);
if (typeof handler !== 'function') { return false; }
const result = handler(this, params, sender);
callback(result);
return false;
}
onKeyDown(e) { onKeyDown(e) {
const key = Display.getKeyFromEvent(e); const key = Display.getKeyFromEvent(e);
const ignoreKeys = DisplaySearch.onKeyDownIgnoreKeys; const ignoreKeys = DisplaySearch.onKeyDownIgnoreKeys;
@ -192,9 +203,11 @@ class DisplaySearch extends Display {
this.clipboardMonitor.setPreviousText(document.getSelection().toString().trim()); this.clipboardMonitor.setPreviousText(document.getSelection().toString().trim());
} }
onClipboardText(text) { onExternalSearchUpdate(text) {
this.setQuery(text); this.setQuery(text);
window.history.pushState(null, '', `${window.location.pathname}?query=${encodeURIComponent(text)}`); const url = new URL(window.location.href);
url.searchParams.set('query', text);
window.history.pushState(null, '', url.toString());
this.onSearchQueryUpdated(this.query.value, true); this.onSearchQueryUpdated(this.query.value, true);
} }
@ -340,4 +353,8 @@ DisplaySearch.onKeyDownIgnoreKeys = {
'Shift': [] 'Shift': []
}; };
DisplaySearch._runtimeMessageHandlers = new Map([
['searchQueryUpdate', (self, {query}) => { self.onExternalSearchUpdate(query); }]
]);
DisplaySearch.instance = DisplaySearch.create(); DisplaySearch.instance = DisplaySearch.create();