简体   繁体   English

如何在WKWebView中检测history.pushstate

[英]How to detect history.pushstate in WKWebView

I developed hybrid iOS application with Swift and want to detect history.pushstate() in WKWebView. 我使用Swift开发了混合iOS应用程序,并希望在WKWebView中检测history.pushstate()。 I did override WKWebView's methods, but I couldn't detect anything. 我确实覆盖了WKWebView的方法,但我无法检测到任何东西。

Is there a way or trick to detect history.pushstate() 有没有办法或技巧来检测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') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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