Add scanTextAtCaret option (#1632)

This commit is contained in:
toasted-nutbread 2021-04-26 21:11:43 -04:00 committed by GitHub
parent 6f5ad490fb
commit 763c6c76aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 6 deletions

View File

@ -79,7 +79,8 @@ class Frontend {
]);
this._hotkeyHandler.registerActions([
['scanSelectedText', this._onActionScanSelectedText.bind(this)]
['scanSelectedText', this._onActionScanSelectedText.bind(this)],
['scanTextAtCaret', this._onActionScanTextAtCaret.bind(this)]
]);
}
@ -172,7 +173,11 @@ class Frontend {
// Action handlers
_onActionScanSelectedText() {
this._scanSelectedText();
this._scanSelectedText(false);
}
_onActionScanTextAtCaret() {
this._scanSelectedText(true);
}
// API message handlers
@ -679,19 +684,19 @@ class Frontend {
};
}
async _scanSelectedText() {
const range = this._getFirstNonEmptySelectionRange();
async _scanSelectedText(allowEmptyRange) {
const range = this._getFirstSelectionRange(allowEmptyRange);
if (range === null) { return false; }
const source = new TextSourceRange(range, range.toString(), null, null);
await this._textScanner.search(source, {focus: true, restoreSelection: true});
return true;
}
_getFirstNonEmptySelectionRange() {
_getFirstSelectionRange(allowEmptyRange) {
const selection = window.getSelection();
for (let i = 0, ii = selection.rangeCount; i < ii; ++i) {
const range = selection.getRangeAt(i);
if (range.toString().length > 0) {
if (range.toString().length > 0 || allowEmptyRange) {
return range;
}
}

View File

@ -52,6 +52,7 @@ class KeyboardShortcutController {
['playAudioFromSource', {scopes: new Set(['popup', 'search']), argument: {template: 'hotkey-argument-audio-source', default: 'jpod101'}}],
['copyHostSelection', {scopes: new Set(['popup'])}],
['scanSelectedText', {scopes: new Set(['web'])}],
['scanTextAtCaret', {scopes: new Set(['web'])}],
['toggleOption', {scopes: new Set(['popup', 'search']), argument: {template: 'hotkey-argument-setting-path', default: ''}}]
]);
}

View File

@ -3227,6 +3227,7 @@
<option value="playAudioFromSource">Play audio from source</option>
<option value="copyHostSelection">Copy host window selection</option>
<option value="scanSelectedText">Scan selected text</option>
<option value="scanTextAtCaret">Scan text at caret</option>
<option value="toggleOption">Toggle option</option>
</select>
<div class="hotkey-list-item-action-argument-container"></div>