diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-02-11 17:59:28 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-02-11 18:04:22 +0200 |
| commit | 001b52fc1b6d2b30b371611f5a8b961e5c6945d3 (patch) | |
| tree | 937b73ccad1aeb927d8974fe703382dafd360768 /browser/static | |
| parent | e8f3a470ce693bef9303d3dd4c46523cdc691826 (diff) | |
Implement find
Diffstat (limited to 'browser/static')
| -rw-r--r-- | browser/static/htmp.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/browser/static/htmp.js b/browser/static/htmp.js new file mode 100644 index 0000000..36deae2 --- /dev/null +++ b/browser/static/htmp.js @@ -0,0 +1,51 @@ +const iframe = document.createElement("iframe"); +iframe.name = "htmp"; +iframe.hidden = true; +document.body.appendChild(iframe); + +document + .querySelector("iframe[name='htmp']") + .addEventListener("load", function () { + setTimeout(() => { + const cd = this.contentDocument; + const cw = this.contentWindow; + // If the server responds with an entire HTML document, replace the current document with it. + // To check whether the response is an entire page we check the presence of this very snippet. + if (cd.querySelector("iframe[name='htmp']")) { + document.documentElement.replaceWith(cd.documentElement); + // If the server responds with a fragment, replace the target element with it. + } else { + document + .querySelector(cw.location.hash || null) + ?.replaceWith(...cd.body.childNodes); + const url = new URL(cw.location.href); + url.searchParams.delete("htmp"); + url.hash = ""; + history.replaceState({}, "", url.toString()); + } + }); + }); + +document.querySelectorAll("form[htmp]").forEach(function (el) { + const re = el.attributes.replace.value; + const input = document.createElement("input"); + input.type = "hidden"; + input.name = "htmp"; + input.value = re; + el.appendChild(input); + + const action = new URL(el.action); + action.hash = re; + el.action = action.toString(); + el.target = "htmp"; +}); + +document.querySelectorAll("a[htmp]").forEach(function (el) { + const re = el.attributes.replace.value; + + const href = new URL(el.href); + href.hash = re; + href.searchParams.set("htmp", re); + el.target = "htmp"; + el.href = href.toString(); +}); |
