diff options
Diffstat (limited to 'ext/bg/js/search.js')
-rw-r--r-- | ext/bg/js/search.js | 163 |
1 files changed, 82 insertions, 81 deletions
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 98e167ad..0a7a5fe1 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -39,6 +39,25 @@ class DisplaySearch extends Display { this.introAnimationTimer = null; this.clipboardMonitor = new ClipboardMonitor(); + + this._onKeyDownIgnoreKeys = new Map([ + ['ANY_MOD', new Set([ + 'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End', + 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', + 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', + 'F21', 'F22', 'F23', 'F24' + ])], + ['Control', new Set(['C', 'A', 'Z', 'Y', 'X', 'F', 'G'])], + ['Meta', new Set(['C', 'A', 'Z', 'Y', 'X', 'F', 'G'])], + ['OS', new Set()], + ['Alt', new Set()], + ['AltGraph', new Set()], + ['Shift', new Set()] + ]); + + this._runtimeMessageHandlers = new Map([ + ['searchQueryUpdate', ({query}) => { this.onExternalSearchUpdate(query); }] + ]); } static create() { @@ -55,70 +74,37 @@ class DisplaySearch extends Display { const {queryParams: {query='', mode=''}} = parseUrl(window.location.href); - if (this.search !== null) { - this.search.addEventListener('click', (e) => this.onSearch(e), false); - } - if (this.query !== null) { - document.documentElement.dataset.searchMode = mode; - this.query.addEventListener('input', () => this.onSearchInput(), false); - - if (this.wanakanaEnable !== null) { - if (this.options.general.enableWanakana === true) { - this.wanakanaEnable.checked = true; - window.wanakana.bind(this.query); - } else { - this.wanakanaEnable.checked = false; - } - this.wanakanaEnable.addEventListener('change', (e) => { - const {queryParams: {query: query2=''}} = parseUrl(window.location.href); - if (e.target.checked) { - window.wanakana.bind(this.query); - apiOptionsSet({general: {enableWanakana: true}}, this.getOptionsContext()); - } else { - window.wanakana.unbind(this.query); - apiOptionsSet({general: {enableWanakana: false}}, this.getOptionsContext()); - } - this.setQuery(query2); - this.onSearchQueryUpdated(this.query.value, false); - }); - } + document.documentElement.dataset.searchMode = mode; - this.setQuery(query); - this.onSearchQueryUpdated(this.query.value, false); + if (this.options.general.enableWanakana === true) { + this.wanakanaEnable.checked = true; + window.wanakana.bind(this.query); + } else { + this.wanakanaEnable.checked = false; } - if (this.clipboardMonitorEnable !== null && mode !== 'popup') { + + this.setQuery(query); + this.onSearchQueryUpdated(this.query.value, false); + + if (mode !== 'popup') { if (this.options.general.enableClipboardMonitor === true) { this.clipboardMonitorEnable.checked = true; this.clipboardMonitor.start(); } else { this.clipboardMonitorEnable.checked = false; } - this.clipboardMonitorEnable.addEventListener('change', (e) => { - if (e.target.checked) { - chrome.permissions.request( - {permissions: ['clipboardRead']}, - (granted) => { - if (granted) { - this.clipboardMonitor.start(); - apiOptionsSet({general: {enableClipboardMonitor: true}}, this.getOptionsContext()); - } else { - e.target.checked = false; - } - } - ); - } else { - this.clipboardMonitor.stop(); - apiOptionsSet({general: {enableClipboardMonitor: false}}, this.getOptionsContext()); - } - }); + this.clipboardMonitorEnable.addEventListener('change', this.onClipboardMonitorEnableChange.bind(this)); } chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this)); - window.addEventListener('popstate', (e) => this.onPopState(e)); - window.addEventListener('copy', (e) => this.onCopy(e)); + this.search.addEventListener('click', this.onSearch.bind(this), false); + this.query.addEventListener('input', this.onSearchInput.bind(this), false); + this.wanakanaEnable.addEventListener('change', this.onWanakanaEnableChange.bind(this)); + window.addEventListener('popstate', this.onPopState.bind(this)); + window.addEventListener('copy', this.onCopy.bind(this)); - this.clipboardMonitor.onClipboardText = (text) => this.onExternalSearchUpdate(text); + this.clipboardMonitor.onClipboardText = this.onExternalSearchUpdate.bind(this); this.updateSearchButton(); } catch (e) { @@ -174,28 +160,30 @@ class DisplaySearch extends Display { } onRuntimeMessage({action, params}, sender, callback) { - const handler = DisplaySearch._runtimeMessageHandlers.get(action); + const handler = this._runtimeMessageHandlers.get(action); if (typeof handler !== 'function') { return false; } - const result = handler(this, params, sender); + const result = handler(params, sender); callback(result); return false; } onKeyDown(e) { const key = Display.getKeyFromEvent(e); - const ignoreKeys = DisplaySearch.onKeyDownIgnoreKeys; + const ignoreKeys = this._onKeyDownIgnoreKeys; - const activeModifierMap = { - 'Control': e.ctrlKey, - 'Meta': e.metaKey, - 'ANY_MOD': true - }; + const activeModifierMap = new Map([ + ['Control', e.ctrlKey], + ['Meta', e.metaKey], + ['Shift', e.shiftKey], + ['Alt', e.altKey], + ['ANY_MOD', true] + ]); let preventFocus = false; - for (const [modifier, keys] of Object.entries(ignoreKeys)) { - const modifierActive = activeModifierMap[modifier]; - if (key === modifier || (modifierActive && keys.includes(key))) { + for (const [modifier, keys] of ignoreKeys.entries()) { + const modifierActive = activeModifierMap.get(modifier); + if (key === modifier || (modifierActive && keys.has(key))) { preventFocus = true; break; } @@ -253,6 +241,38 @@ class DisplaySearch extends Display { } } + onWanakanaEnableChange(e) { + const {queryParams: {query=''}} = parseUrl(window.location.href); + const enableWanakana = e.target.checked; + if (enableWanakana) { + window.wanakana.bind(this.query); + } else { + window.wanakana.unbind(this.query); + } + this.setQuery(query); + this.onSearchQueryUpdated(this.query.value, false); + apiOptionsSet({general: {enableWanakana}}, this.getOptionsContext()); + } + + onClipboardMonitorEnableChange(e) { + if (e.target.checked) { + chrome.permissions.request( + {permissions: ['clipboardRead']}, + (granted) => { + if (granted) { + this.clipboardMonitor.start(); + apiOptionsSet({general: {enableClipboardMonitor: true}}, this.getOptionsContext()); + } else { + e.target.checked = false; + } + } + ); + } else { + this.clipboardMonitor.stop(); + apiOptionsSet({general: {enableClipboardMonitor: false}}, this.getOptionsContext()); + } + } + async updateOptions(options) { await super.updateOptions(options); this.queryParser.setOptions(this.options); @@ -346,23 +366,4 @@ class DisplaySearch extends Display { } } -DisplaySearch.onKeyDownIgnoreKeys = { - 'ANY_MOD': [ - 'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End', - 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', - 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'F20', - 'F21', 'F22', 'F23', 'F24' - ], - 'Control': ['C', 'A', 'Z', 'Y', 'X', 'F', 'G'], - 'Meta': ['C', 'A', 'Z', 'Y', 'X', 'F', 'G'], - 'OS': [], - 'Alt': [], - 'AltGraph': [], - 'Shift': [] -}; - -DisplaySearch._runtimeMessageHandlers = new Map([ - ['searchQueryUpdate', (self, {query}) => { self.onExternalSearchUpdate(query); }] -]); - DisplaySearch.instance = DisplaySearch.create(); |