Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option for Scanning Alt Text #1169

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export class Frontend {
matchTypePrefix: scanningOptions.matchTypePrefix,
preventMiddleMouse,
sentenceParsingOptions,
scanAltText: scanningOptions.scanAltText,
});
this._updateTextScannerEnabled();

Expand Down
1 change: 1 addition & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export class OptionsUtil {
enableOnSearchPage: true,
enableSearchTags: false,
layoutAwareScan: false,
scanAltText: true,
},

translation: {
Expand Down
2 changes: 2 additions & 0 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ export class Display extends EventDispatcher {
preventMiddleMouse: scanningOptions.preventMiddleMouse.onSearchQuery,
matchTypePrefix: false,
sentenceParsingOptions,
scanAltText: scanningOptions.scanAltText,
},
});

Expand Down Expand Up @@ -1984,6 +1985,7 @@ export class Display extends EventDispatcher {
layoutAwareScan: scanningOptions.layoutAwareScan,
preventMiddleMouse: false,
sentenceParsingOptions,
scanAltText: scanningOptions.scanAltText,
});

this._contentTextScanner.setEnabled(true);
Expand Down
13 changes: 12 additions & 1 deletion ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -443,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) ?
Expand All @@ -468,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};
Expand Down
9 changes: 9 additions & 0 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ <h1>Yomitan Settings</h1>
<label class="toggle"><input type="checkbox" data-setting="scanning.deepDomScan"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
<div class="settings-item"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Scan alt text</div>
<div class="settings-item-description">Scan text that is used for image and button descriptions.</div>
</div>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" data-setting="scanning.scanAltText"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
<div class="settings-item advanced-only">
<div class="settings-item-inner">
<div class="settings-item-left">
Expand Down
2 changes: 2 additions & 0 deletions test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function createProfileOptionsTestData1() {
enableOnSearchPage: true,
enableSearchTags: false,
layoutAwareScan: false,
scanAltText: true,
},
translation: {
convertHalfWidthCharacters: 'false',
Expand Down Expand Up @@ -325,6 +326,7 @@ function createProfileOptionsUpdatedTestData1() {
hidePopupOnCursorExit: false,
hidePopupOnCursorExitDelay: 0,
normalizeCssZoom: true,
scanAltText: true,
preventMiddleMouse: {
onWebPages: false,
onPopupPages: false,
Expand Down
1 change: 1 addition & 0 deletions types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export type ScanningOptions = {
hidePopupOnCursorExit: boolean;
hidePopupOnCursorExitDelay: number;
normalizeCssZoom: boolean;
scanAltText: boolean;
};

export type ScanningInput = {
Expand Down
1 change: 1 addition & 0 deletions types/ext/text-scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type Options = {
preventMiddleMouse?: boolean;
matchTypePrefix?: boolean;
sentenceParsingOptions?: SentenceParsingOptions;
scanAltText?: boolean;
};

export type InputOptionsOuter = {
Expand Down