bank.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package lxrod
  2. import (
  3. "context"
  4. "errors"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "git.listensoft.net/tool/jspkit/taxerr"
  9. "github.com/go-rod/rod"
  10. "github.com/go-rod/rod/lib/devices"
  11. "github.com/go-rod/rod/lib/launcher"
  12. "github.com/go-rod/rod/lib/launcher/flags"
  13. )
  14. // NewLauncher 创建一个新的 rod 浏览器启动器
  15. func NewLauncher(ctx context.Context) *launcher.Launcher {
  16. return launcher.New().Context(ctx).Set("window-size", "1600,900").Headless(false)
  17. }
  18. // NewEdgeBrowser 创建一个由 rod chromium 模拟的 Edge 浏览器
  19. func NewEdgeBrowser(ctx context.Context, u string) *rod.Browser {
  20. return rod.New().Context(ctx).DefaultDevice(EdgeLandscape).ControlURL(u)
  21. }
  22. // EdgeLandscape device.
  23. var EdgeLandscape = devices.Device{
  24. Title: "Edge Laptop with HiDPI screen",
  25. Capabilities: []string{},
  26. UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
  27. AcceptLanguage: "en",
  28. Screen: devices.Screen{
  29. DevicePixelRatio: 2,
  30. Horizontal: devices.ScreenSize{
  31. Width: 1600,
  32. Height: 900,
  33. },
  34. Vertical: devices.ScreenSize{
  35. Width: 900,
  36. Height: 1600,
  37. },
  38. },
  39. }.Landscape()
  40. // NewUserMode 使用系统浏览器启动一个新的浏览器。具体使用哪个浏览器由 rod 选择。
  41. // 一般是 Chrome 浏览器。注意:本地的浏览器必须完全关闭,否则会启动失败。
  42. func (lxrod *Lxrod) NewUserMode(ctx context.Context) (b *rod.Browser, p *rod.Page, err error) {
  43. l := launcher.NewUserMode().Headless(false)
  44. l.Flags["disable-blink-features"] = []string{"AutomationControlled"}
  45. lxrod.Launcher = l
  46. wsURL, err := l.Launch()
  47. lxrod.WsURL = wsURL
  48. if err != nil {
  49. return nil, nil, taxerr.NewWebStuckTitle(true)
  50. }
  51. b = rod.New().NoDefaultDevice().ControlURL(wsURL).Context(ctx).MustConnect()
  52. p = b.MustPage().MustSetWindow(0, 0, 1600, 900).MustSetViewport(1600, 900, 1, false)
  53. lxrod.Browser = b
  54. return b, p, nil
  55. }
  56. // NewSystemEdge 使用用户的 Edge 启动浏览器。注意:本地的 Edge
  57. // 浏览器必须完全关闭,否则会启动失败。
  58. func (lxrod *Lxrod) NewSystemEdge(ctx context.Context) (b *rod.Browser, p *rod.Page, err error) {
  59. bin, ok := findEdgePath()
  60. if !ok {
  61. return nil, nil, errors.New("未找到 Edge 浏览器")
  62. }
  63. l := launcher.NewUserMode().Headless(false)
  64. l.Flags["disable-blink-features"] = []string{"AutomationControlled"}
  65. l.Set(flags.Bin, bin)
  66. lxrod.Launcher = l
  67. wsURL, err := l.Launch()
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. lxrod.WsURL = wsURL
  72. b = rod.New().NoDefaultDevice().ControlURL(wsURL).Context(ctx).MustConnect()
  73. p = b.MustPage().MustSetWindow(0, 0, 1600, 900).MustSetViewport(1600, 900, 1, false)
  74. lxrod.Browser = b
  75. return b, p, nil
  76. }
  77. // 查找系统 Edge 的路径。从 [launcher.LookPath] 处拷贝的。
  78. func findEdgePath() (found string, has bool) {
  79. list := append([]string{"edge"},
  80. expandWindowsExePaths(
  81. `Microsoft\Edge\Application\msedge.exe`,
  82. )...,
  83. )
  84. for _, path := range list {
  85. var err error
  86. found, err = exec.LookPath(path)
  87. has = err == nil
  88. if has {
  89. break
  90. }
  91. }
  92. return
  93. }
  94. func expandWindowsExePaths(list ...string) []string {
  95. newList := []string{}
  96. for _, p := range list {
  97. newList = append(
  98. newList,
  99. filepath.Join(os.Getenv("ProgramFiles"), p),
  100. filepath.Join(os.Getenv("ProgramFiles(x86)"), p),
  101. filepath.Join(os.Getenv("LocalAppData"), p),
  102. )
  103. }
  104. return newList
  105. }