summaryrefslogtreecommitdiff
path: root/extension/captureURL.js
blob: 2f7db4a6322c3dc4772906a8db4bd613f07a120b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(() => {
  function encode(str) {
    return encodeURIComponent(str)
      .replace(",", "%2C")
      .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
  return url.toString()
})()