Text source element scanning improvements (#1464)

* Add api.textHasJapaneseCharacters

* Add support for empty query

* Add support for showing the query parser for TextSourceElement
with content that does not start with Japanese text
This commit is contained in:
toasted-nutbread 2021-02-28 14:18:18 -05:00 committed by GitHub
parent 6f76645f4c
commit 4806414f95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

View File

@ -127,7 +127,8 @@ class Backend {
['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}], ['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}],
['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}], ['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}],
['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}], ['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}],
['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}] ['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}],
['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}]
]); ]);
this._messageHandlersWithProgress = new Map([ this._messageHandlersWithProgress = new Map([
]); ]);
@ -727,6 +728,10 @@ class Backend {
return true; return true;
} }
_onApiTextHasJapaneseCharacters({text}) {
return this._japaneseUtil.isStringPartiallyJapanese(text);
}
// Command handlers // Command handlers
async _onCommandOpenSearchPage(params) { async _onCommandOpenSearchPage(params) {

View File

@ -168,6 +168,10 @@ class API {
return this._invoke('testMecab', {}); return this._invoke('testMecab', {});
} }
textHasJapaneseCharacters(text) {
return this._invoke('textHasJapaneseCharacters', {text});
}
// Utilities // Utilities
_createActionPort(timeout=5000) { _createActionPort(timeout=5000) {

View File

@ -543,7 +543,7 @@ class Display extends EventDispatcher {
case 'kanji': case 'kanji':
{ {
let query = urlSearchParams.get('query'); let query = urlSearchParams.get('query');
if (!query) { break; } if (query === null) { break; }
this._query = query; this._query = query;
clear = false; clear = false;
@ -882,7 +882,7 @@ class Display extends EventDispatcher {
let {definitions} = content; let {definitions} = content;
if (!Array.isArray(definitions)) { if (!Array.isArray(definitions)) {
definitions = lookup ? await this._findDefinitions(isTerms, query, wildcardsEnabled, optionsContext) : []; definitions = lookup && query.length > 0 ? await this._findDefinitions(isTerms, query, wildcardsEnabled, optionsContext) : [];
if (this._setContentToken !== token) { return; } if (this._setContentToken !== token) { return; }
content.definitions = definitions; content.definitions = definitions;
changeHistory = true; changeHistory = true;

View File

@ -17,6 +17,7 @@
/* global /* global
* DocumentUtil * DocumentUtil
* TextSourceElement
*/ */
class TextScanner extends EventDispatcher { class TextScanner extends EventDispatcher {
@ -321,6 +322,12 @@ class TextScanner extends EventDispatcher {
({definitions, sentence, type} = result); ({definitions, sentence, type} = result);
this._inputInfoCurrent = inputInfo; this._inputInfoCurrent = inputInfo;
this.setCurrentTextSource(textSource); this.setCurrentTextSource(textSource);
} else if (textSource instanceof TextSourceElement && await this._hasJapanese(textSource.fullContent)) {
definitions = [];
sentence = {sentence: '', offset: 0};
type = 'terms';
this._inputInfoCurrent = inputInfo;
this.setCurrentTextSource(textSource);
} }
} catch (e) { } catch (e) {
error = e; error = e;
@ -978,4 +985,12 @@ class TextScanner extends EventDispatcher {
} }
} }
} }
async _hasJapanese(text) {
try {
return await yomichan.api.textHasJapaneseCharacters(text);
} catch (e) {
return false;
}
}
} }