From cd052fce5b5b92368689ab42a13f472c31d8493a Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 24 Oct 2022 23:08:05 -0700 Subject: firefox: feature: JP-Hash selection. * firefox/jp-hash.js (jp_hash_edit): If there is no selection, the whole input box is replaced with its hash. Oterwise, only the selection is replaced. --- firefox/jp-hash.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/firefox/jp-hash.js b/firefox/jp-hash.js index 1072b4b..4eaa42d 100644 --- a/firefox/jp-hash.js +++ b/firefox/jp-hash.js @@ -100,6 +100,18 @@ async function jp_hash_edit(elem) if (elem.nodeType == 1 && (elem.nodeName == "INPUT" || elem.nodeName == "TEXTAREA")) { - jphash(elem.value, (jph) => { elem.value = jph; }); + let start = elem.selectionStart; + let end = elem.selectionEnd; + + if (start != end) { + let len = elem.value.length; + let head = elem.value.substring(0, start); + let sel = elem.value.substring(start, end); + let tail = elem.value.substring(end, len); + + jphash(sel, (jph) => { elem.value = head + jph + tail; }); + } else { + jphash(elem.value, (jph) => { elem.value = jph; }); + } } } -- cgit v1.2.3