From b786e1c86571fb63d31a10ded66904e6d415e130 Mon Sep 17 00:00:00 2001 From: twio142 <64556708+twio142@users.noreply.github.com> Date: Thu, 20 Jun 2024 18:09:24 +0200 Subject: [PATCH 1/2] find mode: support matching for selected text --- background_scripts/commands.js | 6 +++++- content_scripts/mode_find.js | 11 ++++++++--- content_scripts/mode_normal.js | 12 ++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/background_scripts/commands.js b/background_scripts/commands.js index 46916cc81..f8f163d9d 100644 --- a/background_scripts/commands.js +++ b/background_scripts/commands.js @@ -330,7 +330,7 @@ const Commands = { "Vomnibar.activateEditUrl", "Vomnibar.activateEditUrlInNewTab", ], - findCommands: ["enterFindMode", "performFind", "performBackwardsFind"], + findCommands: ["enterFindMode", "performFind", "performBackwardsFind", "findSelected", "findSelectedBackwards"], historyNavigation: ["goBack", "goForward"], tabManipulation: [ "createTab", @@ -423,6 +423,8 @@ const defaultKeyMappings = { "/": "enterFindMode", "n": "performFind", "N": "performBackwardsFind", + "*": "findSelected", + "#": "findSelectedBackwards", // Vomnibar "o": "Vomnibar.activate", @@ -509,6 +511,8 @@ const commandDescriptions = { enterFindMode: ["Enter find mode", { noRepeat: true }], performFind: ["Cycle forward to the next find match"], performBackwardsFind: ["Cycle backward to the previous find match"], + findSelected: ["Find the selected text"], + findSelectedBackwards: ["Find the selected text backwards"], goPrevious: ["Follow the link labeled previous or <", { noRepeat: true }], goNext: ["Follow the link labeled next or >", { noRepeat: true }], diff --git a/content_scripts/mode_find.js b/content_scripts/mode_find.js index 0b9c08b8f..45acb50f7 100644 --- a/content_scripts/mode_find.js +++ b/content_scripts/mode_find.js @@ -266,7 +266,12 @@ class FindMode extends Mode { } // Returns null if no search has been performed yet. - static getQuery(backwards) { + static getQuery(backwards, query) { + if (query) { + this.query = {}; + this.updateQuery(query); + this.saveQuery(); + } if (!this.query) return; // check if the query has been changed by a script in another frame const mostRecentQuery = FindModeHistory.getQuery(); @@ -358,9 +363,9 @@ class FindMode extends Mode { return FindMode.saveQuery(); } - static findNext(backwards) { + static findNext(backwards, query) { // Bail out if we don't have any query text. - const nextQuery = FindMode.getQuery(backwards); + const nextQuery = FindMode.getQuery(backwards, query); if (!nextQuery) { HUD.show("No query to find.", 1000); return; diff --git a/content_scripts/mode_normal.js b/content_scripts/mode_normal.js index 460bcee0b..f59e47d11 100644 --- a/content_scripts/mode_normal.js +++ b/content_scripts/mode_normal.js @@ -213,6 +213,18 @@ const NormalModeCommands = { } }, + findSelected() { + let selection = window.getSelection().toString(); + if (!selection) return; + return FindMode.findNext(false, selection); + }, + + findSelectedBackwards() { + let selection = window.getSelection().toString(); + if (!selection) return; + return FindMode.findNext(true, selection); + }, + // Misc. mainFrame() { return focusThisFrame({ highlight: true, forceFocusThisFrame: true }); From 8578bd8847b21d7a7ed852a453492a80017a1387 Mon Sep 17 00:00:00 2001 From: twio142 <64556708+twio142@users.noreply.github.com> Date: Tue, 16 Jul 2024 13:31:19 +0200 Subject: [PATCH 2/2] avoid passing in a query to `FindMode.findNext` --- background_scripts/commands.js | 8 +++++++- content_scripts/mode_find.js | 14 ++++++-------- content_scripts/mode_normal.js | 8 ++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/background_scripts/commands.js b/background_scripts/commands.js index f8f163d9d..105232f2b 100644 --- a/background_scripts/commands.js +++ b/background_scripts/commands.js @@ -330,7 +330,13 @@ const Commands = { "Vomnibar.activateEditUrl", "Vomnibar.activateEditUrlInNewTab", ], - findCommands: ["enterFindMode", "performFind", "performBackwardsFind", "findSelected", "findSelectedBackwards"], + findCommands: [ + "enterFindMode", + "performFind", + "performBackwardsFind", + "findSelected", + "findSelectedBackwards", + ], historyNavigation: ["goBack", "goForward"], tabManipulation: [ "createTab", diff --git a/content_scripts/mode_find.js b/content_scripts/mode_find.js index 45acb50f7..874c6a4af 100644 --- a/content_scripts/mode_find.js +++ b/content_scripts/mode_find.js @@ -152,6 +152,9 @@ class FindMode extends Mode { static updateQuery(query) { let pattern; + if (!this.query) { + this.query = {}; + } this.query.rawQuery = query; // the query can be treated differently (e.g. as a plain string versus regex depending on the // presence of escape sequences. '\' is the escape character and needs to be escaped itself to @@ -266,12 +269,7 @@ class FindMode extends Mode { } // Returns null if no search has been performed yet. - static getQuery(backwards, query) { - if (query) { - this.query = {}; - this.updateQuery(query); - this.saveQuery(); - } + static getQuery(backwards) { if (!this.query) return; // check if the query has been changed by a script in another frame const mostRecentQuery = FindModeHistory.getQuery(); @@ -363,9 +361,9 @@ class FindMode extends Mode { return FindMode.saveQuery(); } - static findNext(backwards, query) { + static findNext(backwards) { // Bail out if we don't have any query text. - const nextQuery = FindMode.getQuery(backwards, query); + const nextQuery = FindMode.getQuery(backwards); if (!nextQuery) { HUD.show("No query to find.", 1000); return; diff --git a/content_scripts/mode_normal.js b/content_scripts/mode_normal.js index f59e47d11..4240fab90 100644 --- a/content_scripts/mode_normal.js +++ b/content_scripts/mode_normal.js @@ -216,13 +216,17 @@ const NormalModeCommands = { findSelected() { let selection = window.getSelection().toString(); if (!selection) return; - return FindMode.findNext(false, selection); + FindMode.updateQuery(selection); + FindMode.saveQuery(); + return FindMode.findNext(false); }, findSelectedBackwards() { let selection = window.getSelection().toString(); if (!selection) return; - return FindMode.findNext(true, selection); + FindMode.updateQuery(selection); + FindMode.saveQuery(); + return FindMode.findNext(true); }, // Misc.