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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Client shim: keeps dynamic requests (fetch, XHR, DOM URL setters, history)
|
| 2 |
+
flowing through the server proxy, and draws the small URL bar. */
|
| 3 |
+
(function () {
|
| 4 |
+
if (window.__PX_INSTALLED__) return;
|
| 5 |
+
window.__PX_INSTALLED__ = true;
|
| 6 |
+
|
| 7 |
+
var ORIGIN = location.origin;
|
| 8 |
+
var base = window.__PX_BASE__ || "";
|
| 9 |
+
|
| 10 |
+
function isProxied(s) {
|
| 11 |
+
return /^(https?:\/\/[^\/]+)?\/p\?(u|__px_u)=/.test(s) &&
|
| 12 |
+
(s.charAt(0) === "/" || s.indexOf(ORIGIN) === 0);
|
| 13 |
+
}
|
| 14 |
+
function toProxy(abs) {
|
| 15 |
+
var i = abs.indexOf("#"), hash = "";
|
| 16 |
+
if (i >= 0) { hash = abs.slice(i); abs = abs.slice(0, i); }
|
| 17 |
+
return "/p?u=" + encodeURIComponent(abs) + hash;
|
| 18 |
+
}
|
| 19 |
+
function px(url) {
|
| 20 |
+
if (!base || url == null) return url;
|
| 21 |
+
var s = String(url).trim();
|
| 22 |
+
if (!s || /^(#|data:|blob:|javascript:|mailto:|tel:|about:)/i.test(s) || isProxied(s)) return url;
|
| 23 |
+
try {
|
| 24 |
+
var u = new URL(s, base);
|
| 25 |
+
// Code that built a URL from location.origin points at the proxy itself;
|
| 26 |
+
// re-aim it at the real site.
|
| 27 |
+
if (u.origin === ORIGIN) u = new URL(u.pathname + u.search + u.hash, base);
|
| 28 |
+
if (u.protocol !== "http:" && u.protocol !== "https:") return url;
|
| 29 |
+
return toProxy(u.href);
|
| 30 |
+
} catch (e) { return url; }
|
| 31 |
+
}
|
| 32 |
+
function pxSrcset(v) {
|
| 33 |
+
return String(v).split(",").map(function (part) {
|
| 34 |
+
var bits = part.trim().split(/\s+/);
|
| 35 |
+
if (bits[0]) bits[0] = px(bits[0]);
|
| 36 |
+
return bits.join(" ");
|
| 37 |
+
}).join(", ");
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
/* ---------------------------------------------------------- patches */
|
| 41 |
+
if (base) {
|
| 42 |
+
var _fetch = window.fetch;
|
| 43 |
+
if (_fetch) {
|
| 44 |
+
window.fetch = function (input, init) {
|
| 45 |
+
try {
|
| 46 |
+
if (typeof input === "string" || input instanceof URL) input = px(String(input));
|
| 47 |
+
else if (input && input.url) input = new Request(px(input.url), input);
|
| 48 |
+
} catch (e) {}
|
| 49 |
+
return _fetch.call(this, input, init);
|
| 50 |
+
};
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
var _xhrOpen = XMLHttpRequest.prototype.open;
|
| 54 |
+
XMLHttpRequest.prototype.open = function (method, url) {
|
| 55 |
+
var a = Array.prototype.slice.call(arguments);
|
| 56 |
+
a[1] = px(url);
|
| 57 |
+
return _xhrOpen.apply(this, a);
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
if (navigator.sendBeacon) {
|
| 61 |
+
var _beacon = navigator.sendBeacon;
|
| 62 |
+
navigator.sendBeacon = function (url, data) { return _beacon.call(this, px(url), data); };
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
var _open = window.open;
|
| 66 |
+
window.open = function () {
|
| 67 |
+
var a = Array.prototype.slice.call(arguments);
|
| 68 |
+
if (a.length) a[0] = px(a[0]);
|
| 69 |
+
return _open.apply(window, a);
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
["pushState", "replaceState"].forEach(function (fn) {
|
| 73 |
+
var orig = history[fn];
|
| 74 |
+
history[fn] = function (state, title, url) {
|
| 75 |
+
if (url != null) {
|
| 76 |
+
try {
|
| 77 |
+
var abs = new URL(String(url), base).href;
|
| 78 |
+
base = abs;
|
| 79 |
+
window.__PX_BASE__ = abs;
|
| 80 |
+
url = toProxy(abs);
|
| 81 |
+
} catch (e) {}
|
| 82 |
+
}
|
| 83 |
+
var r = orig.call(this, state, title, url);
|
| 84 |
+
syncBar();
|
| 85 |
+
return r;
|
| 86 |
+
};
|
| 87 |
+
});
|
| 88 |
+
function syncBase() {
|
| 89 |
+
var u = new URLSearchParams(location.search).get("u");
|
| 90 |
+
if (u) base = u + location.hash;
|
| 91 |
+
syncBar();
|
| 92 |
+
}
|
| 93 |
+
window.addEventListener("popstate", syncBase);
|
| 94 |
+
window.addEventListener("hashchange", syncBase);
|
| 95 |
+
|
| 96 |
+
[[HTMLImageElement, "src"], [HTMLScriptElement, "src"], [HTMLIFrameElement, "src"],
|
| 97 |
+
[HTMLMediaElement, "src"], [HTMLSourceElement, "src"], [HTMLEmbedElement, "src"],
|
| 98 |
+
[HTMLInputElement, "src"], [HTMLLinkElement, "href"], [HTMLAnchorElement, "href"],
|
| 99 |
+
[HTMLFormElement, "action"], [HTMLVideoElement, "poster"]
|
| 100 |
+
].forEach(function (pair) {
|
| 101 |
+
var d = Object.getOwnPropertyDescriptor(pair[0].prototype, pair[1]);
|
| 102 |
+
if (!d || !d.set) return;
|
| 103 |
+
Object.defineProperty(pair[0].prototype, pair[1], {
|
| 104 |
+
configurable: true, enumerable: d.enumerable, get: d.get,
|
| 105 |
+
set: function (v) { d.set.call(this, px(v)); }
|
| 106 |
+
});
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
var URL_ATTR = { src: 1, href: 1, action: 1, poster: 1, data: 1, formaction: 1 };
|
| 110 |
+
var _setAttr = Element.prototype.setAttribute;
|
| 111 |
+
Element.prototype.setAttribute = function (name, value) {
|
| 112 |
+
var n = String(name).toLowerCase();
|
| 113 |
+
if (URL_ATTR[n]) value = px(value);
|
| 114 |
+
else if (n === "srcset") value = pxSrcset(value);
|
| 115 |
+
return _setAttr.call(this, name, value);
|
| 116 |
+
};
|
| 117 |
+
|
| 118 |
+
// Markup injected as a string: rewrite URLs before the browser can start loading them.
|
| 119 |
+
function rewriteMarkup(html) {
|
| 120 |
+
var t = document.createElement("template");
|
| 121 |
+
tplSet.call(t, html);
|
| 122 |
+
t.content.querySelectorAll("[src],[href],[action],[poster],[srcset]").forEach(function (el) {
|
| 123 |
+
["src", "href", "action", "poster"].forEach(function (a) {
|
| 124 |
+
if (el.hasAttribute(a)) _setAttr.call(el, a, px(el.getAttribute(a)));
|
| 125 |
+
});
|
| 126 |
+
if (el.hasAttribute("srcset")) _setAttr.call(el, "srcset", pxSrcset(el.getAttribute("srcset")));
|
| 127 |
+
});
|
| 128 |
+
return tplGet.call(t);
|
| 129 |
+
}
|
| 130 |
+
var innerDesc = Object.getOwnPropertyDescriptor(Element.prototype, "innerHTML");
|
| 131 |
+
var tplSet = innerDesc.set, tplGet = innerDesc.get;
|
| 132 |
+
Object.defineProperty(Element.prototype, "innerHTML", {
|
| 133 |
+
configurable: true, enumerable: innerDesc.enumerable, get: innerDesc.get,
|
| 134 |
+
set: function (v) { innerDesc.set.call(this, rewriteMarkup(v)); }
|
| 135 |
+
});
|
| 136 |
+
var _iah = Element.prototype.insertAdjacentHTML;
|
| 137 |
+
Element.prototype.insertAdjacentHTML = function (pos, html) {
|
| 138 |
+
return _iah.call(this, pos, rewriteMarkup(html));
|
| 139 |
+
};
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
/* ---------------------------------------------------------- URL bar */
|
| 143 |
+
var ICON = {
|
| 144 |
+
back: '<path d="M15 18l-6-6 6-6"/>',
|
| 145 |
+
fwd: '<path d="M9 18l6-6-6-6"/>',
|
| 146 |
+
reload: '<path d="M21 12a9 9 0 1 1-3-6.7"/><path d="M21 4v5h-5"/>',
|
| 147 |
+
go: '<path d="M5 12h14"/><path d="M13 6l6 6-6 6"/>',
|
| 148 |
+
up: '<path d="M6 15l6-6 6 6"/>',
|
| 149 |
+
down: '<path d="M6 9l6 6 6-6"/>'
|
| 150 |
+
};
|
| 151 |
+
function svg(name) {
|
| 152 |
+
return '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" ' +
|
| 153 |
+
'stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' + ICON[name] + "</svg>";
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
var input, barHost;
|
| 157 |
+
function syncBar() {
|
| 158 |
+
if (input && document.activeElement !== barHost) input.value = base.split("#")[0];
|
| 159 |
+
}
|
| 160 |
+
function normalize(v) {
|
| 161 |
+
v = v.trim();
|
| 162 |
+
if (!v) return null;
|
| 163 |
+
if (/^https?:\/\//i.test(v)) return v;
|
| 164 |
+
if (!/\s/.test(v) && /^[^\s\/]+\.[^\s\/]{2,}(\/.*)?$/.test(v)) return "https://" + v;
|
| 165 |
+
return "https://duckduckgo.com/html/?q=" + encodeURIComponent(v);
|
| 166 |
+
}
|
| 167 |
+
function navigate() {
|
| 168 |
+
var url = normalize(input.value);
|
| 169 |
+
if (url) location.href = "/p?u=" + encodeURIComponent(url);
|
| 170 |
+
}
|
| 171 |
+
function mountBar() {
|
| 172 |
+
if (document.getElementById("__px_bar")) return;
|
| 173 |
+
barHost = document.createElement("div");
|
| 174 |
+
barHost.id = "__px_bar";
|
| 175 |
+
var root = barHost.attachShadow({ mode: "open" });
|
| 176 |
+
root.innerHTML =
|
| 177 |
+
"<style>" +
|
| 178 |
+
".bar{position:fixed;top:0;left:0;right:0;height:40px;box-sizing:border-box;display:flex;gap:6px;" +
|
| 179 |
+
"align-items:center;padding:0 8px;background:#1e1e1e;z-index:2147483647;font:14px system-ui,sans-serif}" +
|
| 180 |
+
"button{flex:none;width:28px;height:28px;border:0;border-radius:6px;background:#2d2d2d;color:#ddd;" +
|
| 181 |
+
"display:grid;place-items:center;cursor:pointer;padding:0}button:hover{background:#3a3a3a}" +
|
| 182 |
+
"input{flex:1;min-width:0;height:28px;border:0;border-radius:6px;padding:0 10px;background:#2d2d2d;" +
|
| 183 |
+
"color:#fff;font:inherit;outline:none}input:focus{box-shadow:0 0 0 1px #4f8cff}" +
|
| 184 |
+
".tab{position:fixed;top:6px;right:6px;z-index:2147483647;display:none;background:#1e1e1e}" +
|
| 185 |
+
"</style>" +
|
| 186 |
+
'<div class="bar"><button id="b" title="Back">' + svg("back") + "</button>" +
|
| 187 |
+
'<button id="f" title="Forward">' + svg("fwd") + "</button>" +
|
| 188 |
+
'<button id="r" title="Reload">' + svg("reload") + "</button>" +
|
| 189 |
+
'<input id="u" spellcheck="false" autocapitalize="off" placeholder="Search or enter address">' +
|
| 190 |
+
'<button id="g" title="Go">' + svg("go") + "</button>" +
|
| 191 |
+
'<button id="c" title="Hide bar">' + svg("up") + "</button></div>" +
|
| 192 |
+
'<button class="tab" id="t" title="Show bar">' + svg("down") + "</button>";
|
| 193 |
+
document.documentElement.appendChild(barHost);
|
| 194 |
+
document.documentElement.style.setProperty("margin-top", "40px", "important");
|
| 195 |
+
|
| 196 |
+
input = root.getElementById("u");
|
| 197 |
+
barHost = root.host;
|
| 198 |
+
input.value = base.split("#")[0];
|
| 199 |
+
var bar = root.querySelector(".bar"), tab = root.getElementById("t");
|
| 200 |
+
root.getElementById("b").onclick = function () { history.back(); };
|
| 201 |
+
root.getElementById("f").onclick = function () { history.forward(); };
|
| 202 |
+
root.getElementById("r").onclick = function () { location.reload(); };
|
| 203 |
+
root.getElementById("g").onclick = navigate;
|
| 204 |
+
input.addEventListener("keydown", function (e) { if (e.key === "Enter") navigate(); });
|
| 205 |
+
input.addEventListener("focus", function () { input.select(); });
|
| 206 |
+
root.getElementById("c").onclick = function () {
|
| 207 |
+
bar.style.display = "none"; tab.style.display = "grid";
|
| 208 |
+
document.documentElement.style.setProperty("margin-top", "0", "important");
|
| 209 |
+
};
|
| 210 |
+
tab.onclick = function () {
|
| 211 |
+
bar.style.display = "flex"; tab.style.display = "none";
|
| 212 |
+
document.documentElement.style.setProperty("margin-top", "40px", "important");
|
| 213 |
+
};
|
| 214 |
+
}
|
| 215 |
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", mountBar);
|
| 216 |
+
else mountBar();
|
| 217 |
+
})();
|