Last active
May 21, 2024 19:07
-
-
Save felipecarvalho/5f767346b19c8aa10bfad6f820856891 to your computer and use it in GitHub Desktop.
Inject JS on webview_flutter
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
| void _injectJS(controller) { | |
| controller.runJavaScript(''' | |
| function dispatchButtonEvent(element, func) { | |
| const button = document.querySelector(element); | |
| button.addEventListener("click", function() { | |
| setTimeout(func, "$_delayTime"); | |
| }); | |
| } | |
| function changeValue(itemId, itemValue) { | |
| const item = document.getElementById(itemId); | |
| item.scrollIntoView(); | |
| item.value = itemValue; | |
| Print.postMessage(item.value + " added to input " + itemId); | |
| } | |
| dispatchButtonEvent('button[name="tipo_graduacao"]', () => { | |
| // Add name to input | |
| changeValue("name", "$_name"); | |
| dispatchButtonEvent('button[name="name"]', () => { | |
| // Add email to input | |
| changeValue("email", "$_email"); | |
| }); | |
| }); | |
| '''); | |
| } | |
| //* WebView snippet settings *// | |
| // ..addJavaScriptChannel( | |
| // 'Print', | |
| // onMessageReceived: (JavaScriptMessage message) { | |
| // debugPrint(message.message); | |
| // }, | |
| // ) | |
| // ..setNavigationDelegate( | |
| // NavigationDelegate( | |
| // onPageFinished: (String url) { | |
| // debugPrint('Page finished loading: $url'); | |
| // _injectJS(_controller); | |
| // }, | |
| // )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment