简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM