Skip to content

Instantly share code, notes, and snippets.

@felipecarvalho
Last active May 21, 2024 19:07
Show Gist options
  • Select an option

  • Save felipecarvalho/5f767346b19c8aa10bfad6f820856891 to your computer and use it in GitHub Desktop.

Select an option

Save felipecarvalho/5f767346b19c8aa10bfad6f820856891 to your computer and use it in GitHub Desktop.
Inject JS on webview_flutter
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