I'm trying to catch all errors with the window.onerror function on a page encoded in base64 and loaded with a data: URI, but I only get Script error. as error message instead of the full error. I get the full error message, when I load the webpage directly from the file, so it looks like it has to do something with the page being in a data: URI.

I've already tried with the crossorigin attribute on the <script> tag and searched on MDN for data: URIs and Content Security Policy, but to no avail.

My question is, how to get the full error, when the page is loaded as data: URI? It doesn't need to be with the window.onerror handler, as long as I can get the full error message, but I'm limited to HTML and javascript.

My test script:

<!DOCTYPE html>
<html>
<head>
    <!-- <meta http-equiv="Content-Security-Policy" content="script-src *;"> -->
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, width=device-width">
    <style>
        body {
            white-space: pre; 
            font-family: monospace;
        }
    </style>
    <script crossorigin="crossorigin">
    var oldHandler = window.onerror;
    window.onerror = function (message, source, line, col, error) {
        oldHandler && oldHandler(message, source, line, col, error);
    error = error || {};
        error.messageOnError = message;
        error.source = source;
        error.lineno = line;
        error.colno = col;
        dw(error);
    };

    // window.addEventListener("error", dw);

    function dw(o) {
        if (typeof o === "object") {
            o = JSON.stringify(o, function(k, v) {
                if (v instanceof Error) {
                    var n = {
                        error: "error"
                    };
                    var props = Object.getOwnPropertyNames(v);
                    for (var i = 0, p; i < props.length; i++) {
                        p = props[i];
                        n[p] = v[p];
                        if (p === "stack") {
                            n.stack = n.stack.replace(/data:text\/html;base64,(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?(:\d+)/g, "source$1");
                        }
                    }
                    v = n;
                }
                return v;
            }, 4);
        }

    document.write(o);
    //alert(o);
    console.log(o);
    return;

        var div = document.createElement("div");
        div.innerText = o;
        document.body.appendChild(div);
    }
    </script>
</head>
<body>
    <script crossorigin="crossorigin">
    throw new Error("test");
    // / (/
    </script>
</body>
</html>

and the data: URI:

data:text/html;base64,PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4NCgk8IS0tIDxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtU2V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