0

I've tried implementing other code on stackoverflow with no luck. I'm evaluating javascript in a WKWeview, and would like to listen for when the content has fully loaded in order to execute statements on the loaded page.

Javascript: (isn't executing func webView)

webView.evaluateJavaScript("_formHist.submit();", completionHandler: nil)

Code I've tried using so far: (only works on some pages)

    func webView(_ webView: WKWebView,
    didFinish navigation: WKNavigation!) {
    //insert code
    }

I'm new to iOS development, so please explain answers in depth if you can!

Kevin Lynch
  • 35
  • 1
  • 4

2 Answers2

0

How is _formHist defined??

If this isn't defined as a javascript object already, it won't work.

Apart from that, the page may not be fully loaded at the time the code is executed so you may need to listen to an event that is triggered when the page is fully loaded

If you refer to this answer it tells you that you can determine when the page has fully loaded using the following method

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});

So for your case, you just need to use this event listener

webView.evaluateJavaScript("document.addEventListener("DOMContentLoaded", function(event) { _formHist.submit(); });", completionHandler: nil)

Give that a try and see if it works

Scriptable
  • 17,875
  • 4
  • 50
  • 65
  • I don't believe it worked. _formHist is an onclick event to an input on a website. The problem is that after submitting _formHist, my above function isn't being triggered. – Kevin Lynch Jan 04 '19 at 10:44
  • This isn't making much sense. for this `_formHist.submit();` to work, `_formHist` would have to be a JS object that references the form DOM element in the HTML, but you haven't shown any code how _formHist is being assigned. first thing I would do if I was you is use a normal, desktop browser and use JS in the debug console to submit the form. then when you have that fully working, then put the *exact* same code into the evaluate javascript method inside the DOMContentLoaded event listener callback – Scriptable Jan 04 '19 at 10:50
  • _formHist.submit(); worked correctly in browser. Just incase my original problem isn't clear, its my following code that is executed to early(providing it here incase it helps): webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML", completionHandler: {(innerHTML, error) in do { let transactionResponse = try TransactionResponse(innerHTML) }catch{}}) if i add a wait, it works – Kevin Lynch Jan 04 '19 at 11:09
  • `var _divHist = $('divHist'); var _mnuPlan = $('mnuPlan'); var _formHist = new AJAXForm( 'svcHistForm', function HistoryDisplay( ajax ) { if ( ajax != null ) { _divHist.innerHTML = ajax.responseText; window.location = '#viewHist'; } else alert( 'Request failed' ); } ); function QuickHistory( planID ) { if ( _mnuPlan.value == planID ) { // plan already selected window.location = '#top'; return; } _mnuPlan.value = planID; if ( _mnuPlan.value == planID ) _formHist.submit(); else window.location = '#top'; }` – Kevin Lynch Jan 04 '19 at 11:17
  • you didn't mention you was using jQuery and didn't show all this code in the question. so is this code what you are passing through evaluate javascript? or is this a script on the webpage? – Scriptable Jan 04 '19 at 11:19
  • this is a script on the website. I didn't know the problem centered around my specific html, my apologies for not including it. – Kevin Lynch Jan 04 '19 at 11:21
  • is this code within a function on the website? you might be able to just call it? – Scriptable Jan 04 '19 at 11:28
  • For me, this approach worked only after adding the event listener to the 'window' object instead of the 'document' object. webView.evaluateJavaScript("window.addEventListener("DOMContentLoaded", function(event) { doSomething(); });", completionHandler: nil) – LaborEtArs Apr 26 '20 at 20:48
0

Maybe you would like to make it much advanced and simple at the same time, so you can show the estimation progress for the user and sure then you can detect that everything was loaded and finished so you can do whatever you want. here's how you can do it:

webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "estimatedProgress" {
            print(Float(webView.estimatedProgress))
            //Do whatever you want when finished
        }
    }
H.Jacob
  • 91
  • 7
  • I tried implementing this, however the function was never called after my above javascript: webView.evaluateJavaScript("_formHist.submit();", completionHandler: nil) – Kevin Lynch Jan 04 '19 at 10:42
  • what is the _formHist?? – H.Jacob Jan 04 '19 at 10:53
  • _formHist brings the browser to the next page. html: – Kevin Lynch Jan 04 '19 at 11:13
  • `var _divHist = $('divHist'); var _mnuPlan = $('mnuPlan'); var _formHist = new AJAXForm( 'svcHistForm', function HistoryDisplay( ajax ) { if ( ajax != null ) { _divHist.innerHTML = ajax.responseText; window.location = '#viewHist'; } else alert( 'Request failed' ); } ); function QuickHistory( planID ) { if ( _mnuPlan.value == planID ) { // plan already selected window.location = '#top'; return; } _mnuPlan.value = planID; if ( _mnuPlan.value == planID ) _formHist.submit(); else window.location = '#top'; }` – Kevin Lynch Jan 04 '19 at 11:14