'JavaScript to download blob:url in WKWebview

I am trying to download a blob video “blob:https://www.xxxxxxx.com/310be2d8-da0a-47b3-b0ea-8e4dac021a5b” using WKWebView. My code is:

            if (err != nil) {
                print(err!.localizedDescription)
                print(err.debugDescription)
                debugPrint("JavaScript Error: \(String(describing: err))")
            }
            if result != nil {
                print(result!)
            }
            print("Complete")
        }

where termWebView is my WKWebview.

The JavaScript is as follows:

        function blobToDataURL(blob, callback) {
            var reader = new FileReader();
            reader.onload = function(e) {callback(e.target.result.split(",")[1])};
            reader.readAsDataURL(blob);
        }

        async function fetchURL() {
            const url = '\(url)';
            try {
                const blob = await fetch(url).then(response => response.blob());
            }
            catch (err) {
                window.webkit.messageHandlers.jsListener.postMessage(`fetch threw, error: ${err}, url: ${url}`);
                return;
            }
            window.webkit.messageHandlers.jsListener.postMessage("Fetched");

            blobToDataURL(blob, datauri => {
                const responseObj = {
                url: url,
                mimeType: blob.type,
                size: blob.size,
                dataString: datauri};
            });
            window.webkit.messageHandlers.jsListener.postMessage("JSON.stringify(responseObj)");
        }
        
        fetchURL();
        """

I am getting the following error message: JavaScript Error: Optional(Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type" UserInfo={NSLocalizedDescription=JavaScript execution returned a result of an unsupported type})

I cannot establish exactly what the type is that is unsupported only that the catch after the await fetch(url) gave me the following message: fetch threw, error: TypeError: Load failed, url: blob:https://www.xxxxxxx.com/310be2d8-da0a-47b3-b0ea-8e4dac021a5b

I am at a bit of a loss. Any help would be much appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source