This commit is contained in:
Nebel 2023-12-14 00:12:51 +09:00
parent 345f71de3d
commit d907875d03
Signed by: nebel
GPG key ID: 79807D08C6EF6460

View file

@ -22,6 +22,59 @@ type File struct {
Data []byte
}
type Browser struct {
pw *playwright.Playwright
browser playwright.Browser
}
func Install() error {
opts := playwright.RunOptions{
Browsers: []string{"chromium"},
}
return playwright.Install(&opts)
}
type RunOptions struct {
DB Database
Headless *bool
}
func Run(options RunOptions) (*Browser, error) {
pw, err := playwright.Run()
if err != nil {
return nil, err
}
launchOptions := playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(*options.Headless),
}
b, err := pw.Chromium.Launch(launchOptions)
if err != nil {
return nil, err
}
browser := Browser{
pw: pw,
browser: b,
}
return &browser, nil
}
func (b Browser) Stop() error {
return b.pw.Stop()
}
func (b Browser) NewContext() (playwright.BrowserContext, error) {
return b.browser.NewContext()
}
// TODO
// Browser LoadBrowserContext(platform: TPlatform): Promise<Playwright.BrowserContext>;
// Browser SaveBrowserContext(platform: TPlatform, ctx: BrowserContext): Promise<void>;
var draw = `
async function drawImage(imageFile) {
const canvas = Object.assign(document.createElement("canvas"), {
@ -98,58 +151,6 @@ func parseDataURL(dataURL string) (*File, error) {
return &file, nil
}
type Browser struct {
pw *playwright.Playwright
browser playwright.Browser
}
func Install() error {
opts := playwright.RunOptions{
Browsers: []string{"chromium"},
}
return playwright.Install(&opts)
}
type RunOptions struct {
DB Database
Headless *bool
}
func Run(options RunOptions) (*Browser, error) {
pw, err := playwright.Run()
if err != nil {
return nil, err
}
launchOptions := playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(*options.Headless),
}
b, err := pw.Chromium.Launch(launchOptions)
if err != nil {
return nil, err
}
browser := Browser{
pw: pw,
browser: b,
}
return &browser, nil
}
func (b Browser) Stop() error {
return b.pw.Stop()
}
func (b Browser) NewContext() (playwright.BrowserContext, error) {
return b.browser.NewContext()
}
// TODO
// Browser LoadBrowserContext(platform: TPlatform): Promise<Playwright.BrowserContext>;
// Browser SaveBrowserContext(platform: TPlatform, ctx: BrowserContext): Promise<void>;
// Browser DrawImage(
// pageOrFrame: Playwright.Page | Playwright.Frame,
// imageFile: ImageFile,