diff options
-rw-r--r-- | ext/bg/js/api.js | 2 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 7 | ||||
-rw-r--r-- | ext/fg/js/api.js | 4 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 30 | ||||
-rw-r--r-- | ext/fg/js/source.js | 2 | ||||
-rw-r--r-- | ext/fg/js/util.js | 5 | ||||
-rw-r--r-- | ext/manifest.json | 6 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 8 |
8 files changed, 50 insertions, 14 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index c33ba709..4b2bacd7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -64,7 +64,7 @@ async function apiDefinitionAdd(definition, mode, context) { ); } - if (context.screenshot) { + if (context && context.screenshot) { await apiInjectScreenshot( definition, options.anki.terms.fields, diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d49286d0..d95cb82d 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -57,9 +57,10 @@ class Backend { this.anki = new AnkiNull(); } + const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, () => null); + chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, callback); } }); } @@ -147,6 +148,10 @@ class Backend { chrome.browserAction.setBadgeText({text}); } } + + checkLastError(e) { + // NOP + } } window.yomichan_backend = new Backend(); diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index 0c86b412..99ad307c 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -49,6 +49,10 @@ function apiTemplateRender(template, data, dynamic) { return utilInvoke('templateRender', {data, template, dynamic}); } +function apiAudioGetUrl(definition, source) { + return utilInvoke('audioGetUrl', {definition, source}); +} + function apiCommandExec(command) { return utilInvoke('commandExec', {command}); } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 25dd38e1..3c5f2ac8 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -172,7 +172,12 @@ class Frontend { return; } - this.setPrimaryTouch(this.getPrimaryTouch(e.changedTouches)); + let touch = this.getPrimaryTouch(e.changedTouches); + if (this.selectionContainsPoint(window.getSelection(), touch.clientX, touch.clientY)) { + touch = null; + } + + this.setPrimaryTouch(touch); } onTouchEnd(e) { @@ -289,7 +294,8 @@ class Frontend { if (!hideResults && (!this.textSourceLast || !this.textSourceLast.equals(textSource))) { searched = true; this.pendingLookup = true; - hideResults = !await this.searchTerms(textSource) && !await this.searchKanji(textSource); + const focus = (type === 'mouse'); + hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); success = true; } } catch (e) { @@ -312,7 +318,7 @@ class Frontend { } } - async searchTerms(textSource) { + async searchTerms(textSource, focus) { textSource.setEndOffset(this.options.scanning.length); const {definitions, length} = await apiTermsFind(textSource.text()); @@ -328,7 +334,7 @@ class Frontend { textSource.getRect(), definitions, this.options, - {sentence, url} + {sentence, url, focus} ); this.textSourceLast = textSource; @@ -339,7 +345,7 @@ class Frontend { return true; } - async searchKanji(textSource) { + async searchKanji(textSource, focus) { textSource.setEndOffset(1); const definitions = await apiKanjiFind(textSource.text()); @@ -353,7 +359,7 @@ class Frontend { textSource.getRect(), definitions, this.options, - {sentence, url} + {sentence, url, focus} ); this.textSourceLast = textSource; @@ -456,6 +462,18 @@ class Frontend { search(); } + + selectionContainsPoint(selection, x, y) { + for (let i = 0; i < selection.rangeCount; ++i) { + const range = selection.getRangeAt(i); + for (const rect of range.getClientRects()) { + if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { + return true; + } + } + } + return false; + } } window.yomichan_frontend = new Frontend(); diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index bbf00e30..a360b331 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -180,7 +180,7 @@ class TextSourceRange { let consumed = 0; let stripped = 0; while (state.remainder - consumed > 0) { - const currentChar = node.nodeValue[offset - consumed - stripped]; // negative indices are undefined in JS + const currentChar = node.nodeValue[offset - 1 - consumed - stripped]; // negative indices are undefined in JS if (!currentChar) { break; } else if (currentChar.match(IGNORE_TEXT_PATTERN)) { diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js index 954b3988..7518beb5 100644 --- a/ext/fg/js/util.js +++ b/ext/fg/js/util.js @@ -27,6 +27,7 @@ function utilInvoke(action, params={}) { return new Promise((resolve, reject) => { try { chrome.runtime.sendMessage({action, params}, (response) => { + utilCheckLastError(chrome.runtime.lastError); if (response !== null && typeof response === 'object') { if (response.error) { reject(response.error); @@ -43,3 +44,7 @@ function utilInvoke(action, params={}) { } }); } + +function utilCheckLastError(e) { + // NOP +} diff --git a/ext/manifest.json b/ext/manifest.json index baec58f6..e12f85ad 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, - "name": "Yomichan (testing)", - "version": "1.6.2", + "name": "Yomichan", + "version": "1.7.4", "description": "Japanese dictionary with Anki integration (testing)", "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"}, @@ -58,7 +58,7 @@ "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "applications": { "gecko": { - "id": "alex.testing@foosoft.net", + "id": "alex@foosoft.net", "strict_min_version": "52.0" } } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 01cb406e..a2707bd0 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -272,7 +272,9 @@ class Display { async termsShow(definitions, options, context) { try { - window.focus(); + if (!context || context.focus !== false) { + window.focus(); + } this.definitions = definitions; this.options = options; @@ -324,7 +326,9 @@ class Display { async kanjiShow(definitions, options, context) { try { - window.focus(); + if (!context || context.focus !== false) { + window.focus(); + } this.definitions = definitions; this.options = options; |