mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
delete useless globals
This commit is contained in:
parent
4ce4226afa
commit
7f0663425a
5 changed files with 30 additions and 31 deletions
|
@ -15,8 +15,6 @@ import "./scope.ts";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
__location: Location;
|
|
||||||
__window: Window;
|
|
||||||
//@ts-ignore scope function cant be typed
|
//@ts-ignore scope function cant be typed
|
||||||
__s: any;
|
__s: any;
|
||||||
}
|
}
|
||||||
|
@ -37,4 +35,4 @@ for (const script of scripts) {
|
||||||
|
|
||||||
script.insertAdjacentElement("afterend", clone);
|
script.insertAdjacentElement("afterend", clone);
|
||||||
script.remove();
|
script.remove();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,28 +7,24 @@ function urlLocation() {
|
||||||
loc.reload = () => location.reload();
|
loc.reload = () => location.reload();
|
||||||
loc.replace = (url: string) => location.replace(encodeUrl(url));
|
loc.replace = (url: string) => location.replace(encodeUrl(url));
|
||||||
loc.toString = () => loc.href;
|
loc.toString = () => loc.href;
|
||||||
|
|
||||||
return loc;
|
return loc;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LocationProxy() {
|
const loc = urlLocation();
|
||||||
const loc = urlLocation();
|
export const locationProxy = new Proxy(window.location, {
|
||||||
|
get(target, prop) {
|
||||||
|
return loc[prop];
|
||||||
|
},
|
||||||
|
|
||||||
return new Proxy(window.location, {
|
set(obj, prop, value) {
|
||||||
get(target, prop) {
|
if (prop === "href") {
|
||||||
return loc[prop];
|
location.href = encodeUrl(value);
|
||||||
},
|
} else {
|
||||||
|
loc[prop] = value;
|
||||||
set(obj, prop, value) {
|
|
||||||
if (prop === "href") {
|
|
||||||
location.href = encodeUrl(value);
|
|
||||||
} else {
|
|
||||||
loc[prop] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
window.__location = LocationProxy();
|
return true;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
|
import { locationProxy } from "./location";
|
||||||
|
import { windowProxy } from "./window";
|
||||||
|
|
||||||
function scope(identifier: any) {
|
function scope(identifier: any) {
|
||||||
if (identifier instanceof Window) {
|
if (identifier instanceof Window) {
|
||||||
return window.__window;
|
return windowProxy;
|
||||||
} else if (identifier instanceof Location) {
|
} else if (identifier instanceof Location) {
|
||||||
return window.__location;
|
return locationProxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
// shorthand because this can get out of hand reall quickly
|
// shorthand because this can get out of hand reall quickly
|
||||||
window.__s = scope;
|
window.__s = scope;
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import IDBMapSync from "@webreflection/idb-map/sync";
|
import IDBMapSync from "@webreflection/idb-map/sync";
|
||||||
const store = new IDBMapSync(window.__location.host, {
|
import { locationProxy } from "./location";
|
||||||
|
|
||||||
|
const store = new IDBMapSync(locationProxy.host, {
|
||||||
prefix: "Storage",
|
prefix: "Storage",
|
||||||
durability: "relaxed"
|
durability: "relaxed"
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
const windowProxy = new Proxy(window, {
|
import { locationProxy } from "./location";
|
||||||
|
|
||||||
|
export const windowProxy = new Proxy(window, {
|
||||||
get(target, prop) {
|
get(target, prop) {
|
||||||
const propIsString = typeof prop === "string";
|
const propIsString = typeof prop === "string";
|
||||||
if (propIsString && prop === "location") {
|
if (propIsString && prop === "location") {
|
||||||
return target.__location;
|
return locationProxy;
|
||||||
} else if (propIsString && ["window", "top", "parent", "self", "globalThis"].includes(prop)) {
|
} else if (propIsString && ["window", "top", "parent", "self", "globalThis"].includes(prop)) {
|
||||||
return target.__window;
|
return windowProxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
return target[prop];
|
return target[prop];
|
||||||
|
@ -19,5 +21,3 @@ const windowProxy = new Proxy(window, {
|
||||||
return Reflect.set(target, prop, newValue);
|
return Reflect.set(target, prop, newValue);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
window.__window = windowProxy;
|
|
Loading…
Add table
Add a link
Reference in a new issue