I developed hybrid iOS application with Swift and want to detect history.pushstate() in WKWebView. I did override WKWebView's methods, but I couldn't detect anything.
Is there a way or trick to detect history.pushstate()
This is certainly a workaround, but if you are allowed to edit properties of built-in objects (like in most browsers) you can wrap both functions and use an intercepting handler to track when they are called:
function track (fn, handler) {
return function interceptor () {
handler.apply(this, arguments)
return fn.apply(this, arguments)
}
}
history.pushState = track(history.pushState, function (state, title, url) {
// do something with `state, `title`, and `url`
console.log('pushState called with:', { state: state, title: title, url: url })
})
history.replaceState = track(history.replaceState, function (state, title, url) {
// do something with `state, `title`, and `url`
console.log('replaceState called with:', { state: state, title: title, url: url })
})
history.pushState(null, 'Title 1', '/example-page')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With