Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Fetch` API Overridden. How do I access the original function?

the Fetch API is totally mutable and can be replaced or removed by doing

window.fetch = null;

or,

var fetch = null;

or, the fetch property can be removed as well.

delete window.fetch;

This means, if a legacy code defines a global variable named fetch, the fetch API cannot be used.

Is there any way to access the original fetch function in JavaScript?

like image 292
Microtribute Avatar asked May 24 '26 16:05

Microtribute


1 Answers

I appreciate your answers but none of you answered the question properly. The fetch function is already erased and we have no way to access it. In this case we can use the following simple hack.

This will hand us the original fetch function.

function restoreFetch() {
    if (!window._restoredFetch) {
        const iframe = document.createElement('iframe');

        iframe.style.display = 'none';
        document.body.appendChild(iframe); // add element

        window._restoredFetch = iframe.contentWindow.fetch;
    }

    return window._restoredFetch;
}

Then, we can use the fetch API:

const f = restoreFetch();

const result = await f('https://stackoverflow.com');
like image 109
Microtribute Avatar answered May 26 '26 07:05

Microtribute



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!