diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-10-24 23:08:05 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-10-24 23:17:10 -0700 |
commit | cd052fce5b5b92368689ab42a13f472c31d8493a (patch) | |
tree | 676e957fbff14a9109f4594b5ddc0db7c3a12966 | |
parent | 4b966c906d71387ef0ef380b27a9394d1d80829c (diff) | |
download | jp-hash-cd052fce5b5b92368689ab42a13f472c31d8493a.tar.gz jp-hash-cd052fce5b5b92368689ab42a13f472c31d8493a.tar.bz2 jp-hash-cd052fce5b5b92368689ab42a13f472c31d8493a.zip |
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.
-rw-r--r-- | firefox/jp-hash.js | 14 |
1 files changed, 13 insertions, 1 deletions
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; }); + } } } |