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)}],
['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.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([
]);
@ -727,6 +728,10 @@ class Backend {
return true;
}
_onApiTextHasJapaneseCharacters({text}) {
return this._japaneseUtil.isStringPartiallyJapanese(text);
}
// Command handlers
async _onCommandOpenSearchPage(params) {

View File

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

View File

@ -543,7 +543,7 @@ class Display extends EventDispatcher {
case 'kanji':
{
let query = urlSearchParams.get('query');
if (!query) { break; }
if (query === null) { break; }
this._query = query;
clear = false;
@ -882,7 +882,7 @@ class Display extends EventDispatcher {
let {definitions} = content;
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; }
content.definitions = definitions;
changeHistory = true;

View File

@ -17,6 +17,7 @@
/* global
* DocumentUtil
* TextSourceElement
*/
class TextScanner extends EventDispatcher {
@ -321,6 +322,12 @@ class TextScanner extends EventDispatcher {
({definitions, sentence, type} = result);
this._inputInfoCurrent = inputInfo;
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) {
error = e;
@ -978,4 +985,12 @@ class TextScanner extends EventDispatcher {
}
}
}
async _hasJapanese(text) {
try {
return await yomichan.api.textHasJapaneseCharacters(text);
} catch (e) {
return false;
}
}
}