-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[webview_flutter_wkwebview] Fixes JSON.stringify() cannot serialize cyclic structures #6274
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
Merged
auto-submit
merged 12 commits into
flutter:main
from
LinXunFeng:fix_json_stringify_cyclic_structures_error_wkwebview
May 15, 2024
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6057b1d
Fixs JSON.stringify() cannot serialize cyclic structures
LinXunFeng a37f7bf
Update version and CHANGELOG
LinXunFeng 182afac
Remove cyclic object
LinXunFeng ee66a4a
Remove whitespace
LinXunFeng a709175
Remove whitespace
LinXunFeng b7a3661
Improve logic
LinXunFeng f7ac9bd
Merge branch 'main' into fix_json_stringify_cyclic_structures_error_w…
LinXunFeng ae1a4a2
Adjust to use _flutter_webview_plugin_overrides namespace
LinXunFeng 979d8ee
Update comment
LinXunFeng fd9bbcf
Update comment
LinXunFeng c32dedd
Update AUTHORS
LinXunFeng 3700600
Rename
LinXunFeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -630,33 +630,65 @@ class WebKitWebViewController extends PlatformWebViewController { | |
| } | ||
|
|
||
| Future<void> _injectConsoleOverride() { | ||
| // Within overrideScript, a series of console output methods such as | ||
| // console.log will be rewritten to pass the output content to the Flutter | ||
| // end. | ||
| // | ||
| // These output contents will first be serialized through JSON.stringify(), | ||
| // but if the output content contains cyclic objects, it will encounter the | ||
| // following error. | ||
| // TypeError: JSON.stringify cannot serialize cyclic structures. | ||
| // See https://github.com/flutter/flutter/issues/144535. | ||
| // | ||
| // Considering this is just looking at the logs printed via console.log, | ||
| // the cyclic object is not important, so remove it. | ||
| // Therefore, the replacer parameter of JSON.stringify() is used and the | ||
| // removeCyclicObject method is passed in to solve the error. | ||
| const WKUserScript overrideScript = WKUserScript( | ||
| ''' | ||
| function log(type, args) { | ||
| var message = Object.values(args) | ||
| .map(v => typeof(v) === "undefined" ? "undefined" : typeof(v) === "object" ? JSON.stringify(v) : v.toString()) | ||
| .map(v => v.substring(0, 3000)) // Limit msg to 3000 chars | ||
| .join(", "); | ||
|
|
||
| var log = { | ||
| level: type, | ||
| message: message | ||
| }; | ||
|
|
||
| window.webkit.messageHandlers.fltConsoleMessage.postMessage(JSON.stringify(log)); | ||
| } | ||
| var _flutter_webview_plugin_overrides = _flutter_webview_plugin_overrides || { | ||
| removeCyclicObject: function() { | ||
| const levelObjects = []; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the description in the PR comment,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx. |
||
| return function (k, v) { | ||
| if (typeof v !== "object" || v === null) { return v; } | ||
| const currentParentObj = this; | ||
| while ( | ||
| levelObjects.length > 0 && | ||
| levelObjects[levelObjects.length - 1] !== currentParentObj | ||
| ) { | ||
| levelObjects.pop(); | ||
| } | ||
| if (levelObjects.includes(v)) { return; } | ||
| levelObjects.push(v); | ||
| return v; | ||
| }; | ||
| }, | ||
| log: function (type, args) { | ||
| var message = Object.values(args) | ||
| .map(v => typeof(v) === "undefined" ? "undefined" : typeof(v) === "object" ? JSON.stringify(v, _flutter_webview_plugin_overrides.removeCyclicObject()) : v.toString()) | ||
| .map(v => v.substring(0, 3000)) // Limit msg to 3000 chars | ||
| .join(", "); | ||
|
|
||
| var log = { | ||
| level: type, | ||
| message: message | ||
| }; | ||
|
|
||
| window.webkit.messageHandlers.fltConsoleMessage.postMessage(JSON.stringify(log)); | ||
| } | ||
| }; | ||
|
|
||
| let originalLog = console.log; | ||
| let originalInfo = console.info; | ||
| let originalWarn = console.warn; | ||
| let originalError = console.error; | ||
| let originalDebug = console.debug; | ||
|
|
||
| console.log = function() { log("log", arguments); originalLog.apply(null, arguments) }; | ||
| console.info = function() { log("info", arguments); originalInfo.apply(null, arguments) }; | ||
| console.warn = function() { log("warning", arguments); originalWarn.apply(null, arguments) }; | ||
| console.error = function() { log("error", arguments); originalError.apply(null, arguments) }; | ||
| console.debug = function() { log("debug", arguments); originalDebug.apply(null, arguments) }; | ||
| console.log = function() { _flutter_webview_plugin_overrides.log("log", arguments); originalLog.apply(null, arguments) }; | ||
| console.info = function() { _flutter_webview_plugin_overrides.log("info", arguments); originalInfo.apply(null, arguments) }; | ||
| console.warn = function() { _flutter_webview_plugin_overrides.log("warning", arguments); originalWarn.apply(null, arguments) }; | ||
| console.error = function() { _flutter_webview_plugin_overrides.log("error", arguments); originalError.apply(null, arguments) }; | ||
| console.debug = function() { _flutter_webview_plugin_overrides.log("debug", arguments); originalDebug.apply(null, arguments) }; | ||
|
|
||
| window.addEventListener("error", function(e) { | ||
| log("error", e.message + " at " + e.filename + ":" + e.lineno + ":" + e.colno); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment briefly explaining the approach at a high level so readers don't need to reverse-engineer what is being done here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.