diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-01-17 19:28:42 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-17 19:28:42 -0500 |
commit | c875ca728f619c6e6bccbd13ceb90d898c59e53e (patch) | |
tree | edcf6862bd5d33eefea6c1a2af867b16d1040081 | |
parent | de5d3ec3ad608893a9db75229296094f85c200c3 (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
-rw-r--r-- | dev/data/manifest-variants.json | 1 | ||||
-rw-r--r-- | ext/bg/data/options-schema.json | 2 | ||||
-rw-r--r-- | ext/bg/js/settings/popup-preview-frame-main.js | 6 | ||||
-rw-r--r-- | ext/bg/js/settings/popup-preview-frame.js | 6 | ||||
-rw-r--r-- | ext/bg/popup-preview.html | 1 | ||||
-rw-r--r-- | ext/bg/settings2.html | 5 | ||||
-rw-r--r-- | ext/fg/js/content-script-main.js | 7 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 36 | ||||
-rw-r--r-- | ext/manifest.json | 1 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 3 |
10 files changed, 61 insertions, 7 deletions
diff --git a/dev/data/manifest-variants.json b/dev/data/manifest-variants.json index d94ed9ab..757066f0 100644 --- a/dev/data/manifest-variants.json +++ b/dev/data/manifest-variants.json @@ -47,6 +47,7 @@ "mixed/js/frame-client.js", "mixed/js/text-scanner.js", "mixed/js/document-util.js", + "mixed/js/hotkey-handler.js", "fg/js/dom-text-scanner.js", "fg/js/popup.js", "fg/js/text-source-range.js", diff --git a/ext/bg/data/options-schema.json b/ext/bg/data/options-schema.json index def279cc..a140d3e9 100644 --- a/ext/bg/data/options-schema.json +++ b/ext/bg/data/options-schema.json @@ -1011,7 +1011,7 @@ "type": "array", "items": { "type": "string", - "enum": ["popup", "search"], + "enum": ["popup", "search", "web"], "default": "popup" }, "default": ["popup", "search"] diff --git a/ext/bg/js/settings/popup-preview-frame-main.js b/ext/bg/js/settings/popup-preview-frame-main.js index a639ced7..71454017 100644 --- a/ext/bg/js/settings/popup-preview-frame-main.js +++ b/ext/bg/js/settings/popup-preview-frame-main.js @@ -16,6 +16,7 @@ */ /* global + * HotkeyHandler * PopupFactory * PopupPreviewFrame * api @@ -27,10 +28,13 @@ const {frameId} = await api.frameInformationGet(); + const hotkeyHandler = new HotkeyHandler(); + hotkeyHandler.prepare(); + const popupFactory = new PopupFactory(frameId); popupFactory.prepare(); - const preview = new PopupPreviewFrame(frameId, popupFactory); + const preview = new PopupPreviewFrame(frameId, popupFactory, hotkeyHandler); await preview.prepare(); document.documentElement.dataset.loaded = 'true'; diff --git a/ext/bg/js/settings/popup-preview-frame.js b/ext/bg/js/settings/popup-preview-frame.js index 73d8882a..92b57c7a 100644 --- a/ext/bg/js/settings/popup-preview-frame.js +++ b/ext/bg/js/settings/popup-preview-frame.js @@ -23,9 +23,10 @@ */ class PopupPreviewFrame { - constructor(frameId, popupFactory) { + constructor(frameId, popupFactory, hotkeyHandler) { this._frameId = frameId; this._popupFactory = popupFactory; + this._hotkeyHandler = hotkeyHandler; this._frontend = null; this._apiOptionsGetOld = null; this._popupShown = false; @@ -74,7 +75,8 @@ class PopupPreviewFrame { useProxyPopup: false, pageType: 'web', allowRootFramePopupProxy: false, - childrenSupported: false + childrenSupported: false, + hotkeyHandler: this._hotkeyHandler }); this._frontend.setOptionsContextOverride(this._optionsContext); await this._frontend.prepare(); diff --git a/ext/bg/popup-preview.html b/ext/bg/popup-preview.html index 75386e24..d8855946 100644 --- a/ext/bg/popup-preview.html +++ b/ext/bg/popup-preview.html @@ -45,6 +45,7 @@ <script src="/mixed/js/text-scanner.js"></script> <script src="/mixed/js/document-util.js"></script> +<script src="/mixed/js/hotkey-handler.js"></script> <script src="/fg/js/dom-text-scanner.js"></script> <script src="/fg/js/popup.js"></script> <script src="/fg/js/text-source-range.js"></script> diff --git a/ext/bg/settings2.html b/ext/bg/settings2.html index 0fd3feb0..128bd5ef 100644 --- a/ext/bg/settings2.html +++ b/ext/bg/settings2.html @@ -3014,10 +3014,15 @@ <option value="viewNote" data-scopes="popup search">View note</option> <option value="playAudio" data-scopes="popup search">Play audio</option> <option value="copyHostSelection" data-scopes="popup search">Copy selection</option> + <option value="scanSelectedText" data-scopes="web">Scan selected text</option> </select> <div class="hotkey-list-item-flex-row"> <div class="hotkey-list-item-flex-row-label">Scopes:</div> <div class="hotkey-list-item-flex-row"> + <label class="hotkey-scope-checkbox-container" data-scope="web"> + <label class="checkbox"><input type="checkbox" class="hotkey-scope-checkbox" data-scope="web"><span class="checkbox-body"><span class="checkbox-fill"></span><span class="checkbox-border"></span><span class="checkbox-check"></span></span></label> + <span>Web</span> + </label> <label class="hotkey-scope-checkbox-container" data-scope="popup"> <label class="checkbox"><input type="checkbox" class="hotkey-scope-checkbox" data-scope="popup"><span class="checkbox-body"><span class="checkbox-fill"></span><span class="checkbox-border"></span><span class="checkbox-check"></span></span></label> <span>Popup</span> diff --git a/ext/fg/js/content-script-main.js b/ext/fg/js/content-script-main.js index 80a600e2..42f95e24 100644 --- a/ext/fg/js/content-script-main.js +++ b/ext/fg/js/content-script-main.js @@ -17,6 +17,7 @@ /* global * Frontend + * HotkeyHandler * PopupFactory * api */ @@ -31,6 +32,9 @@ throw new Error('Failed to get frameId'); } + const hotkeyHandler = new HotkeyHandler(); + hotkeyHandler.prepare(); + const popupFactory = new PopupFactory(frameId); popupFactory.prepare(); @@ -42,7 +46,8 @@ parentFrameId: null, useProxyPopup: false, pageType: 'web', - allowRootFramePopupProxy: true + allowRootFramePopupProxy: true, + hotkeyHandler }); await frontend.prepare(); 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; + } } diff --git a/ext/manifest.json b/ext/manifest.json index 97f77dde..495044a4 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -46,6 +46,7 @@ "mixed/js/frame-client.js", "mixed/js/text-scanner.js", "mixed/js/document-util.js", + "mixed/js/hotkey-handler.js", "fg/js/dom-text-scanner.js", "fg/js/popup.js", "fg/js/text-source-range.js", diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index c017a2f1..3c7deefe 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -1640,7 +1640,8 @@ class Display extends EventDispatcher { popupFactory, pageType: this._pageType, allowRootFramePopupProxy: true, - childrenSupported: this._childrenSupported + childrenSupported: this._childrenSupported, + hotkeyHandler: this._hotkeyHandler }); const frontend = new Frontend(setupNestedPopupsOptions); |