bank.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // NewChromeBrowser 创建一个由 rod chromium 模拟的 Chrome 浏览器
  23. func NewChromeBrowser(ctx context.Context, u string) *rod.Browser {
  24. return rod.New().Context(ctx).DefaultDevice(ChromeLandscape).ControlURL(u)
  25. }
  26. // EdgeLandscape device.
  27. var EdgeLandscape = devices.Device{
  28. Title: "Edge Laptop with HiDPI screen",
  29. Capabilities: []string{},
  30. 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",
  31. AcceptLanguage: "en",
  32. Screen: devices.Screen{
  33. DevicePixelRatio: 2,
  34. Horizontal: devices.ScreenSize{
  35. Width: 1600,
  36. Height: 900,
  37. },
  38. Vertical: devices.ScreenSize{
  39. Width: 900,
  40. Height: 1600,
  41. },
  42. },
  43. }.Landscape()
  44. // ChromeLandscape device.
  45. var ChromeLandscape = devices.Device{
  46. Title: "Chrome Laptop with HiDPI screen",
  47. Capabilities: []string{},
  48. UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
  49. AcceptLanguage: "en",
  50. Screen: devices.Screen{
  51. DevicePixelRatio: 2,
  52. Horizontal: devices.ScreenSize{
  53. Width: 1600,
  54. Height: 900,
  55. },
  56. Vertical: devices.ScreenSize{
  57. Width: 900,
  58. Height: 1600,
  59. },
  60. },
  61. }.Landscape()
  62. // NewUserMode 使用系统浏览器启动一个新的浏览器。具体使用哪个浏览器由 rod 选择。
  63. // 一般是 Chrome 浏览器。注意:本地的浏览器必须完全关闭,否则会启动失败。
  64. func (lxrod *Lxrod) NewUserMode(ctx context.Context) (b *rod.Browser, p *rod.Page, err error) {
  65. l := launcher.NewUserMode().Headless(false)
  66. l.Flags["disable-blink-features"] = []string{"AutomationControlled"}
  67. lxrod.Launcher = l
  68. wsURL, err := l.Launch()
  69. lxrod.WsURL = wsURL
  70. if err != nil {
  71. return nil, nil, taxerr.NewWebStuckTitle(true)
  72. }
  73. b = rod.New().NoDefaultDevice().ControlURL(wsURL).Context(ctx).MustConnect()
  74. p = b.MustPage().MustSetWindow(0, 0, 1600, 900).MustSetViewport(1600, 900, 1, false)
  75. lxrod.Browser = b
  76. return b, p, nil
  77. }
  78. // NewSystemEdge 使用用户的 Edge 启动浏览器。注意:本地的 Edge
  79. // 浏览器必须完全关闭,否则会启动失败。
  80. func (lxrod *Lxrod) NewSystemEdge(ctx context.Context) (b *rod.Browser, p *rod.Page, err error) {
  81. bin, ok := findEdgePath()
  82. if !ok {
  83. return nil, nil, errors.New("未找到 Edge 浏览器")
  84. }
  85. l := launcher.NewUserMode().Headless(false)
  86. l.Flags["disable-blink-features"] = []string{"AutomationControlled"}
  87. l.Set(flags.Bin, bin)
  88. lxrod.Launcher = l
  89. wsURL, err := l.Launch()
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. lxrod.WsURL = wsURL
  94. b = rod.New().NoDefaultDevice().ControlURL(wsURL).Context(ctx).MustConnect()
  95. p = b.MustPage().MustSetWindow(0, 0, 1600, 900).MustSetViewport(1600, 900, 1, false)
  96. lxrod.Browser = b
  97. return b, p, nil
  98. }
  99. // 查找系统 Edge 的路径。从 [launcher.LookPath] 处拷贝的。
  100. func findEdgePath() (found string, has bool) {
  101. list := append([]string{"edge"},
  102. expandWindowsExePaths(
  103. `Microsoft\Edge\Application\msedge.exe`,
  104. )...,
  105. )
  106. for _, path := range list {
  107. var err error
  108. found, err = exec.LookPath(path)
  109. has = err == nil
  110. if has {
  111. break
  112. }
  113. }
  114. return
  115. }
  116. func expandWindowsExePaths(list ...string) []string {
  117. newList := []string{}
  118. for _, p := range list {
  119. newList = append(
  120. newList,
  121. filepath.Join(os.Getenv("ProgramFiles"), p),
  122. filepath.Join(os.Getenv("ProgramFiles(x86)"), p),
  123. filepath.Join(os.Getenv("LocalAppData"), p),
  124. )
  125. }
  126. return newList
  127. }