`Fetch` API 被覆盖。如何访问原始功能?

aut*_*ode 9 javascript overriding replace fetch

Fetch API 是完全可变的,可以通过执行以下操作来替换或删除

window.fetch = null;
Run Code Online (Sandbox Code Playgroud)

或者,

var fetch = null;
Run Code Online (Sandbox Code Playgroud)

或者,也可以删除 fetch 属性。

delete window.fetch;
Run Code Online (Sandbox Code Playgroud)

这意味着,如果遗留代码定义了一个名为的全局变量fetch,则无法使用 fetch API。

有没有办法访问fetchJavaScript中的原始函数?

aut*_*ode 4

我很感谢您的回答,但你们都没有正确回答问题。该fetch函数已被删除,我们无法访问它。在这种情况下,我们可以使用以下简单的技巧。

这将为我们提供原始的获取函数。

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;
}
Run Code Online (Sandbox Code Playgroud)

然后,我们可以使用 fetch API:

const f = restoreFetch();

const result = await f('https://stackoverflow.com');
Run Code Online (Sandbox Code Playgroud)