Skip to content

evalJavascript(document.documentElement.innerHTML) escaped text? #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AnsonLai opened this issue May 14, 2019 · 1 comment
Closed

evalJavascript(document.documentElement.innerHTML) escaped text? #417

AnsonLai opened this issue May 14, 2019 · 1 comment

Comments

@AnsonLai
Copy link

I'm trying to get the source from a webview, and I'm using evalJavascript to try and get the document's innerHTML:

@override void initState() { flutterWebviewPlugin.onStateChanged.listen((state) async { if (state.type == WebViewState.finishLoad) { String response = await flutterWebviewPlugin.evalJavascript('document.documentElement.innerHTML;'); print(response); } }); }

But the output is always some form of escaped text:
"\u003Chead>\u003Cmeta charset=\"UTF-8\">\u003Cmeta content=\"width=device-width,minimum-scale=1.0\" name=\"viewport\">\u003Cmeta content=\"telephone=no\" name=\"format-detection\">\u003Cmeta content=\"address=no\" name=\"format-detection\">\u003Cmeta content=\"origin\" name=\"referrer\">\u003Cmeta content=\"notranslate\" name=\"google\">\u003Clink href=\"/images/branding/product/1x/gsa_android_144dp.png\" rel=\"icon\">\u003Cmeta content=\"/images/branding/googleg/1x/googleg_standard_color_128dp.png\" itemprop=\"image\">

Is this the proper behavior? I can't seem to figure out what type of escape this is. One of the things I was thinking is if there was a way to catch it earlier? I noticed that if I use console.log, it seems to output in the flutter console in proper formatting:

String response = await flutterWebviewPlugin.evalJavascript('console.log(document.documentElement.innerHTML);');

Which yields something like this:

[INFO:CONSOLE(1)] "<head><meta charset="UTF-8"><meta content="width=device-width,minimum-scale=1.0" name="viewport"><meta content="telephone=no" name="format-detection"><meta content="address=no" name="format-detection"><meta content="origin" name="referrer"><meta content="notranslate" name="google"><link href="/images/branding/product/1x/gsa_android_144dp.png" rel="icon"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="vnS8R6Yzt54tLKduMnYLPg==">

@AnsonLai
Copy link
Author

AnsonLai commented May 14, 2019

The response is JSON escaped, so if anyone else runs into this problem, just use the built in decode function in the dart-convert package to unescape it.

var output = jsonDecode(response);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant