event.go 945 B

1234567891011121314151617181920212223
  1. package lxrod
  2. import (
  3. "github.com/go-rod/rod"
  4. "github.com/go-rod/rod/lib/proto"
  5. )
  6. // vue 页面导航完成结果。如果提前停止等待则返回 false。
  7. type NavigateResultFunc = func() (proto.PageNavigatedWithinDocument, bool)
  8. // 等待 vue hash 路由页面导航完成。
  9. // vue router 有一个 hash 模式。页面的 url 会变成 xx.com/#/yy 的格式。
  10. // 此时页面的“跳转”是用 url 的 fragment 模拟的,并不会触发真正的页面导航。
  11. // 该函数会等待这种页面的跳转事件,并且返回导航的信息。如果监听提前停止则会返回 false。
  12. // 可以给 p 的 ctx 添加 timeout 或者 cancel 来达到提前停止的目的。
  13. func WaitVuePageNavigated(p *rod.Page) NavigateResultFunc {
  14. var e proto.PageNavigatedWithinDocument
  15. wait := p.WaitEvent(&e)
  16. return func() (proto.PageNavigatedWithinDocument, bool) {
  17. wait()
  18. return e, e != proto.PageNavigatedWithinDocument{}
  19. }
  20. }