Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmaa committed Jun 4, 2024
1 parent 664902d commit 8f504f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
7 changes: 4 additions & 3 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class Frontend {
* @returns {void}
*/
_onApiScanSelectedText() {
void this._scanSelectedText(false, true);
void this._scanSelectedText(false, true, true);
}

/**
Expand Down Expand Up @@ -942,13 +942,14 @@ export class Frontend {
/**
* @param {boolean} allowEmptyRange
* @param {boolean} disallowExpandSelection
* @param {boolean} showEmpty show empty popup if no results are found
* @returns {Promise<boolean>}
*/
async _scanSelectedText(allowEmptyRange, disallowExpandSelection) {
async _scanSelectedText(allowEmptyRange, disallowExpandSelection, showEmpty = false) {
const range = this._getFirstSelectionRange(allowEmptyRange);
if (range === null) { return false; }
const source = disallowExpandSelection ? TextSourceRange.createLazy(range) : TextSourceRange.create(range);
await this._textScanner.search(source, {focus: true, restoreSelection: true});
await this._textScanner.search(source, {focus: true, restoreSelection: true}, showEmpty);
return true;
}

Expand Down
20 changes: 10 additions & 10 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,6 @@ export class Backend {

this._sendMessageAllTabsIgnoreResponse({action: 'applicationBackendReady'});
this._sendMessageIgnoreResponse({action: 'applicationBackendReady'});
chrome.contextMenus.create({
id: 'yomitan_search',
title: 'Lookup in yomitan',
contexts: ['all'],
});
chrome.contextMenus.onClicked.addListener((info) => {
if (info.selectionText) {
this._sendMessageAllTabsIgnoreResponse({action: 'frontendScanSelectedText'});
}
});
} catch (e) {
log.error(e);
throw e;
Expand Down Expand Up @@ -437,6 +427,16 @@ export class Backend {
* @param {chrome.runtime.InstalledDetails} event
*/
_onInstalled({reason}) {
chrome.contextMenus.create({
id: 'yomitan_search',
title: 'Lookup in yomitan',
contexts: ['selection'],
});
chrome.contextMenus.onClicked.addListener((info) => {
if (info.selectionText) {
this._sendMessageAllTabsIgnoreResponse({action: 'frontendScanSelectedText'});
}
});
if (reason !== 'install') { return; }
void this._requestPersistentStorage();
}
Expand Down
13 changes: 9 additions & 4 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,11 @@ export class TextScanner extends EventDispatcher {
/**
* @param {import('text-source').TextSource} textSource
* @param {import('text-scanner').InputInfoDetail} [inputDetail]
* @param {boolean} showEmpty
*/
async search(textSource, inputDetail) {
async search(textSource, inputDetail, showEmpty = false) {
const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail);
await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo, showEmpty);
}

// Private
Expand All @@ -437,8 +438,9 @@ export class TextScanner extends EventDispatcher {
* @param {boolean} searchTerms
* @param {boolean} searchKanji
* @param {import('text-scanner').InputInfo} inputInfo
* @param {boolean} showEmpty shows a "No results found" popup if no results are found
*/
async _search(textSource, searchTerms, searchKanji, inputInfo) {
async _search(textSource, searchTerms, searchKanji, inputInfo, showEmpty = false) {
try {
const inputInfoDetail = inputInfo.detail;
const selectionRestoreInfo = (
Expand All @@ -465,12 +467,14 @@ export class TextScanner extends EventDispatcher {
const result = await this._findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext);
if (result !== null) {
({dictionaryEntries, sentence, type} = result);
} else if (textSource !== null && textSource instanceof TextSourceElement && await this._isTextLookupWorthy(textSource.fullContent)) {
} else if (showEmpty || (textSource !== null && textSource instanceof TextSourceElement && await this._isTextLookupWorthy(textSource.fullContent))) {
// Shows a "No results found" message
dictionaryEntries = [];
sentence = {text: '', offset: 0};
}

if (dictionaryEntries !== null && sentence !== null) {
console.log('searchSuccess');

Check failure on line 477 in ext/js/language/text-scanner.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Unexpected console statement
this._inputInfoCurrent = inputInfo;
this.setCurrentTextSource(textSource);
this._selectionRestoreInfo = selectionRestoreInfo;
Expand All @@ -485,6 +489,7 @@ export class TextScanner extends EventDispatcher {
detail,
});
} else {
console.log('searchEmpty');

Check failure on line 492 in ext/js/language/text-scanner.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Unexpected console statement
this._triggerSearchEmpty(inputInfo);
}
} catch (error) {
Expand Down

0 comments on commit 8f504f4

Please sign in to comment.