0

me again ... Is it possible to run javascript automatically in flutter_webview_plugin?.

I Try this by tapping an IconButton

flutterWebviewPlugin.evalJavascript('document.addEventListener("DOMContentLoaded", function(event){console.log("DOM fully loaded and parsed");})');

it doesn't works.

Actually my goal is to fill two textfields (name and password) and to submit it when the document is loaded.

If I try

flutterWebviewPlugin.evalJavascript('document.getElementById("user-id").value = "Phil Osoph"; document.getElementById("pw-id").value = "Baum123"; document._CustomLoginForm.submit(); ');

it works but if I try

flutterWebviewPlugin.evalJavascript('document.addEventListener("DOMContentLoaded", function(event){document.getElementById("user-id").value = "Phil Osoph"; document.getElementById("pw-id").value = "Baum123"; document._CustomLoginForm.submit();})');

nothing happens.

5
  • Sounds like DOMContentLoaded already happened when the JavaScript is evaluated. Commented May 18, 2018 at 11:26
  • hmmm ok, but how I can execute javascript without tapping an button? I thought DOMContentLoaded do it automatically. Commented May 18, 2018 at 11:34
  • DOMContentLoaded is great, but it happens only once and if it already happended when you call evalJavascript() the listener will wait forever. Commented May 18, 2018 at 12:00
  • You write "If I try ... it works". So why not stick with that? There is some risk that the page is not yet loaded though. github.com/dart-flitter/flutter_webview_plugin/blob/… might help with that. I don't have much experience with WebView plugin though. Commented May 18, 2018 at 12:02
  • "So why not stick with that?" My goal is to do it automaticly, if I open the Webview it should logged in automaticly and redirect to a sub site. www.example.com/login after login redirect to www.example.com/application. For the automaticly logged in I write value in textfields name and password and Submit it. But yet i musst push a button to write and submit. Commented May 18, 2018 at 14:58

1 Answer 1

2

This should run the script when the page is loaded

flutterWebviewPlugin.onStateChanged.listen((state) async {
  if(state == WebViewState.finishLoad) {
    flutterWebviewPlugin.evalJavascript('document.getElementById("user-id").value = "Phil Osoph"; document.getElementById("pw-id").value = "Baum123"; document._CustomLoginForm.submit(); ');
  }
});
Sign up to request clarification or add additional context in comments.

10 Comments

You have forgot closing bracket and semicolon at the end. And I to stupid for it or it doesn't works.
It works with state.type == WebViewState.finishLoad, but just only for the first time, if i close it and start it again this doesn't works.
You might miss the call to dispose after closing
Where do I have to execute the dispose function? I have tried it in Button onPressed: () { flutterWebviewPlugin.close(); dispose(); //flutterWebviewPlugin.dispose(); },
Could it be possible that the oppening with widget webview is buggy? Because if i oppend it in a (rect) and as hidden webview it works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.