Download shim.js from Sebebeb/Forge: direct link, hf CLI and curl.
- Browser
- Download file 10.3 kB
-
https://huggingface.co/spaces/Sebebeb/Forge/resolve/main/shim.js
- Command line
-
hf download hf://spaces/Sebebeb/Forge/shim.js
-
curl -L -o shim.js https://huggingface.co/spaces/Sebebeb/Forge/resolve/main/shim.js
10.3 kB
| /* 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: '<path d="M15 18l-6-6 6-6"/>', | |
| fwd: '<path d="M9 18l6-6-6-6"/>', | |
| reload: '<path d="M21 12a9 9 0 1 1-3-6.7"/><path d="M21 4v5h-5"/>', | |
| go: '<path d="M5 12h14"/><path d="M13 6l6 6-6 6"/>', | |
| up: '<path d="M6 15l6-6 6 6"/>', | |
| down: '<path d="M6 9l6 6 6-6"/>', | |
| code: '<path d="M8 6l-6 6 6 6"/><path d="M16 6l6 6-6 6"/>' | |
| }; | |
| function svg(name) { | |
| return '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" ' + | |
| 'stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' + ICON[name] + "</svg>"; | |
| } | |
| 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 = | |
| "<style>" + | |
| ".bar{position:fixed;top:0;left:0;right:0;height:40px;box-sizing:border-box;display:flex;gap:6px;" + | |
| "align-items:center;padding:0 8px;background:#1e1e1e;z-index:2147483647;font:14px system-ui,sans-serif}" + | |
| "button{flex:none;width:28px;height:28px;border:0;border-radius:6px;background:#2d2d2d;color:#ddd;" + | |
| "display:grid;place-items:center;cursor:pointer;padding:0}button:hover{background:#3a3a3a}" + | |
| "input{flex:1;min-width:0;height:28px;border:0;border-radius:6px;padding:0 10px;background:#2d2d2d;" + | |
| "color:#fff;font:inherit;outline:none}input:focus{box-shadow:0 0 0 1px #4f8cff}" + | |
| "button.on{background:#4f8cff;color:#fff}" + | |
| ".tab{position:fixed;top:6px;right:6px;z-index:2147483647;display:none;background:#1e1e1e}" + | |
| "</style>" + | |
| '<div class="bar"><button id="b" title="Back">' + svg("back") + "</button>" + | |
| '<button id="f" title="Forward">' + svg("fwd") + "</button>" + | |
| '<button id="r" title="Reload">' + svg("reload") + "</button>" + | |
| '<input id="u" spellcheck="false" autocapitalize="off" placeholder="Search or enter address">' + | |
| '<button id="g" title="Go">' + svg("go") + "</button>" + | |
| '<button id="j">' + svg("code") + "</button>" + | |
| '<button id="c" title="Hide bar">' + svg("up") + "</button></div>" + | |
| '<button class="tab" id="t" title="Show bar">' + svg("down") + "</button>"; | |
| 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(); | |
| })(); |