I've been attempting to add a listener for when the user click this button element in a WKWebView:
<div class="web-navigation__auth" data-test-web-chrome-auth="">
<button class="web-navigation__auth-button web-navigation__auth-button--sign-in button button-reset button--auth">
<svg ... ></path></g></svg>
Sign In
</button>
<div class="web-chrome-auth-container__spinner web-chrome-auth-container__spinner--hidden"></div>
</div>
I've attempted to observe using numerous different JavaScript evaluations, and have tried the code in different places (ie. viewDidLoad, webView(didFinish), webView(didStartProvisionalNavigation), etc):
let js1 = "document.addEventListener('click', function(e) { e = e || window.event; if(e.target.getAttribute('class') == 'web-navigation__auth-button') { console.log(e.target.value); } });"
let js2 = "var elements = document.getElementsByClassName('web-navigation__auth'); var myFunction = function() { var attribute = this.getAttribute('web-navigation__auth-button--sign-in'); alert(attribute); }; for (var i = 0; i < elements.length; i++) { elements[i].addEventListener('click', myFunction, false); }"
let js3 = "document.addEventListener('click', getElementsByClassName('web-navigation__auth-button').onclick();"
webView.evaluateJavaScript(js) { (key, err) in
if let err = err {
print(err.localizedDescription)
} else {
print("JS: You tapped the Sign In button!")
}
}
The output to console only seems to appear whenever the webView.evaluateJavaScript is called, not when button is pressed. I have not gotten any code to trigger when the button is pressed, aside from the note below regarding js3. This the the output to console for each JS call in webView(didFinish):
js1: A JavaScript exception occurred // on webView.didFinish
JS: You tapped the Sign In button! // on webView.didFinish
js2: A JavaScript exception occurred // on webView.didFinish
JS: You tapped the Sign In button! // on webView.didFinish
js3: A JavaScript exception occurred // on webView.didFinish
A JavaScript exception occurred // on Sign In button click (fired again)
One thing to note about js3 is that, it will fire the error message on webView.didFinish, but it will also fire the error if the button is pressed.
So theoretically, I could create a flag to check for the clicking of that button; however, I would prefer something more concrete than this hacky workaround:
// Error output to console on every new page load AND "Sign In" button click
var pageLoad = false // Page has not yet loaded, false by default
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
pageLoad = true // Page has loaded, unless new URL detected, next error = button click!
if webView.urlHasChanged() { pageLoad = false }
let js3 = "document.getElementsByClassName('web-navigation__auth-button').onclick();"
webView?.evaluateJavaScript(js3) { (key, err) in
if let err = err {
if pageLoad {
// Error message && pageLoad = "Sign in" button pressed: CODE HERE
}
}
if pageLoad && url.hasNotChanged() { pageLoad = false }
}