diff --git a/src/components/ProxyRegistrar.astro b/src/components/ProxyRegistrar.astro index e1e8c8b..f157a84 100644 --- a/src/components/ProxyRegistrar.astro +++ b/src/components/ProxyRegistrar.astro @@ -54,92 +54,95 @@ async function loadContent() { await initTransport(); - let openWith = localStorage.getItem("alu__selectedOpenWith"); - let currentProxy = localStorage.getItem("alu__selectedProxy"); - let url = input!.value.trim(); - if (!isUrl(url)) url = getSearchEngine() + url; - else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url; - if (openWith) { - let openWithParsed = JSON.parse(openWith); - if ( - openWithParsed.value === "newTab" || - (currentProxy && JSON.parse(currentProxy).value === "rammerhead") + // The setTimeout is because service workers are a little silly and can take a while longer to register despite .then being called, which causes a bug on the first load. + setTimeout(async () => { + let openWith = localStorage.getItem("alu__selectedOpenWith"); + let currentProxy = localStorage.getItem("alu__selectedProxy"); + let url = input!.value.trim(); + if (!isUrl(url)) url = getSearchEngine() + url; + else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url; + if (openWith) { + let openWithParsed = JSON.parse(openWith); + if ( + openWithParsed.value === "newTab" || + (currentProxy && JSON.parse(currentProxy).value === "rammerhead") + ) { + window.open(await getProxyURL(), "_blank"); + return; + } + if (openWithParsed.value === "about:blank") { + // Open about:blank window and inject iframe into it + let newWindow = window.open("about:blank", "_blank"); + let newWindowDocument = newWindow!.document; + let iframe = newWindowDocument.createElement("iframe"); + iframe.src = await getProxyURL(); + // Inject css into the iframe + let css = newWindowDocument.createElement("link"); + css.rel = "stylesheet"; + css.href = "/iframe.css"; + newWindowDocument.head.appendChild(css); + newWindowDocument.body.appendChild(iframe); + return; + } + } + let loadingContent = document.getElementById("loading-content") as HTMLElement; + if (loadingContent) loadingContent.style.opacity = "1"; + + let iframe = document.getElementById("proxy-frame") as HTMLIFrameElement; + let topbar = document.getElementById("top-bar") as HTMLDivElement; + let closeButton = document.getElementById("close-button") as HTMLButtonElement; + let preference = getProxyPreference(); + if (preference === "ultraviolet") { + iframe.src = window.__uv$config.prefix + window.__uv$config.encodeUrl(url); + } else if (preference == "rammerhead") { + // Check if rammerhead-session exists in cookies + let rammerheadSession = getCookie("rammerhead-session"); + if (!rammerheadSession) { + let session = await fetch("/newsession"); + let sessionID = await session.text(); + // Disable URL shuffling on rewrite, eventually I'll try and figure out how it works, but for now, it's disabled. + await fetch("/editsession?id=" + sessionID + "&enableShuffling=0"); + // Now save it in a cookie that expires in 72 hours. + document.cookie = `rammerhead-session=${sessionID}; max-age=${60 * 60 * 72}; path=/`; + // Now add an origin_proxy cookie for our domain + document.cookie = `origin_proxy=${window.location.origin}; max-age=${60 * 60 * 72}; path=/`; + } + if (iframe) iframe.src = `/${getCookie("rammerhead-session")}/${url}`; + } else { + // Default to UV + iframe.src = window.__uv$config.prefix + window.__uv$config.encodeUrl(url); + } + iframe.style.pointerEvents = "auto"; + iframe.classList.add("proxy-frame"); + document.body.appendChild(iframe); + const boundIFrameLoad = iframeLoad.bind(null, iframe, loadingContent, topbar, closeButton); + iframe.addEventListener("load", boundIFrameLoad); + + function iframeLoad( + iframe: HTMLIFrameElement, + loadingContent: HTMLElement, + topbar: HTMLDivElement, + closeButton: HTMLButtonElement ) { - window.open(await getProxyURL(), "_blank"); - return; - } - if (openWithParsed.value === "about:blank") { - // Open about:blank window and inject iframe into it - let newWindow = window.open("about:blank", "_blank"); - let newWindowDocument = newWindow!.document; - let iframe = newWindowDocument.createElement("iframe"); - iframe.src = await getProxyURL(); - // Inject css into the iframe - let css = newWindowDocument.createElement("link"); - css.rel = "stylesheet"; - css.href = "/iframe.css"; - newWindowDocument.head.appendChild(css); - newWindowDocument.body.appendChild(iframe); - return; - } - } - let loadingContent = document.getElementById("loading-content") as HTMLElement; - if (loadingContent) loadingContent.style.opacity = "1"; + loadingContent.style.opacity = "0"; + iframe.style.opacity = "1"; + topbar.style.opacity = "1"; + topbar.style.pointerEvents = "auto"; + document.body.style.overflow = "hidden"; + closeButton.onclick = () => { + iframe.style.opacity = "0"; + topbar.style.opacity = "0"; + iframe.style.pointerEvents = "none"; + topbar.style.pointerEvents = "none"; + document.body.style.overflow = "auto"; + iframe.removeEventListener("load", boundIFrameLoad); - let iframe = document.getElementById("proxy-frame") as HTMLIFrameElement; - let topbar = document.getElementById("top-bar") as HTMLDivElement; - let closeButton = document.getElementById("close-button") as HTMLButtonElement; - let preference = getProxyPreference(); - if (preference === "ultraviolet") { - iframe.src = window.__uv$config.prefix + window.__uv$config.encodeUrl(url); - } else if (preference == "rammerhead") { - // Check if rammerhead-session exists in cookies - let rammerheadSession = getCookie("rammerhead-session"); - if (!rammerheadSession) { - let session = await fetch("/newsession"); - let sessionID = await session.text(); - // Disable URL shuffling on rewrite, eventually I'll try and figure out how it works, but for now, it's disabled. - await fetch("/editsession?id=" + sessionID + "&enableShuffling=0"); - // Now save it in a cookie that expires in 72 hours. - document.cookie = `rammerhead-session=${sessionID}; max-age=${60 * 60 * 72}; path=/`; - // Now add an origin_proxy cookie for our domain - document.cookie = `origin_proxy=${window.location.origin}; max-age=${60 * 60 * 72}; path=/`; + setTimeout(() => { + iframe.src = "about:blank"; + }, 500); + }; } - if (iframe) iframe.src = `/${getCookie("rammerhead-session")}/${url}`; - } else { - // Default to UV - iframe.src = window.__uv$config.prefix + window.__uv$config.encodeUrl(url); - } - iframe.style.pointerEvents = "auto"; - iframe.classList.add("proxy-frame"); - document.body.appendChild(iframe); - const boundIFrameLoad = iframeLoad.bind(null, iframe, loadingContent, topbar, closeButton); - iframe.addEventListener("load", boundIFrameLoad); - - function iframeLoad( - iframe: HTMLIFrameElement, - loadingContent: HTMLElement, - topbar: HTMLDivElement, - closeButton: HTMLButtonElement - ) { - loadingContent.style.opacity = "0"; - iframe.style.opacity = "1"; - topbar.style.opacity = "1"; - topbar.style.pointerEvents = "auto"; - document.body.style.overflow = "hidden"; - closeButton.onclick = () => { - iframe.style.opacity = "0"; - topbar.style.opacity = "0"; - iframe.style.pointerEvents = "none"; - topbar.style.pointerEvents = "none"; - document.body.style.overflow = "auto"; - iframe.removeEventListener("load", boundIFrameLoad); - - setTimeout(() => { - iframe.src = "about:blank"; - }, 500); - }; - } + }, 100); } async function formEventListener(event: Event) { diff --git a/src/components/SettingsTablist.astro b/src/components/SettingsTablist.astro index dc43418..5428ae0 100644 --- a/src/components/SettingsTablist.astro +++ b/src/components/SettingsTablist.astro @@ -311,9 +311,10 @@ const t = i18n.useTranslations(lang); const useWss = location.protocol == "https:"; const webSocketProtocol = useWss ? "wss://" : "ws://"; let savedWispUrl = localStorage.getItem("alu__wispUrl"); - if (savedWispUrl == null) localStorage.setItem("alu__wispUrl", webSocketProtocol + location.host + "/wisp/"); + if (savedWispUrl == null) + localStorage.setItem("alu__wispUrl", webSocketProtocol + location.host + "/wisp/"); let savedBareUrl = localStorage.getItem("alu__bareUrl"); - if (savedBareUrl == null) localStorage.setItem("alu__bareUrl", location.origin + "/bare/") + if (savedBareUrl == null) localStorage.setItem("alu__bareUrl", location.origin + "/bare/"); wispURLInput.value = localStorage.getItem("alu__wispUrl"); bareURLInput.value = localStorage.getItem("alu__bareUrl"); // Proxy settings