| 27 | ) |
| 28 | |
| 29 | func init() { |
| 30 | engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
| 31 | DisableOnDefault: false, |
| 32 | Brief: "网页截图", |
| 33 | Help: "- /网页截图 -p https://zhuanlan.zhihu.com/p/497349204 -w 600 -h 800\n" + |
| 34 | "- >github xxx\n", |
| 35 | PrivateDataFolder: "playwright", |
| 36 | }).ApplySingle(single.New( |
| 37 | single.WithKeyFn(func(ctx *zero.Ctx) int64 { return ctx.Event.GroupID }), |
| 38 | single.WithPostFn[int64](func(ctx *zero.Ctx) { |
| 39 | ctx.Send( |
| 40 | message.ReplyWithMessage(ctx.Event.MessageID, |
| 41 | message.Text("已经有正在进行的网页截图..."), |
| 42 | ), |
| 43 | ) |
| 44 | }), |
| 45 | )) |
| 46 | cachePath := engine.DataFolder() + "cache/" |
| 47 | _ = os.RemoveAll(cachePath) |
| 48 | _ = os.MkdirAll(cachePath, 0755) |
| 49 | screenshot := func(ctx *zero.Ctx) (picPath string) { |
| 50 | pageURL := ctx.State["pageURL"].(string) |
| 51 | width := ctx.State["width"].(float64) |
| 52 | height := ctx.State["height"].(float64) |
| 53 | uid := ctx.Event.UserID |
| 54 | now := time.Now() |
| 55 | today := now.Format("20060102") |
| 56 | pwFile := cachePath + strconv.FormatInt(uid, 10) + today + "playwright.png" |
| 57 | ctx.SendChain(message.Text("少女祈祷中...")) |
| 58 | pw, err := playwright.Run() |
| 59 | if err != nil { |
| 60 | ctx.Send(fmt.Sprintf("could not start playwright: %v", err)) |
| 61 | return |
| 62 | } |
| 63 | browser, err := pw.Chromium.Launch() |
| 64 | if err != nil { |
| 65 | ctx.Send(fmt.Sprintf("could not launch browser: %v", err)) |
| 66 | return |
| 67 | } |
| 68 | device := pw.Devices["Pixel 5"] |
| 69 | context, err := browser.NewContext(playwright.BrowserNewContextOptions{ |
| 70 | Geolocation: &playwright.Geolocation{ |
| 71 | Longitude: 12.492507, |
| 72 | Latitude: 41.889938, |
| 73 | }, |
| 74 | Permissions: []string{"geolocation"}, |
| 75 | Viewport: device.Viewport, |
| 76 | UserAgent: playwright.String(device.UserAgent), |
| 77 | DeviceScaleFactor: playwright.Float(device.DeviceScaleFactor), |
| 78 | IsMobile: playwright.Bool(device.IsMobile), |
| 79 | HasTouch: playwright.Bool(device.HasTouch), |
| 80 | }) |
| 81 | if err != nil { |
| 82 | ctx.Send(fmt.Sprintf("could not create context: %v", err)) |
| 83 | return |
| 84 | } |
| 85 | page, err := context.NewPage() |
| 86 | if err != nil { |