265 lines
9.6 KiB
Text
265 lines
9.6 KiB
Text
<script src="/uv/uv.bundle.js" transition:persist is:inline></script>
|
|
<script src="/uv.config.js" transition:persist is:inline></script>
|
|
<script is:inline>
|
|
//@ts-nocheck
|
|
let form = document.querySelector("form");
|
|
let input = document.querySelector("input");
|
|
window.navigator.serviceWorker.register("/sw.js", {
|
|
scope: window.__uv$config.prefix,
|
|
});
|
|
document.addEventListener("astro:after-swap", initForm);
|
|
function initForm() {
|
|
let formEle = document.querySelector("form");
|
|
input = document.querySelector("input");
|
|
if (formEle) formEle.addEventListener("submit", formEventListener);
|
|
}
|
|
if (form) {
|
|
form.addEventListener("submit", formEventListener);
|
|
}
|
|
|
|
async function getProxyURL() {
|
|
let preference = getProxyPreference();
|
|
let url = input.value.trim();
|
|
if (!isUrl(url)) url = getSearchEngine() + url;
|
|
else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url;
|
|
if (preference === "ultraviolet") {
|
|
return 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=/`;
|
|
}
|
|
return `/${getCookie("rammerhead-session")}/${url}`;
|
|
} else {
|
|
// Default to UV
|
|
return window.__uv$config.prefix + window.__uv$config.encodeUrl(url);
|
|
}
|
|
}
|
|
|
|
async function loadContent() {
|
|
let openWith = localStorage.getItem("alu__selectedOpenWith");
|
|
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") {
|
|
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");
|
|
loadingContent.style.opacity = 1;
|
|
|
|
let iframe = document.getElementById("proxy-frame");
|
|
let topbar = document.getElementById("top-bar");
|
|
let closeButton = document.getElementById("close-button");
|
|
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=/`;
|
|
}
|
|
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);
|
|
iframeURLChange(iframe, (newURL) => updateTopbarURL(preference, newURL));
|
|
const boundIFrameLoad = iframeLoad.bind(null, iframe, loadingContent, topbar, closeButton);
|
|
iframe.addEventListener("load", boundIFrameLoad);
|
|
|
|
function iframeLoad(iframe, loadingContent, topbar, closeButton) {
|
|
loadingContent.style.opacity = 0;
|
|
iframe.style.opacity = 1;
|
|
topbar.style.opacity = 1;
|
|
topbar.style.pointerEvents = "auto";
|
|
|
|
updateProxiedFavicon();
|
|
closeButton.onclick = () => {
|
|
iframe.style.opacity = 0;
|
|
topbar.style.opacity = 0;
|
|
iframe.style.pointerEvents = "none";
|
|
topbar.style.pointerEvents = "none";
|
|
iframe.removeEventListener("load", boundIFrameLoad);
|
|
|
|
setTimeout(() => {
|
|
iframe.src = "about:blank";
|
|
}, 500);
|
|
};
|
|
}
|
|
}
|
|
|
|
function formEventListener(event) {
|
|
event.preventDefault();
|
|
loadContent();
|
|
}
|
|
window.loadFormContent = loadContent;
|
|
|
|
function isUrl(val = "") {
|
|
if (/^http(s?):\/\//.test(val) || (val.includes(".") && val.substr(0, 1) !== " ")) return true;
|
|
return false;
|
|
}
|
|
|
|
function getCookie(name) {
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop().split(";").shift();
|
|
}
|
|
|
|
function getSearchEngine() {
|
|
let localStorageSearchEngine = localStorage.getItem("alu__search_engine");
|
|
if (!localStorageSearchEngine) {
|
|
return "https://google.com/search?q=";
|
|
}
|
|
switch (JSON.parse(localStorage.getItem("alu__search_engine")).value.toLowerCase()) {
|
|
case "google": {
|
|
return "https://google.com/search?q=";
|
|
}
|
|
case "bing": {
|
|
return "https://bing.com/search?q=d";
|
|
}
|
|
case "brave": {
|
|
return "https://search.brave.com/search?q=";
|
|
}
|
|
case "searx": {
|
|
let localStorageURL = localStorage.getItem("alu__searxngUrl");
|
|
if (localStorageURL) {
|
|
if (localStorageURL == "") return "https://searxng.site/search?q=";
|
|
// Ugly hack to remove the trailing slash :)
|
|
if (localStorageURL.endsWith("/")) localStorageURL = localStorageURL.slice(0, -1);
|
|
return localStorageURL + "/search?q=";
|
|
} else return "https://searxng.site/search?q=";
|
|
}
|
|
default: {
|
|
return "https://google.com/search?q=";
|
|
}
|
|
}
|
|
}
|
|
|
|
function getProxyPreference() {
|
|
let localStorageItem = localStorage.getItem("alu__selectedProxy");
|
|
|
|
if (!localStorageItem) return "uv";
|
|
|
|
switch (JSON.parse(localStorageItem).value.toLowerCase()) {
|
|
case "ultraviolet": {
|
|
return "ultraviolet";
|
|
}
|
|
case "rammerhead":
|
|
return "rammerhead";
|
|
case "dynamic":
|
|
// temporary because dynamic is not implemented yet :)
|
|
return "ultraviolet";
|
|
default: {
|
|
return "uv";
|
|
}
|
|
}
|
|
}
|
|
|
|
function updateProxiedFavicon(iframe) {
|
|
let proxiedFavicon = document.getElementById("proxied-favicon");
|
|
if (iframe) {
|
|
if (iframe.contentDocument) {
|
|
let favicon = iframe.contentDocument.querySelector("link[rel='icon']") || iframe.contentDocument.querySelector("link[rel*='icon']");
|
|
if (favicon) {
|
|
proxiedFavicon.src = favicon.href;
|
|
} else {
|
|
proxiedFavicon.src = "/favicon.ico";
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
setTimeout(() => updateProxiedFavicon(iframe), 400);
|
|
|
|
}
|
|
|
|
|
|
function iframeURLChange(iframe, callback) {
|
|
var lastDispatched = null;
|
|
|
|
var dispatchChange = function () {
|
|
var newHref = iframe.contentWindow.location.href;
|
|
|
|
if (newHref !== lastDispatched) {
|
|
callback(newHref);
|
|
|
|
lastDispatched = newHref;
|
|
}
|
|
};
|
|
|
|
var unloadHandler = function () {
|
|
// Timeout needed because the URL changes immediately after
|
|
// the `unload` event is dispatched.
|
|
setTimeout(dispatchChange, 0);
|
|
};
|
|
|
|
function attachUnload() {
|
|
// Remove the unloadHandler in case it was already attached.
|
|
// Otherwise, there will be two handlers, which is unnecessary.
|
|
iframe.contentWindow.removeEventListener("unload", unloadHandler);
|
|
iframe.contentWindow.addEventListener("unload", unloadHandler);
|
|
}
|
|
|
|
iframe.addEventListener("load", function () {
|
|
attachUnload();
|
|
updateProxiedFavicon(iframe);
|
|
|
|
// Just in case the change wasn't dispatched during the unload event...
|
|
dispatchChange();
|
|
});
|
|
|
|
attachUnload();
|
|
}
|
|
function updateTopbarURL(preference, newURL) {
|
|
if (newURL === "about:blank") return;
|
|
let urlText = document.getElementById("url-text");
|
|
if (urlText) {
|
|
if (preference === "rammerhead") {
|
|
let newurl = newURL.slice(
|
|
newURL.indexOf("/" + getCookie("rammerhead-session")) +
|
|
getCookie("rammerhead-session").length +
|
|
2
|
|
);
|
|
if (newurl.length > 30) newurl = newurl.slice(0, 50) + "...";
|
|
urlText.innerText = newurl;
|
|
}
|
|
else urlText.innerText = window.__uv$config.decodeUrl(newURL.split(__uv$config.prefix)[1]);
|
|
}
|
|
}
|
|
</script>
|