aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js/frontend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-17 19:28:42 -0500
committerGitHub <noreply@github.com>2021-01-17 19:28:42 -0500
commitc875ca728f619c6e6bccbd13ceb90d898c59e53e (patch)
treeedcf6862bd5d33eefea6c1a2af867b16d1040081 /ext/fg/js/frontend.js
parentde5d3ec3ad608893a9db75229296094f85c200c3 (diff)
Add support for frontend hotkeys; add scanSelectedText action (#1266)
* Add support for frontend hotkeys; add scanSelectedText action * Remove unused global * Remove duplicate hotkey handler script
Diffstat (limited to 'ext/fg/js/frontend.js')
-rw-r--r--ext/fg/js/frontend.js36
1 files changed, 35 insertions, 1 deletions
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index d789c4f0..e9b57eb5 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -19,6 +19,7 @@
* DocumentUtil
* TextScanner
* TextSourceElement
+ * TextSourceRange
* api
*/
@@ -32,7 +33,8 @@ class Frontend {
parentFrameId,
useProxyPopup,
allowRootFramePopupProxy,
- childrenSupported=true
+ childrenSupported=true,
+ hotkeyHandler
}) {
this._pageType = pageType;
this._popupFactory = popupFactory;
@@ -43,6 +45,7 @@ class Frontend {
this._useProxyPopup = useProxyPopup;
this._allowRootFramePopupProxy = allowRootFramePopupProxy;
this._childrenSupported = childrenSupported;
+ this._hotkeyHandler = hotkeyHandler;
this._popup = null;
this._disabledOverride = false;
this._options = null;
@@ -71,6 +74,10 @@ class Frontend {
['setAllVisibleOverride', {async: true, handler: this._onApiSetAllVisibleOverride.bind(this)}],
['clearAllVisibleOverride', {async: true, handler: this._onApiClearAllVisibleOverride.bind(this)}]
]);
+
+ this._hotkeyHandler.registerActions([
+ ['scanSelectedText', this._onActionScanSelectedText.bind(this)]
+ ]);
}
get canClearSelection() {
@@ -161,6 +168,12 @@ class Frontend {
this._signalFrontendReady(frameId);
}
+ // Action handlers
+
+ _onActionScanSelectedText() {
+ this._scanSelectedText();
+ }
+
// API message handlers
_onApiGetUrl() {
@@ -319,6 +332,8 @@ class Frontend {
const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
this._options = options;
+ this._hotkeyHandler.setHotkeys('web', options.inputs.hotkeys);
+
await this._updatePopup();
const preventMiddleMouse = this._getPreventMiddleMouseValueForPageType(scanningOptions.preventMiddleMouse);
@@ -646,4 +661,23 @@ class Frontend {
detail: {documentTitle}
};
}
+
+ async _scanSelectedText() {
+ const range = this._getFirstNonEmptySelectionRange();
+ if (range === null) { return false; }
+ const source = new TextSourceRange(range, range.toString(), null, null);
+ await this._textScanner.search(source, {focus: true});
+ return true;
+ }
+
+ _getFirstNonEmptySelectionRange() {
+ const selection = window.getSelection();
+ for (let i = 0, ii = selection.rangeCount; i < ii; ++i) {
+ const range = selection.getRangeAt(i);
+ if (range.toString().length > 0) {
+ return range;
+ }
+ }
+ return null;
+ }
}