-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Add unimplemented features to the web version. #5808
Conversation
Webview flutter web encoding
Why do the Github tests fail? I don't understand the output. |
These tests are failing:
|
Where are these tests located at? I don't see them in |
Thanks for the submission! Please split this into two separate PR; this is doing two very different things:
|
I agree, this should be 2 different PRs. I'll revert the bug fix and make a new PR for it. |
This reverts commit 6af1972. The revert was bad.
I'm not sure that this can be landed as is, too spicy! I'm surprised that the analyzer didn't want more I think the most appropriate way of doing this (but I don't have a proper design doc yet) would be to create some JS code that gets distributed alongside the webview_flutter_web plugin, as an asset, that can be loaded by the iframes that users want to embed. That JS would configure the iframe in a way that "collaborates" with the embedding flutter app, by establishing a proper communication channel through the Read more about postMessage, here: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage |
Based on the comments above, I'm going to close this, as it's not an implementation we would move forward with. If you are interesting in supporting message posting in general, using an approach like the one described in @ditman's comment above, the next step would be a design document, which could be posted for discussion in flutter/flutter#101758 once drafted. Thanks! |
The methods that were implemented in this PR are the following:
evaluateJavascript
,runJavascript
, andrunJavascriptReturningResult
These only work when setting the content with
loadHtmlString
because of security reasons browsers have with iframes.The methods will also not work with
loadRequest
, because it can load things other than HTML and I don't know how to detect whether it is HTML or not (The HTML needs to be preprocessed.)loadRequest
.loadHtmlString
also had the unreliable encoding, but it's been changed and now doesn't have any encoding.The new encoding is taken from WebViewX.
Tests added
testWidgets('loadHtml', ...)
- Tests the functionality ofloadHtmlString
. This test doesn't pass without solving the issue linked above.testWidgets('JavascriptChannel', ...)
- Tests the communication between Flutter and the Webview with Javascript channels and raw Javascript evaluation.Technical Details
Technical Details
The communication between the IFrame's HTML and the global scope is done by preprocessing the HTML to add a script element to expose the IFrame's Window object to the global scope. With the Window object of the IFrame's HTML you can evaluate raw Javascript.
Preprocessing the HTML also handles the Javascript channels, which is why you can't add/remove them after loading the HTML.
The preprocessing of the HTML creates the necessary elements ("", "html", "head", "body") in case they aren't present. It also creates a script element to expose the Window object and handle Javascript channels.
So for example,
<div>Hello, world</div>
will turn into this after the preprocessing:Pre-launch Checklist
dart format
.)[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style.///
).