How to modify the global window object in Safari Extensions?

I want use the Safari Extension to decorate the window.fetch function, But No matter how I try, I can't get the fetch function to execute correctly. I was going through the documentation: https://developer.apple.com/documentation/safariservices/using-injected-style-sheets-and-scripts

and found this sentence:

"Injected scripts have an implied namespace — you don’t have to worry about your variable or function names conflicting with those of the website author, nor can a website author call functions in your extension. In other words, injected scripts and scripts that you include in the webpage run in isolated worlds, with no access to each other’s functions or data."

Does this mean I can't modify the window object in the content script just like a Chrome extension does with the webpage?

BTW, In chrome I use chrome.scripting.executeScript API, and in plasmo I just use world: "MAIN" content script's config to achieved this feature.

I'll set aside if it will be approved for the public but you could use script injection.

function injectScript() {
    const script = document.createElement('script');
    script.textContent = `
        // Store the original fetch
        const originalFetch = window.fetch;
        
        // Decorate fetch with your custom logic
        window.fetch = function(...args) {
            // Your custom logic here
            console.log('Fetch intercepted:', args[0]);
            
            // Example: Add custom headers
            if (args[1] && args[1].headers) {
                args[1].headers = {
                    ...args[1].headers,
                    'X-Custom-Header': 'MyValue'
                };
            }
            
            // Call original fetch with modified arguments
            return originalFetch.apply(this, args);
        };
    `;
    
    // Inject script into webpage
    (document.head || document.documentElement).appendChild(script);
    
    // Clean up - remove the script tag after injection
    script.remove();
}

// Execute the injection when content script loads
injectScript();
How to modify the global window object in Safari Extensions?
 
 
Q