From 0e52a1f509155673d26a2a1ad00987389a05fea6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 18 Dec 2021 12:38:39 -0500 Subject: [PATCH] Scanning wildcards (#2042) * Add scanning.matchTypePrefix option * Update TextScanner to use matchTypePrefix option * Add setting * Update warning --- ext/data/schemas/options-schema.json | 7 ++++++- ext/js/app/frontend.js | 1 + ext/js/data/options-util.js | 12 +++++++++++- ext/js/display/display.js | 1 + ext/js/language/text-scanner.js | 11 +++++++++-- ext/settings.html | 26 ++++++++++++++++++++++++++ test/test-options-util.js | 3 ++- 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/ext/data/schemas/options-schema.json b/ext/data/schemas/options-schema.json index 2dd3981d..d15b533b 100644 --- a/ext/data/schemas/options-schema.json +++ b/ext/data/schemas/options-schema.json @@ -435,7 +435,8 @@ "enableOnPopupExpressions", "enableOnSearchPage", "enableSearchTags", - "layoutAwareScan" + "layoutAwareScan", + "matchTypePrefix" ], "properties": { "inputs": { @@ -658,6 +659,10 @@ "layoutAwareScan": { "type": "boolean", "default": false + }, + "matchTypePrefix": { + "type": "boolean", + "default": false } } }, diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index 3b482ad2..97d9e750 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -397,6 +397,7 @@ class Frontend { pointerEventsEnabled: scanningOptions.pointerEventsEnabled, scanLength: scanningOptions.length, layoutAwareScan: scanningOptions.layoutAwareScan, + matchTypePrefix: scanningOptions.matchTypePrefix, preventMiddleMouse, sentenceParsingOptions }); diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index c8ab2d01..7a725005 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -464,7 +464,8 @@ class OptionsUtil { {async: true, update: this._updateVersion12.bind(this)}, {async: true, update: this._updateVersion13.bind(this)}, {async: false, update: this._updateVersion14.bind(this)}, - {async: false, update: this._updateVersion15.bind(this)} + {async: false, update: this._updateVersion15.bind(this)}, + {async: false, update: this._updateVersion16.bind(this)} ]; if (typeof targetVersion === 'number' && targetVersion < result.length) { result.splice(targetVersion); @@ -888,4 +889,13 @@ class OptionsUtil { } return options; } + + _updateVersion16(options) { + // Version 16 changes: + // Added scanning.matchTypePrefix. + for (const profile of options.profiles) { + profile.options.scanning.matchTypePrefix = false; + } + return options; + } } diff --git a/ext/js/display/display.js b/ext/js/display/display.js index cd60488d..42b7c111 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -325,6 +325,7 @@ class Display extends EventDispatcher { scanLength: scanningOptions.length, layoutAwareScan: scanningOptions.layoutAwareScan, preventMiddleMouse: scanningOptions.preventMiddleMouse.onSearchQuery, + matchTypePrefix: false, sentenceParsingOptions } }); diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index 5d37c84b..cdcec257 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -61,6 +61,7 @@ class TextScanner extends EventDispatcher { this._scanLength = 1; this._layoutAwareScan = false; this._preventMiddleMouse = false; + this._matchTypePrefix = false; this._sentenceScanExtent = 0; this._sentenceTerminateAtNewlines = true; this._sentenceTerminatorMap = new Map(); @@ -155,7 +156,8 @@ class TextScanner extends EventDispatcher { scanLength, layoutAwareScan, preventMiddleMouse, - sentenceParsingOptions + sentenceParsingOptions, + matchTypePrefix }) { if (Array.isArray(inputs)) { this._inputs = inputs.map(({ @@ -210,6 +212,9 @@ class TextScanner extends EventDispatcher { if (typeof preventMiddleMouse === 'boolean') { this._preventMiddleMouse = preventMiddleMouse; } + if (typeof matchTypePrefix === 'boolean') { + this._matchTypePrefix = matchTypePrefix; + } if (typeof sentenceParsingOptions === 'object' && sentenceParsingOptions !== null) { const {scanExtent, terminationCharacterMode, terminationCharacters} = sentenceParsingOptions; if (typeof scanExtent === 'number') { @@ -854,7 +859,9 @@ class TextScanner extends EventDispatcher { const searchText = this.getTextSourceContent(textSource, scanLength, layoutAwareScan); if (searchText.length === 0) { return null; } - const {dictionaryEntries, originalTextLength} = await yomichan.api.termsFind(searchText, {}, optionsContext); + const details = {}; + if (this._matchTypePrefix) { details.matchType = 'prefix'; } + const {dictionaryEntries, originalTextLength} = await yomichan.api.termsFind(searchText, details, optionsContext); if (dictionaryEntries.length === 0) { return null; } textSource.setEndOffset(originalTextLength, layoutAwareScan); diff --git a/ext/settings.html b/ext/settings.html index 9309c889..6452a335 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -484,6 +484,32 @@ +
+
+
+
Wildcard scanning
+
+ Enable suffix wildcard when looking up scanned webpage text. + More… +
+
+
+ +
+
+ +
Text scan length
diff --git a/test/test-options-util.js b/test/test-options-util.js index 70229683..b1d56b28 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -344,6 +344,7 @@ function createProfileOptionsUpdatedTestData1() { layoutAwareScan: false, hideDelay: 0, pointerEventsEnabled: false, + matchTypePrefix: false, preventMiddleMouse: { onWebPages: false, onPopupPages: false, @@ -595,7 +596,7 @@ function createOptionsUpdatedTestData1() { } ], profileCurrent: 0, - version: 15, + version: 16, global: { database: { prefixWildcardsSupported: false