Begin migration to Alu.store.get for fetching values, stored in global Alu namespace.

This commit is contained in:
wearrrrr 2024-07-29 18:52:09 -05:00
parent 5a48e2d71e
commit d41b427266
15 changed files with 82 additions and 106 deletions

11
src/alu.d.ts vendored Normal file
View file

@ -0,0 +1,11 @@
export declare global {
namespace Alu {
let store: AluStore;
type DefaultKeys = {
[key: string]: AluKey;
};
type Key = Record<string, string>;
type ValidStoreKeys = "proxy" | "search" | "openpage" | "wisp" | "bareUrl" | "transport" | "searxng" | "theme" | "lang" | "cloak";
}
}

View file

@ -1,20 +1,19 @@
<script> <script>
function loadCloak() { function loadCloak() {
const localStorageCloak = localStorage.getItem("alu__selectedCloak"); const cloak = Alu.store.get("cloak");
if (localStorageCloak) { if (cloak) {
const parsedCloak = JSON.parse(localStorageCloak); if (cloak) {
if (parsedCloak) { if (cloak.name != "None") {
if (parsedCloak.name != "None") { document.title = cloak.name;
document.title = parsedCloak.name;
let link = document.querySelector("link[rel~='icon']") as HTMLLinkElement; let link = document.querySelector("link[rel~='icon']") as HTMLLinkElement;
if (!link) { if (!link) {
link = document.createElement("link"); link = document.createElement("link");
link.rel = "icon"; link.rel = "icon";
} }
if (!parsedCloak.icon.startsWith("http")) { if (!cloak.icon.startsWith("http")) {
parsedCloak.icon = window.location.origin + parsedCloak.icon; cloak.icon = window.location.origin + cloak.icon;
} }
link.href = `/custom-favicon?url=${parsedCloak.icon}`; link.href = `/custom-favicon?url=${cloak.icon}`;
document.getElementsByTagName("head")[0].appendChild(link); document.getElementsByTagName("head")[0].appendChild(link);
} }
} }

View file

@ -94,18 +94,17 @@
await loadPageExtensions(); await loadPageExtensions();
// 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. // 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 () => { setTimeout(async () => {
const openWith = localStorage.getItem("alu__selectedOpenWith"); const openWith = Alu.store.get("openpage");
const currentProxy = localStorage.getItem("alu__selectedProxy"); const proxy = Alu.store.get("proxy");
let url = input!.value.trim(); let url = input!.value.trim();
if (!isUrl(url)) url = getSearchEngine() + url; if (!isUrl(url)) url = getSearchEngine() + url;
else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url; else if (!(url.startsWith("https://") || url.startsWith("http://"))) url = "http://" + url;
if (openWith) { if (openWith) {
const openWithParsed = JSON.parse(openWith); if (openWith.value === "newTab" || proxy.value === "rammerhead") {
if (openWithParsed.value === "newTab" || (currentProxy && JSON.parse(currentProxy).value === "rammerhead")) {
window.open(await getProxyURL(), "_blank"); window.open(await getProxyURL(), "_blank");
return; return;
} }
if (openWithParsed.value === "about:blank") { if (openWith.value === "about:blank") {
// Open about:blank window and inject iframe into it // Open about:blank window and inject iframe into it
const newWindow = window.open("about:blank", "_blank"); const newWindow = window.open("about:blank", "_blank");
const newWindowDocument = newWindow!.document; const newWindowDocument = newWindow!.document;
@ -192,9 +191,8 @@
} }
}; };
shareButton.onclick = () => { shareButton.onclick = () => {
const currentProxy = localStorage.getItem("alu__selectedProxy");
const proxyFrame = document.getElementById("proxy-frame") as HTMLIFrameElement; const proxyFrame = document.getElementById("proxy-frame") as HTMLIFrameElement;
if (currentProxy && JSON.parse(currentProxy).value === "rammerhead") { if (proxy.value === "rammerhead") {
navigator.clipboard.writeText(window.location.origin + "/" + getCookie("rammerhead-session") + "/" + input!.value.trim()); navigator.clipboard.writeText(window.location.origin + "/" + getCookie("rammerhead-session") + "/" + input!.value.trim());
} else { } else {
navigator.clipboard.writeText(getUVProxyURL(proxyFrame)); navigator.clipboard.writeText(getUVProxyURL(proxyFrame));
@ -239,11 +237,8 @@
} }
function getSearchEngine() { function getSearchEngine() {
const localStorageSearchEngine = localStorage.getItem("alu__search_engine"); const searchEngine = Alu.store.get("search");
if (!localStorageSearchEngine) { switch (searchEngine.value.toLowerCase()) {
return "https://google.com/search?q=";
}
switch (JSON.parse(localStorage.getItem("alu__search_engine") as string).value.toLowerCase()) {
case "google": { case "google": {
return "https://google.com/search?q="; return "https://google.com/search?q=";
} }
@ -254,13 +249,10 @@
return "https://search.brave.com/search?q="; return "https://search.brave.com/search?q=";
} }
case "searx": { case "searx": {
let localStorageURL = localStorage.getItem("alu__searxngUrl"); const searxngURL = Alu.store.get("searxng");
if (localStorageURL) { // Ugly hack to remove the trailing slash :)
if (localStorageURL == "") return "https://searxng.site/search?q="; if (searxngURL.value.endsWith("/")) searxngURL.value = searxngURL.value.slice(0, -1);
// Ugly hack to remove the trailing slash :) return searxngURL.value + "/search?q=";
if (localStorageURL.endsWith("/")) localStorageURL = localStorageURL.slice(0, -1);
return localStorageURL + "/search?q=";
} else return "https://searxng.site/search?q=";
} }
default: { default: {
return "https://google.com/search?q="; return "https://google.com/search?q=";
@ -269,17 +261,14 @@
} }
function getProxyPreference() { function getProxyPreference() {
const localStorageItem = localStorage.getItem("alu__selectedProxy"); const proxy = Alu.store.get("proxy");
switch (proxy.value) {
if (!localStorageItem) return "uv";
switch (JSON.parse(localStorageItem).value.toLowerCase()) {
case "ultraviolet": case "ultraviolet":
return "ultraviolet"; return "ultraviolet";
case "rammerhead": case "rammerhead":
return "rammerhead"; return "rammerhead";
default: default:
return "uv"; return "ultraviolet";
} }
} }

View file

@ -2,18 +2,8 @@
import IDBManager from "@components/ts/IDBManager"; import IDBManager from "@components/ts/IDBManager";
function switchTheme() { function switchTheme() {
const currentTheme = localStorage.getItem("alu__selectedTheme"); const currentTheme = Alu.store.get("theme");
document.documentElement.setAttribute("data-theme", currentTheme.value);
if (currentTheme) {
document.documentElement.setAttribute("data-theme", JSON.parse(currentTheme).value.toLowerCase());
const footer = document.getElementById("footer");
if (footer) {
footer.dataset.theme = JSON.parse(currentTheme).value.toLowerCase();
}
} else {
localStorage.setItem("alu__selectedTheme", JSON.stringify({ name: "Alu", value: "alu" }));
switchTheme();
}
} }
switchTheme(); switchTheme();

View file

@ -1,4 +1,4 @@
const KEYSTORE: AluDefaultKeys = { const KEYSTORE: Alu.DefaultKeys = {
proxy: { proxy: {
name: "Ultraviolet", name: "Ultraviolet",
value: "ultraviolet", value: "ultraviolet",
@ -13,16 +13,12 @@ const KEYSTORE: AluDefaultKeys = {
}, },
wisp: { wisp: {
name: "Alu (US)", name: "Alu (US)",
value: "alu", value: "wss://aluu.xyz/wisp/",
}, },
bareUrl: { bareUrl: {
name: `${window.location.protocol}//${window.location.host}/bare/`, name: `${window.location.protocol}//${window.location.host}/bare/`,
value: `${window.location.protocol}//${window.location.host}/bare/`, value: `${window.location.protocol}//${window.location.host}/bare/`,
}, },
theme: {
name: "Alu",
value: "alu",
},
transport: { transport: {
name: "Epoxy", name: "Epoxy",
value: "epoxy", value: "epoxy",
@ -31,6 +27,14 @@ const KEYSTORE: AluDefaultKeys = {
name: "https://searxng.site", name: "https://searxng.site",
value: "https://searxng.site", value: "https://searxng.site",
}, },
theme: {
name: "Alu",
value: "alu",
},
lang: {
name: "English",
value: "en",
},
}; };
if (localStorage.getItem("AluStore") === null) { if (localStorage.getItem("AluStore") === null) {
@ -38,7 +42,7 @@ if (localStorage.getItem("AluStore") === null) {
} }
class AluStore { class AluStore {
private store: AluDefaultKeys = KEYSTORE; private store: Alu.DefaultKeys;
constructor() { constructor() {
const localstore = localStorage.getItem("AluStore"); const localstore = localStorage.getItem("AluStore");
if (!localstore) { if (!localstore) {
@ -46,19 +50,24 @@ class AluStore {
} }
this.store = JSON.parse(localStorage.getItem("AluStore") || "{}"); this.store = JSON.parse(localStorage.getItem("AluStore") || "{}");
} }
public getStore(): AluDefaultKeys { public getStore(): Alu.DefaultKeys {
return this.store; return this.store;
} }
public get(key: string): AluKey { public get(key: Alu.ValidStoreKeys): Alu.Key {
return this.store[key]; return this.store[key];
} }
public set(key: string, value: AluKey): void { public set(key: Alu.ValidStoreKeys, value: Alu.Key): void {
this.store[key] = value; this.store[key] = value;
this.save(); this.save();
} }
public reset(key: Alu.ValidStoreKeys) {
this.set(key, KEYSTORE[key]);
}
private save(): void { private save(): void {
localStorage.setItem("AluStore", JSON.stringify(this.store)); localStorage.setItem("AluStore", JSON.stringify(this.store));
} }
} }
window.AluStore = new AluStore(); globalThis.Alu = {
store: new AluStore(),
};

View file

@ -1,38 +1,25 @@
import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; import { BareMuxConnection } from "@mercuryworkshop/bare-mux";
type transportConfig = type transportConfig = {
| { wisp: string;
wisp: string; };
wasm?: string;
}
| string;
export const wispURLDefault = (location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/wisp/";
export default class TransportManager { export default class TransportManager {
private transport: Alu.Key;
connection: BareMuxConnection; connection: BareMuxConnection;
private transport: string = "/epoxy/index.mjs";
constructor(transport?: string) { constructor(transport?: Alu.Key) {
this.connection = new BareMuxConnection("/baremux/worker.js"); this.connection = new BareMuxConnection("/baremux/worker.js");
if (transport) { if (transport) {
this.transport = transport; this.transport = transport;
} }
if (localStorage.getItem("alu__selectedTransport") != null && !transport) { this.transport = Alu.store.get("transport");
const selectedTransport = JSON.parse(localStorage.getItem("alu__selectedTransport")!) as { value: string };
this.transport = selectedTransport.value;
}
if (localStorage.getItem("alu__selectedTransport") == null) {
// Set the default transport for the next reload.
localStorage.setItem("alu__selectedTransport", JSON.stringify({ value: this.transport }));
}
} }
async updateTransport() { async updateTransport() {
try { try {
const selectedTransport = JSON.parse(localStorage.getItem("alu__selectedTransport")!) as { value: string }; const selectedTransport = Alu.store.get("transport");
await this.setTransport(selectedTransport.value); await this.setTransport(selectedTransport);
} catch { } catch {
console.log("Failed to update transport! Falling back to old transport."); throw new Error("Failed to update transport! Try reloading.");
await this.setTransport(this.transport);
} }
} }
@ -40,20 +27,24 @@ export default class TransportManager {
return this.transport; return this.transport;
} }
async setTransport(transport: string, wispURL = wispURLDefault) { async setTransport(transport: Alu.Key, wispURL: string = Alu.store.get("wisp").value) {
this.transport = transport; this.transport = transport;
let transportConfig: transportConfig = { wisp: wispURL }; const transportConfig: transportConfig = { wisp: wispURL };
if (this.transport == "/baremod/index.mjs") { if (this.transport.value == "/baremod/index.mjs") {
transportConfig = localStorage.getItem("alu__bareUrl") || window.location.origin + "/bare/"; return await this.connection.setTransport(transport.value, [Alu.store.get("bareUrl").value]);
} }
await this.connection.setTransport(transport, [transportConfig]); await this.connection.setTransport(transport.value, [transportConfig]);
} }
} }
export const TransportMgr = new TransportManager(); export const TransportMgr = new TransportManager();
export async function initTransport() {
await TransportMgr.setTransport(TransportMgr.getTransport(), Alu.store.get("wisp").value);
}
export async function registerAndUpdateSW(): Promise<void> { export async function registerAndUpdateSW(): Promise<void> {
try { try {
const reg = await navigator.serviceWorker.register("/sw.js", { const reg = await navigator.serviceWorker.register("/sw.js", {
@ -65,7 +56,3 @@ export async function registerAndUpdateSW(): Promise<void> {
console.error("Service worker registration failed: ", err); console.error("Service worker registration failed: ", err);
} }
} }
export async function initTransport() {
await TransportMgr.setTransport(TransportMgr.getTransport(), localStorage.getItem("alu__wispUrl") || wispURLDefault);
}

View file

@ -167,12 +167,9 @@ async function uninstallExtension(slug: string): Promise<InstallReturn> {
reject({ code: EXT_RETURN.INSTALL_FAILED, slug: slug }); reject({ code: EXT_RETURN.INSTALL_FAILED, slug: slug });
} }
if (ext.result.type === "theme") { if (ext.result.type === "theme") {
const currTheme = localStorage.getItem("alu__selectedTheme"); const theme = Alu.store.get("theme");
if (currTheme) { if (theme.value == ext.result.themeName) {
if (JSON.parse(currTheme).value == ext.result.themeName) { Alu.store.reset("theme");
console.log("Reverting theme to default!");
localStorage.setItem("alu__selectedTheme", JSON.stringify({ name: "Alu", value: "alu" }));
}
} }
} }
const deleteRequest = store.delete(slug); const deleteRequest = store.delete(slug);

View file

@ -1,9 +1,9 @@
import en from "./en.json"; import en from "./locale/en.json";
import es from "./es.json"; import es from "./locale/es.json";
import fr from "./fr.json"; import fr from "./locale/fr.json";
import zh from "./zh.json"; import zh from "./locale/zh.json";
import jp from "./jp.json"; import jp from "./locale/jp.json";
import ru from "./ru.json"; import ru from "./locale/ru.json";
export const defaultLang = "en"; export const defaultLang = "en";

6
src/types.d.ts vendored
View file

@ -76,9 +76,3 @@ type WispData = {
server: WispServer; server: WispServer;
time: number; time: number;
}; };
type AluKey = Record<string, string>;
type AluDefaultKeys = {
[key: string]: AluKey;
};