summaryrefslogtreecommitdiff
path: root/extension/currentURL.js
diff options
context:
space:
mode:
Diffstat (limited to 'extension/currentURL.js')
-rw-r--r--extension/currentURL.js11
1 files changed, 11 insertions, 0 deletions
diff --git a/extension/currentURL.js b/extension/currentURL.js
new file mode 100644
index 0000000..0aeab92
--- /dev/null
+++ b/extension/currentURL.js
@@ -0,0 +1,11 @@
+function encode(str) {
+ return encodeURIComponent(str).replace("-", "%2D")
+}
+
+let sel = window.document.getSelection(),
+ ctx = sel?.anchorNode?.parentElement?.textContent?.split(sel),
+ anc = sel.toString() ? `:~:text=${ctx[0] ? encode(ctx[0])+"-," : ""}${sel}${ctx[1] ? ",-"+encode(ctx[1]) : ""}` : ""
+
+let url = new URL(window.location)
+if (anc) url.hash = anc
+url.toString()