Skip to content

Instantly share code, notes, and snippets.

@piksel
Created February 23, 2024 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piksel/2f50134eea24d4626cbf68a1cbac605c to your computer and use it in GitHub Desktop.
Save piksel/2f50134eea24d4626cbf68a1cbac605c to your computer and use it in GitHub Desktop.
Fake Window/Location
class FakeWindow implements Window {
[index: number]: Window;
get clientInformation(): Navigator {
console.log('Called get clientInformation');
return {} as Navigator;
}
closed: boolean = false;
customElements: CustomElementRegistry = {} as CustomElementRegistry;
devicePixelRatio: number = globalThis.devicePixelRatio;
document: Document = globalThis.document;
event: Event | undefined;
external: External = globalThis.external;
frameElement: Element | null = null;
frames: Window = globalThis.frames;
history: History = globalThis.history;
innerHeight: number = globalThis.innerHeight;
innerWidth: number = globalThis.innerWidth;
length: number = globalThis.length;
_loc: Location = new FakeLocation();
get location(): Location {
console.error('called get location');
return this._loc;
}
set location(href: Location) {
console.warn('ignored set location to %o', href);
}
locationbar: BarProp = globalThis.locationbar;
menubar: BarProp = globalThis.menubar;
name: string = globalThis.window.name;
navigator: Navigator = globalThis.navigator;
ondevicemotion: ((this: Window, ev: DeviceMotionEvent) => any) | null = null;
ondeviceorientation:
| ((this: Window, ev: DeviceOrientationEvent) => any)
| null = null;
onorientationchange: ((this: Window, ev: Event) => any) | null = null;
opener: any = globalThis.window.opener;
orientation: number = globalThis.window.orientation;
outerHeight: number = globalThis.window.outerHeight;
outerWidth: number = globalThis.window.outerWidth;
pageXOffset: number = globalThis.window.pageXOffset;
pageYOffset: number = globalThis.window.pageYOffset;
parent: Window = globalThis.window.parent;
personalbar: BarProp = globalThis.window.personalbar;
screen: Screen = globalThis.window.screen;
screenLeft: number = globalThis.window.screenLeft;
screenTop: number = globalThis.window.screenTop;
screenX: number = globalThis.window.screenX;
screenY: number = globalThis.window.screenY;
scrollX: number = globalThis.window.scrollX;
scrollY: number = globalThis.window.scrollY;
scrollbars: BarProp = globalThis.window.scrollbars;
self: Window & typeof globalThis = globalThis.window.self;
speechSynthesis: SpeechSynthesis = globalThis.window.speechSynthesis;
status: string = globalThis.window.status;
statusbar: BarProp = globalThis.window.statusbar;
toolbar: BarProp = globalThis.window.toolbar;
top: Window | null = globalThis.window.top;
visualViewport: VisualViewport | null = globalThis.window.visualViewport;
window: Window & typeof globalThis = globalThis.window.window;
alert(message?: any): void {
console.error('called method');
throw new Error('Method not implemented.');
}
blur(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
cancelIdleCallback(handle: number): void {
console.error('called method');
throw new Error('Method not implemented.');
}
captureEvents(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
close(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
confirm(message?: string | undefined): boolean {
console.error('called method');
throw new Error('Method not implemented.');
}
focus(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
getComputedStyle(
elt: Element,
pseudoElt?: string | null | undefined
): CSSStyleDeclaration {
console.error('called method');
throw new Error('Method not implemented.');
}
getSelection(): Selection | null {
console.error('called method');
throw new Error('Method not implemented.');
}
matchMedia(query: string): MediaQueryList {
console.error('called method');
throw new Error('Method not implemented.');
}
moveBy(x: number, y: number): void {
console.error('called method');
throw new Error('Method not implemented.');
}
moveTo(x: number, y: number): void {
console.error('called method');
throw new Error('Method not implemented.');
}
open(
url?: string | URL | undefined,
target?: string | undefined,
features?: string | undefined
): Window | null {
console.error('called method');
throw new Error('Method not implemented.');
}
postMessage(
message: any,
targetOrigin: string,
transfer?: Transferable[] | undefined
): void;
postMessage(
message: any,
options?: WindowPostMessageOptions | undefined
): void;
postMessage(
message: unknown,
targetOrigin?: unknown,
transfer?: unknown
): void {
console.error('called method');
throw new Error('Method not implemented.');
}
print(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
prompt(
message?: string | undefined,
_default?: string | undefined
): string | null {
console.error('called method');
throw new Error('Method not implemented.');
}
releaseEvents(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
requestIdleCallback(
callback: IdleRequestCallback,
options?: IdleRequestOptions | undefined
): number {
console.error('called method');
throw new Error('Method not implemented.');
}
resizeBy(x: number, y: number): void {
console.error('called method');
throw new Error('Method not implemented.');
}
resizeTo(width: number, height: number): void {
console.error('called method');
throw new Error('Method not implemented.');
}
scroll(options?: ScrollToOptions | undefined): void;
scroll(x: number, y: number): void;
scroll(x?: unknown, y?: unknown): void {
console.error('called method');
throw new Error('Method not implemented.');
}
scrollBy(options?: ScrollToOptions | undefined): void;
scrollBy(x: number, y: number): void;
scrollBy(x?: unknown, y?: unknown): void {
console.error('called method');
throw new Error('Method not implemented.');
}
scrollTo(options?: ScrollToOptions | undefined): void;
scrollTo(x: number, y: number): void;
scrollTo(x?: unknown, y?: unknown): void {
console.error('called method');
throw new Error('Method not implemented.');
}
stop(): void {
console.error('called method');
throw new Error('Method not implemented.');
}
addEventListener<K extends keyof WindowEventMap>(
type: K,
listener: (this: Window, ev: WindowEventMap[K]) => any,
options?: boolean | AddEventListenerOptions | undefined
): void;
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions | undefined
): void;
addEventListener(type: unknown, listener: unknown, options?: unknown): void {
console.error('called method');
throw new Error('Method not implemented.');
}
removeEventListener<K extends keyof WindowEventMap>(
type: K,
listener: (this: Window, ev: WindowEventMap[K]) => any,
options?: boolean | EventListenerOptions | undefined
): void;
removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions | undefined
): void;
removeEventListener(
type: unknown,
listener: unknown,
options?: unknown
): void {
console.error('called method');
throw new Error('Method not implemented.');
}
__TAURI_IPC__: (message: any) => void = globalThis.window.__TAURI_IPC__;
ipc: { postMessage: (args: string) => void } = globalThis.window.ipc;
__TAURI__: { convertFileSrc: (src: string, protocol: string) => string } =
globalThis.window.__TAURI__;
__TAURI_METADATA__: { __windows: WindowDef[]; __currentWindow: WindowDef } =
globalThis.window.__TAURI_METADATA__;
dispatchEvent(event: Event): boolean {
console.error('called method');
throw new Error('Method not implemented.');
}
cancelAnimationFrame(handle: number): void {
console.error('called method');
throw new Error('Method not implemented.');
}
requestAnimationFrame(callback: FrameRequestCallback): number {
console.error('called method');
throw new Error('Method not implemented.');
}
onabort: ((this: GlobalEventHandlers, ev: UIEvent) => any) | null = null;
onanimationcancel:
| ((this: GlobalEventHandlers, ev: AnimationEvent) => any)
| null = null;
onanimationend:
| ((this: GlobalEventHandlers, ev: AnimationEvent) => any)
| null = null;
onanimationiteration:
| ((this: GlobalEventHandlers, ev: AnimationEvent) => any)
| null = null;
onanimationstart:
| ((this: GlobalEventHandlers, ev: AnimationEvent) => any)
| null = null;
onauxclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onbeforeinput: ((this: GlobalEventHandlers, ev: InputEvent) => any) | null =
null;
onblur: ((this: GlobalEventHandlers, ev: FocusEvent) => any) | null = null;
oncancel: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
oncanplay: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
oncanplaythrough: ((this: GlobalEventHandlers, ev: Event) => any) | null =
null;
onchange: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null = null;
onclose: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
oncontextmenu: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
oncopy: ((this: GlobalEventHandlers, ev: ClipboardEvent) => any) | null =
null;
oncuechange: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
oncut: ((this: GlobalEventHandlers, ev: ClipboardEvent) => any) | null = null;
ondblclick: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
ondrag: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null = null;
ondragend: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null = null;
ondragenter: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null =
null;
ondragleave: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null =
null;
ondragover: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null = null;
ondragstart: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null =
null;
ondrop: ((this: GlobalEventHandlers, ev: DragEvent) => any) | null = null;
ondurationchange: ((this: GlobalEventHandlers, ev: Event) => any) | null =
null;
onemptied: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onended: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onerror: OnErrorEventHandler = (event, source, lineno, colno, error) => {
console.error(
'FakeWin: %o, Error: %o',
{ event, source, lineno, colno },
error
);
};
onfocus: ((this: GlobalEventHandlers, ev: FocusEvent) => any) | null = null;
onformdata: ((this: GlobalEventHandlers, ev: FormDataEvent) => any) | null =
null;
ongotpointercapture:
| ((this: GlobalEventHandlers, ev: PointerEvent) => any)
| null = null;
oninput: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
oninvalid: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onkeydown: ((this: GlobalEventHandlers, ev: KeyboardEvent) => any) | null =
null;
onkeypress: ((this: GlobalEventHandlers, ev: KeyboardEvent) => any) | null =
null;
onkeyup: ((this: GlobalEventHandlers, ev: KeyboardEvent) => any) | null =
null;
onload: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onloadeddata: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onloadedmetadata: ((this: GlobalEventHandlers, ev: Event) => any) | null =
null;
onloadstart: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onlostpointercapture:
| ((this: GlobalEventHandlers, ev: PointerEvent) => any)
| null = null;
onmousedown: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onmouseenter: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onmouseleave: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onmousemove: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onmouseout: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onmouseover: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null =
null;
onmouseup: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null = null;
onpaste: ((this: GlobalEventHandlers, ev: ClipboardEvent) => any) | null =
null;
onpause: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onplay: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onplaying: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onpointercancel:
| ((this: GlobalEventHandlers, ev: PointerEvent) => any)
| null = null;
onpointerdown: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null =
null;
onpointerenter:
| ((this: GlobalEventHandlers, ev: PointerEvent) => any)
| null = null;
onpointerleave:
| ((this: GlobalEventHandlers, ev: PointerEvent) => any)
| null = null;
onpointermove: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null =
null;
onpointerout: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null =
null;
onpointerover: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null =
null;
onpointerup: ((this: GlobalEventHandlers, ev: PointerEvent) => any) | null =
null;
onprogress:
| ((this: GlobalEventHandlers, ev: ProgressEvent<EventTarget>) => any)
| null = null;
onratechange: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onreset: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onresize: ((this: GlobalEventHandlers, ev: UIEvent) => any) | null = null;
onscroll: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onscrollend: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onsecuritypolicyviolation:
| ((this: GlobalEventHandlers, ev: SecurityPolicyViolationEvent) => any)
| null = null;
onseeked: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onseeking: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onselect: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onselectionchange: ((this: GlobalEventHandlers, ev: Event) => any) | null =
null;
onselectstart: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onslotchange: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onstalled: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onsubmit: ((this: GlobalEventHandlers, ev: SubmitEvent) => any) | null = null;
onsuspend: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
ontimeupdate: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
ontoggle: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
ontouchcancel?:
| ((this: GlobalEventHandlers, ev: TouchEvent) => any)
| null
| undefined;
ontouchend?:
| ((this: GlobalEventHandlers, ev: TouchEvent) => any)
| null
| undefined;
ontouchmove?:
| ((this: GlobalEventHandlers, ev: TouchEvent) => any)
| null
| undefined;
ontouchstart?:
| ((this: GlobalEventHandlers, ev: TouchEvent) => any)
| null
| undefined;
ontransitioncancel:
| ((this: GlobalEventHandlers, ev: TransitionEvent) => any)
| null = null;
ontransitionend:
| ((this: GlobalEventHandlers, ev: TransitionEvent) => any)
| null = null;
ontransitionrun:
| ((this: GlobalEventHandlers, ev: TransitionEvent) => any)
| null = null;
ontransitionstart:
| ((this: GlobalEventHandlers, ev: TransitionEvent) => any)
| null = null;
onvolumechange: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onwaiting: ((this: GlobalEventHandlers, ev: Event) => any) | null = null;
onwebkitanimationend: ((this: GlobalEventHandlers, ev: Event) => any) | null =
null;
onwebkitanimationiteration:
| ((this: GlobalEventHandlers, ev: Event) => any)
| null = null;
onwebkitanimationstart:
| ((this: GlobalEventHandlers, ev: Event) => any)
| null = null;
onwebkittransitionend:
| ((this: GlobalEventHandlers, ev: Event) => any)
| null = null;
onwheel: ((this: GlobalEventHandlers, ev: WheelEvent) => any) | null = null;
onafterprint: ((this: WindowEventHandlers, ev: Event) => any) | null = null;
onbeforeprint: ((this: WindowEventHandlers, ev: Event) => any) | null = null;
onbeforeunload:
| ((this: WindowEventHandlers, ev: BeforeUnloadEvent) => any)
| null = null;
ongamepadconnected:
| ((this: WindowEventHandlers, ev: GamepadEvent) => any)
| null = null;
ongamepaddisconnected:
| ((this: WindowEventHandlers, ev: GamepadEvent) => any)
| null = null;
onhashchange:
| ((this: WindowEventHandlers, ev: HashChangeEvent) => any)
| null = null;
onlanguagechange: ((this: WindowEventHandlers, ev: Event) => any) | null =
null;
onmessage:
| ((this: WindowEventHandlers, ev: MessageEvent<any>) => any)
| null = null;
onmessageerror:
| ((this: WindowEventHandlers, ev: MessageEvent<any>) => any)
| null = null;
onoffline: ((this: WindowEventHandlers, ev: Event) => any) | null = null;
ononline: ((this: WindowEventHandlers, ev: Event) => any) | null = null;
onpagehide:
| ((this: WindowEventHandlers, ev: PageTransitionEvent) => any)
| null = null;
onpageshow:
| ((this: WindowEventHandlers, ev: PageTransitionEvent) => any)
| null = null;
onpopstate: ((this: WindowEventHandlers, ev: PopStateEvent) => any) | null =
null;
onrejectionhandled:
| ((this: WindowEventHandlers, ev: PromiseRejectionEvent) => any)
| null = null;
onstorage: ((this: WindowEventHandlers, ev: StorageEvent) => any) | null =
null;
onunhandledrejection:
| ((this: WindowEventHandlers, ev: PromiseRejectionEvent) => any)
| null = null;
onunload: ((this: WindowEventHandlers, ev: Event) => any) | null = null;
localStorage: Storage = globalThis.window.localStorage;
caches: CacheStorage = globalThis.window.caches;
crossOriginIsolated: boolean = globalThis.window.crossOriginIsolated;
crypto: Crypto = globalThis.window.crypto;
indexedDB: IDBFactory = globalThis.window.indexedDB;
isSecureContext: boolean = globalThis.window.isSecureContext;
origin: string = globalThis.window.origin;
performance: Performance = globalThis.window.performance;
atob(data: string): string {
console.error('called method');
throw new Error('Method not implemented.');
}
btoa(data: string): string {
console.error('called method');
throw new Error('Method not implemented.');
}
clearInterval(id: number | undefined): void {
console.error('called method');
throw new Error('Method not implemented.');
}
clearTimeout(id: number | undefined): void {
console.error('called method');
throw new Error('Method not implemented.');
}
createImageBitmap(
image: ImageBitmapSource,
options?: ImageBitmapOptions | undefined
): Promise<ImageBitmap>;
createImageBitmap(
image: ImageBitmapSource,
sx: number,
sy: number,
sw: number,
sh: number,
options?: ImageBitmapOptions | undefined
): Promise<ImageBitmap>;
createImageBitmap(
image: unknown,
sx?: unknown,
sy?: unknown,
sw?: unknown,
sh?: unknown,
options?: unknown
): Promise<ImageBitmap> {
console.error('called method');
throw new Error('Method not implemented.');
}
fetch(
input: URL | RequestInfo,
init?: RequestInit | undefined
): Promise<Response> {
console.error('called method');
throw new Error('Method not implemented.');
}
queueMicrotask(callback: VoidFunction): void {
console.error('called method');
throw new Error('Method not implemented.');
}
reportError(e: any): void {
console.error('called method');
throw new Error('Method not implemented.');
}
setInterval(
handler: TimerHandler,
timeout?: number | undefined
// ...arguments: any[]
): number {
console.error('called method');
throw new Error('Method not implemented.');
}
setTimeout(
handler: TimerHandler,
timeout?: number | undefined
// ...arguments: any[]
): number {
console.error('called method');
throw new Error('Method not implemented.');
}
structuredClone<T = any>(
value: T,
options?: StructuredSerializeOptions | undefined
): T {
console.error('called method');
throw new Error('Method not implemented.');
}
sessionStorage: Storage = globalThis.window.sessionStorage;
}
interface WindowDef {
label: string;
}
class FakeLocation implements Location {
inner = {} as Location;
get ancestorOrigins(): DOMStringList {
return this.inner.ancestorOrigins;
}
get hash(): string {
return this.inner.hash;
}
get host(): string {
return this.inner.host;
}
get hostname(): string {
return this.inner.hostname;
}
get href(): string {
return '';
}
set href(value: string) {
console.log('called set href to %o', value);
}
toString(): string {
throw new Error('Method not implemented.');
}
get origin(): string {
return this.inner.origin;
}
get pathname(): string {
return this.inner.pathname;
}
get port(): string {
return this.inner.port;
}
get protocol(): string {
return this.inner.protocol;
}
get search(): string {
return this.inner.search;
}
assign(url: string | URL): void {
console.warn('assign called', url);
this.inner.assign(url);
}
reload(): void {
console.warn('reload called');
this.inner.reload();
}
replace(url: string | URL): void {
console.warn('replace called', url);
this.inner.replace(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment