Fix small bug with ProxyRegistrar on first load.

This commit is contained in:
wearrrrr 2024-04-03 22:04:52 -05:00
parent 390f3e73d9
commit 74cd71e255
2 changed files with 89 additions and 85 deletions

View file

@ -54,92 +54,95 @@
async function loadContent() { async function loadContent() {
await initTransport(); await initTransport();
let openWith = localStorage.getItem("alu__selectedOpenWith"); // 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.
let currentProxy = localStorage.getItem("alu__selectedProxy"); setTimeout(async () => {
let url = input!.value.trim(); let openWith = localStorage.getItem("alu__selectedOpenWith");
if (!isUrl(url)) url = getSearchEngine() + url; let currentProxy = localStorage.getItem("alu__selectedProxy");
else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url; let url = input!.value.trim();
if (openWith) { if (!isUrl(url)) url = getSearchEngine() + url;
let openWithParsed = JSON.parse(openWith); else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url;
if ( if (openWith) {
openWithParsed.value === "newTab" || let openWithParsed = JSON.parse(openWith);
(currentProxy && JSON.parse(currentProxy).value === "rammerhead") 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"); loadingContent.style.opacity = "0";
return; iframe.style.opacity = "1";
} topbar.style.opacity = "1";
if (openWithParsed.value === "about:blank") { topbar.style.pointerEvents = "auto";
// Open about:blank window and inject iframe into it document.body.style.overflow = "hidden";
let newWindow = window.open("about:blank", "_blank"); closeButton.onclick = () => {
let newWindowDocument = newWindow!.document; iframe.style.opacity = "0";
let iframe = newWindowDocument.createElement("iframe"); topbar.style.opacity = "0";
iframe.src = await getProxyURL(); iframe.style.pointerEvents = "none";
// Inject css into the iframe topbar.style.pointerEvents = "none";
let css = newWindowDocument.createElement("link"); document.body.style.overflow = "auto";
css.rel = "stylesheet"; iframe.removeEventListener("load", boundIFrameLoad);
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; setTimeout(() => {
let topbar = document.getElementById("top-bar") as HTMLDivElement; iframe.src = "about:blank";
let closeButton = document.getElementById("close-button") as HTMLButtonElement; }, 500);
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}`; }, 100);
} 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);
};
}
} }
async function formEventListener(event: Event) { async function formEventListener(event: Event) {

View file

@ -311,9 +311,10 @@ const t = i18n.useTranslations(lang);
const useWss = location.protocol == "https:"; const useWss = location.protocol == "https:";
const webSocketProtocol = useWss ? "wss://" : "ws://"; const webSocketProtocol = useWss ? "wss://" : "ws://";
let savedWispUrl = localStorage.getItem("alu__wispUrl"); 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"); 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"); wispURLInput.value = localStorage.getItem("alu__wispUrl");
bareURLInput.value = localStorage.getItem("alu__bareUrl"); bareURLInput.value = localStorage.getItem("alu__bareUrl");
// Proxy settings // Proxy settings