From a716a52cab143ae2e02b54c2f075a8731de16193 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 01:26:56 +0300 Subject: make non-hotkey keys focus input on search page The issue was that scanning on search page introduced a way to lose focus of the query input, and the new feature that the search page hotkey focuses an existing search page instead of opening a new one made it more obvious. Now every key that isn't a hotkey focuses the query input, and typing text into the box scrolls it into view in case it wasn't there when searching. There is an accessibility issue that this can cause, because now tab also focuses the query input before it focuses the next element. I didn't implement a workaround for that because it would have been more complicated than this simple fix. Fixes #263 --- ext/mixed/js/display.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ext/mixed') diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index b40228b0..bdc5e962 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -157,8 +157,10 @@ class Display { const handler = handlers[key]; if (handler(this, e)) { e.preventDefault(); + return true; } } + return false; } onWheel(e) { -- cgit v1.2.3 From 1039536cfb6279aadb9241c5905aa64790cd6fac Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 19:35:41 -0400 Subject: Create promise version of setTimeout --- ext/mixed/js/extension.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'ext/mixed') diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js index 861e52a5..54862e19 100644 --- a/ext/mixed/js/extension.js +++ b/ext/mixed/js/extension.js @@ -95,3 +95,41 @@ if (EXTENSION_IS_BROWSER_EDGE) { // Edge does not have chrome defined. chrome = browser; } + +function promiseTimeout(delay, resolveValue) { + if (delay <= 0) { + return Promise.resolve(resolveValue); + } + + let timer = null; + let promiseResolve = null; + let promiseReject = null; + + const complete = (callback, value) => { + if (callback === null) { return; } + if (timer !== null) { + window.clearTimeout(timer); + timer = null; + } + promiseResolve = null; + promiseReject = null; + callback(value); + }; + + const resolve = (value) => complete(promiseResolve, value); + const reject = (value) => complete(promiseReject, value); + + const promise = new Promise((resolve, reject) => { + promiseResolve = resolve; + promiseReject = reject; + }); + timer = window.setTimeout(() => { + timer = null; + resolve(resolveValue); + }, delay); + + promise.resolve = resolve; + promise.reject = reject; + + return promise; +} -- cgit v1.2.3 From f4a987912a3bc7012cbfeaaa35ea94e5f97655e0 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 3 Nov 2019 18:56:22 +0200 Subject: prevent accidental lookup on glossary text select --- ext/fg/js/frontend.js | 24 +++++++++++++++++++----- ext/mixed/js/display.js | 21 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'ext/mixed') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 802221be..84d6af28 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -80,10 +80,7 @@ class Frontend { onMouseMove(e) { this.popupTimerClear(); - if ( - this.pendingLookup || - (e.buttons & 0x1) !== 0x0 // Left mouse button - ) { + if (this.pendingLookup || Frontend.isMouseButton('primary', e)) { return; } @@ -91,7 +88,7 @@ class Frontend { const scanningModifier = scanningOptions.modifier; if (!( Frontend.isScanningModifierPressed(scanningModifier, e) || - (scanningOptions.middleMouse && (e.buttons & 0x4) !== 0x0) // Middle mouse button + (scanningOptions.middleMouse && Frontend.isMouseButton('auxiliary', e)) )) { return; } @@ -498,6 +495,23 @@ class Frontend { default: return false; } } + + static isMouseButton(button, mouseEvent) { + if (['mouseup', 'mousedown', 'click'].includes(mouseEvent.type)) { + switch (button) { + case 'primary': return mouseEvent.button === 0; + case 'secondary': return mouseEvent.button === 2; + case 'auxiliary': return mouseEvent.button === 1; + default: return false; + } + } + switch (button) { + case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; + case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; + case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; + default: return false; + } + } } Frontend.windowMessageHandlers = { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index bdc5e962..81072957 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -35,6 +35,7 @@ class Display { this.persistentEventListeners = []; this.interactive = false; this.eventListenersActive = false; + this.clickScanPrevent = false; this.windowScroll = new WindowScroll(); @@ -81,6 +82,22 @@ class Display { } } + onGlossaryMousedown(e) { + if (Frontend.isMouseButton('primary', e)) { + this.clickScanPrevent = false; + } + } + + onGlossaryMouseMove(e) { + this.clickScanPrevent = true; + } + + onGlossaryMouseup(e) { + if (!this.clickScanPrevent && Frontend.isMouseButton('primary', e)) { + this.onTermLookup(e); + } + } + async onTermLookup(e) { try { e.preventDefault(); @@ -252,7 +269,9 @@ class Display { this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this)); this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this)); if (this.options.scanning.enablePopupSearch) { - this.addEventListeners('.glossary-item', 'click', this.onTermLookup.bind(this)); + this.addEventListeners('.glossary-item', 'mouseup', this.onGlossaryMouseup.bind(this)); + this.addEventListeners('.glossary-item', 'mousedown', this.onGlossaryMousedown.bind(this)); + this.addEventListeners('.glossary-item', 'mousemove', this.onGlossaryMouseMove.bind(this)); } } else { Display.clearEventListeners(this.eventListeners); -- cgit v1.2.3 From 83460bcdade28fe77908d3008444a23555f09487 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 4 Nov 2019 02:15:33 +0200 Subject: refactoring and optimization --- ext/fg/js/frontend.js | 18 ++++++++++-------- ext/mixed/js/display.js | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'ext/mixed') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 84d6af28..b9410f2c 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -497,19 +497,21 @@ class Frontend { } static isMouseButton(button, mouseEvent) { - if (['mouseup', 'mousedown', 'click'].includes(mouseEvent.type)) { - switch (button) { + switch (mouseEvent.type) { + case 'mouseup': + case 'mousedown': + case 'click': switch (button) { case 'primary': return mouseEvent.button === 0; case 'secondary': return mouseEvent.button === 2; case 'auxiliary': return mouseEvent.button === 1; default: return false; } - } - switch (button) { - case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; - case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; - case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; - default: return false; + default: switch (button) { + case 'primary': return (mouseEvent.buttons & 0x1) !== 0x0; + case 'secondary': return (mouseEvent.buttons & 0x2) !== 0x0; + case 'auxiliary': return (mouseEvent.buttons & 0x4) !== 0x0; + default: return false; + } } } } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 81072957..6d992897 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -82,7 +82,7 @@ class Display { } } - onGlossaryMousedown(e) { + onGlossaryMouseDown(e) { if (Frontend.isMouseButton('primary', e)) { this.clickScanPrevent = false; } @@ -92,7 +92,7 @@ class Display { this.clickScanPrevent = true; } - onGlossaryMouseup(e) { + onGlossaryMouseUp(e) { if (!this.clickScanPrevent && Frontend.isMouseButton('primary', e)) { this.onTermLookup(e); } @@ -269,8 +269,8 @@ class Display { this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this)); this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this)); if (this.options.scanning.enablePopupSearch) { - this.addEventListeners('.glossary-item', 'mouseup', this.onGlossaryMouseup.bind(this)); - this.addEventListeners('.glossary-item', 'mousedown', this.onGlossaryMousedown.bind(this)); + this.addEventListeners('.glossary-item', 'mouseup', this.onGlossaryMouseUp.bind(this)); + this.addEventListeners('.glossary-item', 'mousedown', this.onGlossaryMouseDown.bind(this)); this.addEventListeners('.glossary-item', 'mousemove', this.onGlossaryMouseMove.bind(this)); } } else { -- cgit v1.2.3