/* Client shim: keeps dynamic requests (fetch, XHR, DOM URL setters, history) flowing through the server proxy, and draws the small URL bar. */ (function () { if (window.__PX_INSTALLED__) return; window.__PX_INSTALLED__ = true; var ORIGIN = location.origin; var base = window.__PX_BASE__ || ""; function isProxied(s) { return /^(https?:\/\/[^\/]+)?\/p\?(u|__px_u)=/.test(s) && (s.charAt(0) === "/" || s.indexOf(ORIGIN) === 0); } function toProxy(abs) { var i = abs.indexOf("#"), hash = ""; if (i >= 0) { hash = abs.slice(i); abs = abs.slice(0, i); } return "/p?u=" + encodeURIComponent(abs) + hash; } function px(url) { if (!base || url == null) return url; var s = String(url).trim(); if (!s || /^(#|data:|blob:|javascript:|mailto:|tel:|about:)/i.test(s) || isProxied(s)) return url; try { var u = new URL(s, base); // Code that built a URL from location.origin points at the proxy itself; // re-aim it at the real site. if (u.origin === ORIGIN) u = new URL(u.pathname + u.search + u.hash, base); if (u.protocol !== "http:" && u.protocol !== "https:") return url; return toProxy(u.href); } catch (e) { return url; } } function pxSrcset(v) { return String(v).split(",").map(function (part) { var bits = part.trim().split(/\s+/); if (bits[0]) bits[0] = px(bits[0]); return bits.join(" "); }).join(", "); } /* ---------------------------------------------------------- patches */ if (base) { var _fetch = window.fetch; if (_fetch) { window.fetch = function (input, init) { try { if (typeof input === "string" || input instanceof URL) input = px(String(input)); else if (input && input.url) input = new Request(px(input.url), input); } catch (e) {} return _fetch.call(this, input, init); }; } var _xhrOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function (method, url) { var a = Array.prototype.slice.call(arguments); a[1] = px(url); return _xhrOpen.apply(this, a); }; if (navigator.sendBeacon) { var _beacon = navigator.sendBeacon; navigator.sendBeacon = function (url, data) { return _beacon.call(this, px(url), data); }; } var _open = window.open; window.open = function () { var a = Array.prototype.slice.call(arguments); if (a.length) a[0] = px(a[0]); return _open.apply(window, a); }; ["pushState", "replaceState"].forEach(function (fn) { var orig = history[fn]; history[fn] = function (state, title, url) { if (url != null) { try { var abs = new URL(String(url), base).href; base = abs; window.__PX_BASE__ = abs; url = toProxy(abs); } catch (e) {} } var r = orig.call(this, state, title, url); syncBar(); return r; }; }); function syncBase() { var u = new URLSearchParams(location.search).get("u"); if (u) base = u + location.hash; syncBar(); } window.addEventListener("popstate", syncBase); window.addEventListener("hashchange", syncBase); [[HTMLImageElement, "src"], [HTMLScriptElement, "src"], [HTMLIFrameElement, "src"], [HTMLMediaElement, "src"], [HTMLSourceElement, "src"], [HTMLEmbedElement, "src"], [HTMLInputElement, "src"], [HTMLLinkElement, "href"], [HTMLAnchorElement, "href"], [HTMLFormElement, "action"], [HTMLVideoElement, "poster"] ].forEach(function (pair) { var d = Object.getOwnPropertyDescriptor(pair[0].prototype, pair[1]); if (!d || !d.set) return; Object.defineProperty(pair[0].prototype, pair[1], { configurable: true, enumerable: d.enumerable, get: d.get, set: function (v) { d.set.call(this, px(v)); } }); }); var URL_ATTR = { src: 1, href: 1, action: 1, poster: 1, data: 1, formaction: 1 }; var _setAttr = Element.prototype.setAttribute; Element.prototype.setAttribute = function (name, value) { var n = String(name).toLowerCase(); if (URL_ATTR[n]) value = px(value); else if (n === "srcset") value = pxSrcset(value); return _setAttr.call(this, name, value); }; // Markup injected as a string: rewrite URLs on the new nodes right after // insertion (before image loads start), without re-parsing the HTML. var SEL = "[src],[href],[action],[poster],[srcset]"; function fixTree(root) { if (!root || !root.querySelectorAll) return; var list = root.querySelectorAll(SEL); for (var i = 0; i < list.length; i++) { var el = list[i]; ["src", "href", "action", "poster"].forEach(function (a) { var v = el.getAttribute(a); if (v != null) { var nv = px(v); if (nv !== v) _setAttr.call(el, a, nv); } }); var ss = el.getAttribute("srcset"); if (ss != null) { var ns = pxSrcset(ss); if (ns !== ss) _setAttr.call(el, "srcset", ns); } } } var innerDesc = Object.getOwnPropertyDescriptor(Element.prototype, "innerHTML"); Object.defineProperty(Element.prototype, "innerHTML", { configurable: true, enumerable: innerDesc.enumerable, get: innerDesc.get, set: function (v) { innerDesc.set.call(this, v); fixTree(this); } }); var _iah = Element.prototype.insertAdjacentHTML; Element.prototype.insertAdjacentHTML = function (pos, html) { var r = _iah.call(this, pos, html); fixTree(/^(beforebegin|afterend)$/i.test(pos) ? this.parentNode : this); return r; }; } /* ---------------------------------------------------------- URL bar */ var ICON = { back: '', fwd: '', reload: '', go: '', up: '', down: '', code: '' }; function svg(name) { return '' + ICON[name] + ""; } var input, barHost; function syncBar() { if (input && document.activeElement !== barHost) input.value = base.split("#")[0]; } function normalize(v) { v = v.trim(); if (!v) return null; if (/^https?:\/\//i.test(v)) return v; if (!/\s/.test(v) && /^[^\s\/]+\.[^\s\/]{2,}(\/.*)?$/.test(v)) return "https://" + v; return "https://duckduckgo.com/html/?q=" + encodeURIComponent(v); } function navigate() { var url = normalize(input.value); if (url) location.href = "/p?u=" + encodeURIComponent(url); } function mountBar() { if (document.getElementById("__px_bar")) return; barHost = document.createElement("div"); barHost.id = "__px_bar"; var root = barHost.attachShadow({ mode: "open" }); root.innerHTML = "" + '
" + '" + '" + '' + '" + '" + '
" + '"; document.documentElement.appendChild(barHost); document.documentElement.style.setProperty("margin-top", "40px", "important"); input = root.getElementById("u"); barHost = root.host; input.value = base.split("#")[0]; var bar = root.querySelector(".bar"), tab = root.getElementById("t"); root.getElementById("b").onclick = function () { history.back(); }; root.getElementById("f").onclick = function () { history.forward(); }; root.getElementById("r").onclick = function () { location.reload(); }; root.getElementById("g").onclick = navigate; input.addEventListener("keydown", function (e) { if (e.key === "Enter") navigate(); }); input.addEventListener("focus", function () { input.select(); }); var jsBtn = root.getElementById("j"); function nojsOn() { return /(^|;\s*)px_nojs=1/.test(document.cookie); } function paintJs() { var off = nojsOn(); jsBtn.className = off ? "on" : ""; jsBtn.title = off ? "Page scripts are OFF (click to turn on)" : "Page scripts are on (click to turn off)"; } paintJs(); jsBtn.onclick = function () { document.cookie = "px_nojs=" + (nojsOn() ? "0" : "1") + "; path=/; max-age=31536000; SameSite=None; Secure"; location.reload(); }; root.getElementById("c").onclick = function () { bar.style.display = "none"; tab.style.display = "grid"; document.documentElement.style.setProperty("margin-top", "0", "important"); }; tab.onclick = function () { bar.style.display = "flex"; tab.style.display = "none"; document.documentElement.style.setProperty("margin-top", "40px", "important"); }; } if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", mountBar); else mountBar(); })();