From 0f3447a0e6b74318aab4dff4d570a4fb065da6d4 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Mon, 1 Jul 2024 21:02:07 -0700 Subject: [PATCH 1/6] Add Option for Scanning Alt Text --- ext/js/app/frontend.js | 1 + ext/js/data/options-util.js | 1 + ext/js/display/display.js | 2 ++ ext/js/language/text-scanner.js | 6 ++++++ ext/settings.html | 9 +++++++++ test/options-util.test.js | 2 ++ types/ext/settings.d.ts | 1 + types/ext/text-scanner.d.ts | 1 + 8 files changed, 23 insertions(+) diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index 8ac279798c..c3022908d1 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -513,6 +513,7 @@ export class Frontend { matchTypePrefix: scanningOptions.matchTypePrefix, preventMiddleMouse, sentenceParsingOptions, + scanAltText: scanningOptions.scanAltText, }); this._updateTextScannerEnabled(); diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index 8af299d8e2..bd409fcab6 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -349,6 +349,7 @@ export class OptionsUtil { enableOnSearchPage: true, enableSearchTags: false, layoutAwareScan: false, + scanAltText: true, }, translation: { diff --git a/ext/js/display/display.js b/ext/js/display/display.js index 3d18e41609..014063826c 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -461,6 +461,7 @@ export class Display extends EventDispatcher { preventMiddleMouse: scanningOptions.preventMiddleMouse.onSearchQuery, matchTypePrefix: false, sentenceParsingOptions, + scanAltText: scanningOptions.scanAltText, }, }); @@ -1984,6 +1985,7 @@ export class Display extends EventDispatcher { layoutAwareScan: scanningOptions.layoutAwareScan, preventMiddleMouse: false, sentenceParsingOptions, + scanAltText: scanningOptions.scanAltText, }); this._contentTextScanner.setEnabled(true); diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index 249a2eda3d..8582ea6600 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -124,6 +124,8 @@ export class TextScanner extends EventDispatcher { this._sentenceBackwardQuoteMap = new Map(); /** @type {import('text-scanner').InputConfig[]} */ this._inputs = []; + /** @type {boolean} */ + this._scanAltText = true; /** @type {boolean} */ this._enabled = false; @@ -255,6 +257,7 @@ export class TextScanner extends EventDispatcher { preventMiddleMouse, sentenceParsingOptions, matchTypePrefix, + scanAltText, }) { if (Array.isArray(inputs)) { this._inputs = inputs.map((input) => this._convertInput(input)); @@ -289,6 +292,9 @@ export class TextScanner extends EventDispatcher { if (typeof matchTypePrefix === 'boolean') { this._matchTypePrefix = matchTypePrefix; } + if (typeof scanAltText === 'boolean') { + this._scanAltText = scanAltText; + } if (typeof sentenceParsingOptions === 'object' && sentenceParsingOptions !== null) { const {scanExtent, terminationCharacterMode, terminationCharacters} = sentenceParsingOptions; if (typeof scanExtent === 'number') { diff --git a/ext/settings.html b/ext/settings.html index dfd562a9ca..ccd6dc9b93 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -415,6 +415,15 @@

Yomitan Settings

+
+
+
Scan alt text
+
Scan text that is used for image and button descriptions.
+
+
+ +
+
diff --git a/test/options-util.test.js b/test/options-util.test.js index 253b965067..f3e52a1537 100644 --- a/test/options-util.test.js +++ b/test/options-util.test.js @@ -95,6 +95,7 @@ function createProfileOptionsTestData1() { enableOnSearchPage: true, enableSearchTags: false, layoutAwareScan: false, + scanAltText: true, }, translation: { convertHalfWidthCharacters: 'false', @@ -325,6 +326,7 @@ function createProfileOptionsUpdatedTestData1() { hidePopupOnCursorExit: false, hidePopupOnCursorExitDelay: 0, normalizeCssZoom: true, + scanAltText: true, preventMiddleMouse: { onWebPages: false, onPopupPages: false, diff --git a/types/ext/settings.d.ts b/types/ext/settings.d.ts index 559a27c425..b84942b174 100644 --- a/types/ext/settings.d.ts +++ b/types/ext/settings.d.ts @@ -190,6 +190,7 @@ export type ScanningOptions = { hidePopupOnCursorExit: boolean; hidePopupOnCursorExitDelay: number; normalizeCssZoom: boolean; + scanAltText: boolean; }; export type ScanningInput = { diff --git a/types/ext/text-scanner.d.ts b/types/ext/text-scanner.d.ts index 4277d49afc..0af55d72a7 100644 --- a/types/ext/text-scanner.d.ts +++ b/types/ext/text-scanner.d.ts @@ -41,6 +41,7 @@ export type Options = { preventMiddleMouse?: boolean; matchTypePrefix?: boolean; sentenceParsingOptions?: SentenceParsingOptions; + scanAltText?: boolean; }; export type InputOptionsOuter = { From b88b97c054596c77a5a8bcc56b13412a3fe31be5 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Mon, 1 Jul 2024 21:12:08 -0700 Subject: [PATCH 2/6] Don't scan alt text if option disabled --- ext/js/language/text-scanner.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index 8582ea6600..759adf2047 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -449,6 +449,11 @@ export class TextScanner extends EventDispatcher { */ async _search(textSource, searchTerms, searchKanji, inputInfo, showEmpty = false) { try { + const isAltText = textSource instanceof TextSourceElement; + if (isAltText && !this._scanAltText) { + return; + } + const inputInfoDetail = inputInfo.detail; const selectionRestoreInfo = ( (typeof inputInfoDetail === 'object' && inputInfoDetail !== null && inputInfoDetail.restoreSelection) ? @@ -474,7 +479,7 @@ export class TextScanner extends EventDispatcher { const result = await this._findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext); if (result !== null) { ({dictionaryEntries, sentence, type} = result); - } else if (showEmpty || (textSource !== null && textSource instanceof TextSourceElement && await this._isTextLookupWorthy(textSource.fullContent))) { + } else if (showEmpty || (textSource !== null && isAltText && await this._isTextLookupWorthy(textSource.fullContent))) { // Shows a "No results found" message dictionaryEntries = []; sentence = {text: '', offset: 0}; From 8d3b19d46c1f1b50f767b553237b18f496ee697f Mon Sep 17 00:00:00 2001 From: MarvNC Date: Tue, 2 Jul 2024 19:30:16 -0700 Subject: [PATCH 3/6] Add scanAltText to Options Schema --- ext/data/schemas/options-schema.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/data/schemas/options-schema.json b/ext/data/schemas/options-schema.json index 2fb1c9c383..85e02c021d 100644 --- a/ext/data/schemas/options-schema.json +++ b/ext/data/schemas/options-schema.json @@ -437,6 +437,7 @@ "hideDelay", "length", "deepDomScan", + "scanAltText", "popupNestingMaxDepth", "enablePopupSearch", "enableOnPopupExpressions", @@ -688,6 +689,10 @@ "type": "boolean", "default": false }, + "scanAltText": { + "type": "boolean", + "default": true + }, "popupNestingMaxDepth": { "type": "integer", "minimum": 0, From c8c77b8fb29d2bf5312472ee39265af55298fde1 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Tue, 2 Jul 2024 19:34:34 -0700 Subject: [PATCH 4/6] Add `scanning.scanAltText` Update Function --- ext/js/data/options-util.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index bd409fcab6..5f63acb76c 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -550,6 +550,7 @@ export class OptionsUtil { this._updateVersion39, this._updateVersion40, this._updateVersion41, + this._updateVersion42, ]; /* eslint-enable @typescript-eslint/unbound-method */ if (typeof targetVersion === 'number' && targetVersion < result.length) { @@ -1349,6 +1350,17 @@ export class OptionsUtil { await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v41.handlebars'); } + /** + * - Added scanning.scanAltText + * @type {import('options-util').UpdateFunction} + * @param {import('settings').Options} options + */ + async _updateVersion42(options) { + for (const profile of options.profiles) { + profile.options.scanning.scanAltText = true; + } + } + /** * @param {string} url * @returns {Promise} From 81d953ed285d6e737cc8f2f367e9228753190a49 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Tue, 2 Jul 2024 21:14:56 -0700 Subject: [PATCH 5/6] Bump Options Test Version --- test/options-util.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/options-util.test.js b/test/options-util.test.js index f3e52a1537..60d44470f9 100644 --- a/test/options-util.test.js +++ b/test/options-util.test.js @@ -608,7 +608,7 @@ function createOptionsUpdatedTestData1() { }, ], profileCurrent: 0, - version: 41, + version: 42, global: { database: { prefixWildcardsSupported: false, From 9d764552e8c001e00ad11687eaf33f1ca375dddd Mon Sep 17 00:00:00 2001 From: MarvNC Date: Wed, 3 Jul 2024 02:57:03 -0700 Subject: [PATCH 6/6] Remove Param Annotation --- ext/js/data/options-util.js | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index 5f63acb76c..850ac81d75 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -1353,7 +1353,6 @@ export class OptionsUtil { /** * - Added scanning.scanAltText * @type {import('options-util').UpdateFunction} - * @param {import('settings').Options} options */ async _updateVersion42(options) { for (const profile of options.profiles) {