Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect history.pushstate in WKWebView

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()

like image 358
hoi Avatar asked Oct 30 '22 11:10

hoi


1 Answers

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')
like image 82
gyre Avatar answered Nov 14 '22 07:11

gyre