Skip to content

Commit f8054fb

Browse files
Refactor touch scan behavior (#1442)
* Remove scanAltText, exclude selector based on pointer event * input and textarea don't work * Update text-scanner.js * Prevent touch scan on input or text area * Update text-source-range.js * lint * Prevent exclude .tag and .gloss-link from website * exclude inflection * lint * lint * refactor * allow scan input text when tapping on same position * allow scan hyperlink on tap
1 parent fad0f1c commit f8054fb

File tree

9 files changed

+42
-40
lines changed

9 files changed

+42
-40
lines changed

ext/data/schemas/options-schema.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@
462462
"hideDelay",
463463
"length",
464464
"deepDomScan",
465-
"scanAltText",
466465
"popupNestingMaxDepth",
467466
"enablePopupSearch",
468467
"enableOnPopupExpressions",
@@ -716,10 +715,6 @@
716715
"type": "boolean",
717716
"default": false
718717
},
719-
"scanAltText": {
720-
"type": "boolean",
721-
"default": true
722-
},
723718
"popupNestingMaxDepth": {
724719
"type": "integer",
725720
"minimum": 0,

ext/js/app/frontend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ export class Frontend {
513513
matchTypePrefix: scanningOptions.matchTypePrefix,
514514
preventMiddleMouse,
515515
sentenceParsingOptions,
516-
scanAltText: scanningOptions.scanAltText,
517516
scanWithoutMousemove: scanningOptions.scanWithoutMousemove,
518517
scanResolution: scanningOptions.scanResolution,
519518
});
@@ -525,6 +524,7 @@ export class Frontend {
525524
excludeSelectors.push('.source-text', '.source-text *');
526525
}
527526
this._textScanner.excludeSelector = excludeSelectors.join(',');
527+
this._textScanner.touchEventExcludeSelector = 'gloss-link, gloss-link *, tag, tag *, inflection';
528528
}
529529

530530
this._updateContentScale();

ext/js/data/options-util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ export class OptionsUtil {
563563
this._updateVersion49,
564564
this._updateVersion50,
565565
this._updateVersion51,
566+
this._updateVersion52,
566567
];
567568
/* eslint-enable @typescript-eslint/unbound-method */
568569
if (typeof targetVersion === 'number' && targetVersion < result.length) {
@@ -1487,6 +1488,16 @@ export class OptionsUtil {
14871488
}
14881489
}
14891490

1491+
/**
1492+
* - Remove scanning.scanAltText
1493+
* @type {import('options-util').UpdateFunction}
1494+
*/
1495+
async _updateVersion52(options) {
1496+
for (const profile of options.profiles) {
1497+
delete profile.options.scanning.scanAltText;
1498+
}
1499+
}
1500+
14901501
/**
14911502
* @param {string} url
14921503
* @returns {Promise<chrome.tabs.Tab>}

ext/js/display/display.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ export class Display extends EventDispatcher {
464464
preventMiddleMouse: scanningOptions.preventMiddleMouse.onSearchQuery,
465465
matchTypePrefix: false,
466466
sentenceParsingOptions,
467-
scanAltText: scanningOptions.scanAltText,
468467
scanWithoutMousemove: scanningOptions.scanWithoutMousemove,
469468
scanResolution: scanningOptions.scanResolution,
470469
},
@@ -1994,6 +1993,7 @@ export class Display extends EventDispatcher {
19941993
});
19951994
this._contentTextScanner.includeSelector = '.click-scannable,.click-scannable *';
19961995
this._contentTextScanner.excludeSelector = '.scan-disable,.scan-disable *';
1996+
this._contentTextScanner.touchEventExcludeSelector = null;
19971997
this._contentTextScanner.prepare();
19981998
this._contentTextScanner.on('clear', this._onContentTextScannerClear.bind(this));
19991999
this._contentTextScanner.on('searchSuccess', this._onContentTextScannerSearchSuccess.bind(this));
@@ -2033,7 +2033,6 @@ export class Display extends EventDispatcher {
20332033
layoutAwareScan: scanningOptions.layoutAwareScan,
20342034
preventMiddleMouse: false,
20352035
sentenceParsingOptions,
2036-
scanAltText: scanningOptions.scanAltText,
20372036
});
20382037

20392038
this._contentTextScanner.setEnabled(true);

ext/js/language/text-scanner.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export class TextScanner extends EventDispatcher {
7272
/** @type {?string} */
7373
this._excludeSelector = null;
7474
/** @type {?string} */
75+
this._touchExcludeSelector = null;
76+
/** @type {?string} */
7577
this._language = null;
7678

7779
/** @type {?import('text-scanner').InputInfo} */
@@ -124,8 +126,6 @@ export class TextScanner extends EventDispatcher {
124126
this._sentenceBackwardQuoteMap = new Map();
125127
/** @type {import('text-scanner').InputConfig[]} */
126128
this._inputs = [];
127-
/** @type {boolean} */
128-
this._scanAltText = true;
129129

130130
/** @type {boolean} */
131131
this._enabled = false;
@@ -198,6 +198,15 @@ export class TextScanner extends EventDispatcher {
198198
this._excludeSelector = value;
199199
}
200200

201+
/** @type {?string} */
202+
get touchEventExcludeSelector() {
203+
return this._touchExcludeSelector;
204+
}
205+
206+
set touchEventExcludeSelector(value) {
207+
this._touchExcludeSelector = value;
208+
}
209+
201210
/** @type {?string} */
202211
get language() { return this._language; }
203212
set language(value) { this._language = value; }
@@ -257,7 +266,6 @@ export class TextScanner extends EventDispatcher {
257266
preventMiddleMouse,
258267
sentenceParsingOptions,
259268
matchTypePrefix,
260-
scanAltText,
261269
scanWithoutMousemove,
262270
scanResolution,
263271
}) {
@@ -294,9 +302,6 @@ export class TextScanner extends EventDispatcher {
294302
if (typeof matchTypePrefix === 'boolean') {
295303
this._matchTypePrefix = matchTypePrefix;
296304
}
297-
if (typeof scanAltText === 'boolean') {
298-
this._scanAltText = scanAltText;
299-
}
300305
if (typeof scanWithoutMousemove === 'boolean') {
301306
this._scanWithoutMousemove = scanWithoutMousemove;
302307
}
@@ -348,9 +353,9 @@ export class TextScanner extends EventDispatcher {
348353
clonedTextSource.setEndOffset(length, false, layoutAwareScan);
349354

350355
const includeSelector = this._includeSelector;
351-
const excludeSelector = this._excludeSelector;
356+
const excludeSelector = this._getExcludeSelectorForPointerType(pointerType);
352357
if (includeSelector !== null || excludeSelector !== null) {
353-
this._constrainTextSource(clonedTextSource, includeSelector, excludeSelector, layoutAwareScan, pointerType);
358+
this._constrainTextSource(clonedTextSource, includeSelector, excludeSelector, layoutAwareScan);
354359
}
355360

356361
return clonedTextSource.text();
@@ -460,8 +465,17 @@ export class TextScanner extends EventDispatcher {
460465
async _search(textSource, searchTerms, searchKanji, inputInfo, showEmpty = false) {
461466
try {
462467
const isAltText = textSource instanceof TextSourceElement;
463-
if (isAltText && !this._scanAltText) {
464-
return;
468+
if (inputInfo.pointerType === 'touch') {
469+
if (isAltText) {
470+
return;
471+
}
472+
const {imposterSourceElement, rangeStartOffset} = textSource;
473+
if (imposterSourceElement instanceof HTMLTextAreaElement || imposterSourceElement instanceof HTMLInputElement) {
474+
const isFocused = imposterSourceElement === document.activeElement;
475+
if (!isFocused || imposterSourceElement.selectionStart !== rangeStartOffset) {
476+
return;
477+
}
478+
}
465479
}
466480

467481
const inputInfoDetail = inputInfo.detail;
@@ -1637,11 +1651,9 @@ export class TextScanner extends EventDispatcher {
16371651
* @param {?string} includeSelector
16381652
* @param {?string} excludeSelector
16391653
* @param {boolean} layoutAwareScan
1640-
* @param {import('input').PointerType | undefined} pointerType
16411654
*/
1642-
_constrainTextSource(textSource, includeSelector, excludeSelector, layoutAwareScan, pointerType) {
1655+
_constrainTextSource(textSource, includeSelector, excludeSelector, layoutAwareScan) {
16431656
let length = textSource.text().length;
1644-
excludeSelector = this._createExcludeSelectorForPointerType(excludeSelector, pointerType);
16451657

16461658
while (length > 0) {
16471659
const nodes = textSource.getNodesInRange();
@@ -1658,17 +1670,14 @@ export class TextScanner extends EventDispatcher {
16581670
}
16591671

16601672
/**
1661-
* @param {?string} excludeSelector
16621673
* @param {import('input').PointerType | undefined} pointerType
16631674
* @returns {?string}
16641675
*/
1665-
_createExcludeSelectorForPointerType(excludeSelector, pointerType) {
1676+
_getExcludeSelectorForPointerType(pointerType) {
16661677
if (pointerType === 'touch') {
1667-
// Avoid trigger search with tapping on popup link, tag and inflection.
1668-
const popupClickableSelector = '.gloss-link, .gloss-link *, .tag, .tag *, .inflection';
1669-
return excludeSelector ? `${excludeSelector},${popupClickableSelector}` : popupClickableSelector;
1678+
return this._excludeSelector ? `${this._excludeSelector},${this.touchEventExcludeSelector}` : this.touchEventExcludeSelector;
16701679
}
1671-
return excludeSelector;
1680+
return this._excludeSelector;
16721681
}
16731682

16741683
/**

ext/settings.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,6 @@ <h1>Yomitan Settings</h1>
477477
<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>
478478
</div>
479479
</div></div>
480-
<div class="settings-item"><div class="settings-item-inner">
481-
<div class="settings-item-left">
482-
<div class="settings-item-label">Scan alt text</div>
483-
<div class="settings-item-description">Scan text that is used for image and button descriptions.</div>
484-
</div>
485-
<div class="settings-item-right">
486-
<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>
487-
</div>
488-
</div></div>
489480
<div class="settings-item advanced-only">
490481
<div class="settings-item-inner">
491482
<div class="settings-item-left">

test/options-util.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ function createProfileOptionsUpdatedTestData1() {
348348
hidePopupOnCursorExit: false,
349349
hidePopupOnCursorExitDelay: 0,
350350
normalizeCssZoom: true,
351-
scanAltText: true,
352351
preventMiddleMouse: {
353352
onWebPages: false,
354353
onPopupPages: false,
@@ -645,7 +644,7 @@ function createOptionsUpdatedTestData1() {
645644
},
646645
],
647646
profileCurrent: 0,
648-
version: 51,
647+
version: 52,
649648
global: {
650649
database: {
651650
prefixWildcardsSupported: false,

types/ext/settings.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ export type ScanningOptions = {
197197
hidePopupOnCursorExit: boolean;
198198
hidePopupOnCursorExitDelay: number;
199199
normalizeCssZoom: boolean;
200-
scanAltText: boolean;
201200
scanWithoutMousemove: boolean;
202201
scanResolution: string;
203202
};

types/ext/text-scanner.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export type Options = {
4141
preventMiddleMouse?: boolean;
4242
matchTypePrefix?: boolean;
4343
sentenceParsingOptions?: SentenceParsingOptions;
44-
scanAltText?: boolean;
4544
scanWithoutMousemove?: boolean;
4645
scanResolution?: string;
4746
};

0 commit comments

Comments
 (0)