gpt4 book ai didi

javascript - 如何在 WKWebView 中检测 history.pushstate

转载 作者:搜寻专家 更新时间:2023-10-31 19:28:51 24 4
gpt4 key购买 nike

我使用 Swift 开发了混合 iOS 应用程序并希望在 WKWebView 中检测 history.pushstate()。我确实覆盖了 WKWebView 的方法,但我无法检测到任何东西。

有没有办法或技巧来检测 history.pushstate()

最佳答案

这当然是一种解决方法,但如果允许您编辑内置对象的属性(就像在大多数浏览器中一样),您可以包装这两个函数并使用拦截处理程序来跟踪它们何时被调用:

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

关于javascript - 如何在 WKWebView 中检测 history.pushstate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com