Skip to content

Instantly share code, notes, and snippets.

@alpgul
Last active April 27, 2025 10:28
Show Gist options
  • Save alpgul/c9f306dc16640f3f79b808c343d5624f to your computer and use it in GitHub Desktop.
Save alpgul/c9f306dc16640f3f79b808c343d5624f to your computer and use it in GitHub Desktop.

Revisions

  1. alpgul revised this gist Apr 27, 2025. 1 changed file with 70 additions and 26 deletions.
    96 changes: 70 additions & 26 deletions window_proxy_override.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,76 @@
    const proxyCache = new WeakMap();

    const windowProxyHandler = {
    get(target, property) {
    console.log(`Getting property: ${String(property)}`);
    const value = Reflect.get(target, property);

    // Eğer değer bir fonksiyon ise, bağlamı koruyarak döndür
    if (typeof value === 'function') {
    return function (...args) {
    return value.apply(target, args);
    };
    (() => {
    const proxyCache = new WeakMap();
    const get = Reflect.get;
    const set = Reflect.get;
    const log = console.log;
    const windowProxyHandler = {
    get(target, property) {
    log(`Getting property: ${String(property)}`);
    const value = get(target, property);

    if (typeof value === 'function') {
    return function (...args) {
    return value.apply(target, args);
    };
    }

    if (value !== null && typeof value === 'object') {
    if (proxyCache.has(value)) return proxyCache.get(value);
    const proxy = new Proxy(value, windowProxyHandler);
    proxyCache.set(value, proxy);
    return proxy;
    }

    return value;
    },

    set(target, property, value) {
    log(`Setting property: ${String(property)} to`, value);
    return set(target, property, value);
    }
    };

    // Eğer değer bir obje ise ve null değilse, Proxy ile sarmala
    if (value !== null && typeof value === 'object') {
    if (proxyCache.has(value)) return proxyCache.get(value);
    const proxy = new Proxy(value, windowProxyHandler);
    proxyCache.set(value, proxy);
    return proxy;
    // Tüm global property'leri al
    const globalProps = [
    ...Object.getOwnPropertyNames(globalThis),
    ...Object.getOwnPropertySymbols(globalThis)
    ];

    for (const property of globalProps) {
    let value;
    try {
    value = globalThis[property];
    } catch (e) {
    console.warn(`Cannot access globalThis.${String(property)}: ${e.message}`);
    continue;
    }

    return value;
    },
    if (value !== null && (typeof value === 'object')) {
    var proxy = proxyCache.has(value) ? proxyCache.get(value) : new Proxy(value, windowProxyHandler);
    if (!proxyCache.has(value)) proxyCache.set(value, proxy);

    set(target, property, value) {
    console.log(`Setting property: ${String(property)} to`, value);
    return Reflect.set(target, property, value);
    const descriptor = Object.getOwnPropertyDescriptor(globalThis, property);
    if (!descriptor || descriptor.configurable) {
    Object.defineProperty(globalThis, property, {
    value: proxy,
    writable: true,
    configurable: true,
    });

    console.log(`${String(property)}:`, globalThis[property]);
    } else {
    console.warn(`Property ${String(property)} is non-configurable, trying eval...`);
    try {
    if (typeof property === 'string' && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(property)) {
    eval(`var ${property} = proxy;`);
    eval(`console.log("${property}:", ${property})`);
    } else {
    console.warn(`Skipping eval for non-string or invalid property name: ${String(property)}`);
    }
    } catch (evalError) {
    console.error(`Eval failed for ${String(property)}: ${evalError.message}`);
    }
    }
    }
    }
    };

    const windowProxy = new Proxy(window, windowProxyHandler);
    })();
  2. alpgul created this gist Apr 27, 2025.
    32 changes: 32 additions & 0 deletions window_proxy_override.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const proxyCache = new WeakMap();

    const windowProxyHandler = {
    get(target, property) {
    console.log(`Getting property: ${String(property)}`);
    const value = Reflect.get(target, property);

    // Eğer değer bir fonksiyon ise, bağlamı koruyarak döndür
    if (typeof value === 'function') {
    return function (...args) {
    return value.apply(target, args);
    };
    }

    // Eğer değer bir obje ise ve null değilse, Proxy ile sarmala
    if (value !== null && typeof value === 'object') {
    if (proxyCache.has(value)) return proxyCache.get(value);
    const proxy = new Proxy(value, windowProxyHandler);
    proxyCache.set(value, proxy);
    return proxy;
    }

    return value;
    },

    set(target, property, value) {
    console.log(`Setting property: ${String(property)} to`, value);
    return Reflect.set(target, property, value);
    }
    };

    const windowProxy = new Proxy(window, windowProxyHandler);