Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java webview: "Failed to read the 'cookie' property from 'Document': Access is denied for this document"

I'm trying to use a cookie in my webview without success.

In my code:

CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptFileSchemeCookies(true);
    cookieManager.setAcceptCookie(true);
    cookieManager.acceptCookie();

    myWebView = (WebView) getView(champHTML);// description du champ HTML WM16
    myWebView.setBackgroundColor(Color.parseColor("#42A334"));
    myWebView.getSettings().setJavaScriptEnabled(true); //autorisation javascript

But when I want to debug my webview with Chrome Dev Tools, I get:

Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Access is denied for this document.

What am I doing wrong?

Edit: I forgot to specify that i display in my webview an offline page HTML.

And i don't understand, when i'm in console tab in Chrome Developper Tools, I can read the error message : SecurityError: Failed to read the 'cookie' property from 'Document': Access is denied for this document.

If I change the url width "window.location = 'http://www.google.fr', the page is displayed on my webview and if i type in the tab console 'window.cookie'. I haven't any error, i can see the cookie value.

But if I use 'history.back()', the webview display my offlinepage, and if type : 'window.cookie', I get always the same error.

The error message is displayed when i use an offline page.

May be sombody have an idea ?

like image 649
Slayes Avatar asked Feb 06 '15 17:02

Slayes


2 Answers

Three years later...

But I have the same problem. And I found only one solution.

Just disable cookies before running problem script (Kotlin code):

    companion object {
        const val SOME_HTML = """
    <!DOCTYPE html>
        <html>
            <head>
                <script>
                    if(!document.__defineGetter__) {
                        Object.defineProperty(document, 'cookie', {
                            get: function(){return ''},
                            set: function(){return true},
                        });
                    } else {
                        document.__defineGetter__("cookie", function() { return '';} );
                        document.__defineSetter__("cookie", function() {} );
                    }

                   // Your problem script here
                </script>
        </head>
    </html>"""
    }

//And 
webView.settings.javaScriptEnabled = true
webView.loadData(CHARTA_HTML, "text/html; charset=utf-8", "UTF-8")

Edit

One more workaround:

If you use local HTML or hardcoded HTML like in the example above, you need to load HTML using this method:

webview.loadDataWithBaseURL("https://yoursite.com", YOUR_HTML, "text/html; charset=utf-8", "UTF-8", null)

BaseURL is using to prevent "Uncaught SecurityError" when you use local HTML with javascript which is trying to load data from BaseURL links.

like image 196
bitvale Avatar answered Oct 18 '22 11:10

bitvale


Got this exact error when loading a webview using loadDataWithBaseURL.

I was passing null for the baseUrl parameter. Passing any valid URL here causes the error to go away.

This is called out at the end of one of the other answers .. posting it separately since it was what fixed it for me.

like image 22
stevex Avatar answered Oct 18 '22 10:10

stevex