【问题标题】:How to detect history.pushstate in WKWebView如何在 WKWebView 中检测 history.pushstate
【发布时间】:2017-02-27 01:34:04
【问题描述】:

我用 Swift 开发了混合 iOS 应用程序 并想在 WKWebView 中检测 history.pushstate()。 我确实重写了 WKWebView 的方法,但我什么也没检测到。

有没有办法检测history.pushstate()

【问题讨论】:

  • 覆盖是什么意思?
  • @gyre 表示我使用了“func webView(blah blah blah)”

标签: javascript ios swift


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    相关资源
    最近更新 更多