diff --git a/cdproto.go b/cdproto.go index e9253c6c..c30f1b55 100644 --- a/cdproto.go +++ b/cdproto.go @@ -551,6 +551,7 @@ const ( CommandPWAGetOsAppState = pwa.CommandGetOsAppState CommandPWAInstall = pwa.CommandInstall CommandPWAUninstall = pwa.CommandUninstall + CommandPWALaunch = pwa.CommandLaunch CommandPageAddScriptToEvaluateOnNewDocument = page.CommandAddScriptToEvaluateOnNewDocument CommandPageBringToFront = page.CommandBringToFront CommandPageCaptureScreenshot = page.CommandCaptureScreenshot @@ -2251,6 +2252,9 @@ func UnmarshalMessage(msg *Message) (interface{}, error) { case CommandPWAUninstall: return emptyVal, nil + case CommandPWALaunch: + v = new(pwa.LaunchReturns) + case CommandPageAddScriptToEvaluateOnNewDocument: v = new(page.AddScriptToEvaluateOnNewDocumentReturns) diff --git a/preload/types.go b/preload/types.go index 27d50fcd..f5852133 100644 --- a/preload/types.go +++ b/preload/types.go @@ -277,6 +277,9 @@ const ( PrerenderFinalStatusPrerenderingURLHasEffectiveURL PrerenderFinalStatus = "PrerenderingUrlHasEffectiveUrl" PrerenderFinalStatusRedirectedPrerenderingURLHasEffectiveURL PrerenderFinalStatus = "RedirectedPrerenderingUrlHasEffectiveUrl" PrerenderFinalStatusActivationURLHasEffectiveURL PrerenderFinalStatus = "ActivationUrlHasEffectiveUrl" + PrerenderFinalStatusJavaScriptInterfaceAdded PrerenderFinalStatus = "JavaScriptInterfaceAdded" + PrerenderFinalStatusJavaScriptInterfaceRemoved PrerenderFinalStatus = "JavaScriptInterfaceRemoved" + PrerenderFinalStatusAllPrerenderingCanceled PrerenderFinalStatus = "AllPrerenderingCanceled" ) // MarshalEasyJSON satisfies easyjson.Marshaler. @@ -423,6 +426,12 @@ func (t *PrerenderFinalStatus) UnmarshalEasyJSON(in *jlexer.Lexer) { *t = PrerenderFinalStatusRedirectedPrerenderingURLHasEffectiveURL case PrerenderFinalStatusActivationURLHasEffectiveURL: *t = PrerenderFinalStatusActivationURLHasEffectiveURL + case PrerenderFinalStatusJavaScriptInterfaceAdded: + *t = PrerenderFinalStatusJavaScriptInterfaceAdded + case PrerenderFinalStatusJavaScriptInterfaceRemoved: + *t = PrerenderFinalStatusJavaScriptInterfaceRemoved + case PrerenderFinalStatusAllPrerenderingCanceled: + *t = PrerenderFinalStatusAllPrerenderingCanceled default: in.AddError(fmt.Errorf("unknown PrerenderFinalStatus value: %v", v)) diff --git a/pwa/easyjson.go b/pwa/easyjson.go index ca44d801..69ea7924 100644 --- a/pwa/easyjson.go +++ b/pwa/easyjson.go @@ -4,6 +4,7 @@ package pwa import ( json "encoding/json" + target "github.com/chromedp/cdproto/target" easyjson "github.com/mailru/easyjson" jlexer "github.com/mailru/easyjson/jlexer" jwriter "github.com/mailru/easyjson/jwriter" @@ -83,7 +84,147 @@ func (v *UninstallParams) UnmarshalJSON(data []byte) error { func (v *UninstallParams) UnmarshalEasyJSON(l *jlexer.Lexer) { easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa(l, v) } -func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(in *jlexer.Lexer, out *InstallParams) { +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(in *jlexer.Lexer, out *LaunchReturns) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeFieldName(false) + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "targetId": + out.TargetID = target.ID(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(out *jwriter.Writer, in LaunchReturns) { + out.RawByte('{') + first := true + _ = first + if in.TargetID != "" { + const prefix string = ",\"targetId\":" + first = false + out.RawString(prefix[1:]) + out.String(string(in.TargetID)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v LaunchReturns) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v LaunchReturns) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *LaunchReturns) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *LaunchReturns) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(l, v) +} +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(in *jlexer.Lexer, out *LaunchParams) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeFieldName(false) + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "manifestId": + out.ManifestID = string(in.String()) + case "url": + out.URL = string(in.String()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(out *jwriter.Writer, in LaunchParams) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"manifestId\":" + out.RawString(prefix[1:]) + out.String(string(in.ManifestID)) + } + if in.URL != "" { + const prefix string = ",\"url\":" + out.RawString(prefix) + out.String(string(in.URL)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v LaunchParams) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v LaunchParams) MarshalEasyJSON(w *jwriter.Writer) { + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *LaunchParams) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *LaunchParams) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(l, v) +} +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(in *jlexer.Lexer, out *InstallParams) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -116,7 +257,7 @@ func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(in *jlexer.Lexer, out *I in.Consumed() } } -func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(out *jwriter.Writer, in InstallParams) { +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(out *jwriter.Writer, in InstallParams) { out.RawByte('{') first := true _ = first @@ -136,27 +277,27 @@ func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(out *jwriter.Writer, in // MarshalJSON supports json.Marshaler interface func (v InstallParams) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(&w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v InstallParams) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa1(w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *InstallParams) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(&r, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *InstallParams) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa1(l, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(l, v) } -func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(in *jlexer.Lexer, out *GetOsAppStateReturns) { +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(in *jlexer.Lexer, out *GetOsAppStateReturns) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -218,7 +359,7 @@ func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(in *jlexer.Lexer, out *G in.Consumed() } } -func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(out *jwriter.Writer, in GetOsAppStateReturns) { +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(out *jwriter.Writer, in GetOsAppStateReturns) { out.RawByte('{') first := true _ = first @@ -257,27 +398,27 @@ func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(out *jwriter.Writer, in // MarshalJSON supports json.Marshaler interface func (v GetOsAppStateReturns) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(&w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v GetOsAppStateReturns) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa2(w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *GetOsAppStateReturns) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(&r, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *GetOsAppStateReturns) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa2(l, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(l, v) } -func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(in *jlexer.Lexer, out *GetOsAppStateParams) { +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(in *jlexer.Lexer, out *GetOsAppStateParams) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -308,7 +449,7 @@ func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(in *jlexer.Lexer, out *G in.Consumed() } } -func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(out *jwriter.Writer, in GetOsAppStateParams) { +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(out *jwriter.Writer, in GetOsAppStateParams) { out.RawByte('{') first := true _ = first @@ -323,27 +464,27 @@ func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(out *jwriter.Writer, in // MarshalJSON supports json.Marshaler interface func (v GetOsAppStateParams) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(&w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v GetOsAppStateParams) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa3(w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *GetOsAppStateParams) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(&r, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *GetOsAppStateParams) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa3(l, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(l, v) } -func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(in *jlexer.Lexer, out *FileHandlerAccept) { +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa6(in *jlexer.Lexer, out *FileHandlerAccept) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -397,7 +538,7 @@ func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(in *jlexer.Lexer, out *F in.Consumed() } } -func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(out *jwriter.Writer, in FileHandlerAccept) { +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa6(out *jwriter.Writer, in FileHandlerAccept) { out.RawByte('{') first := true _ = first @@ -428,27 +569,27 @@ func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(out *jwriter.Writer, in // MarshalJSON supports json.Marshaler interface func (v FileHandlerAccept) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(&w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa6(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v FileHandlerAccept) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa4(w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa6(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *FileHandlerAccept) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(&r, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa6(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *FileHandlerAccept) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa4(l, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa6(l, v) } -func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(in *jlexer.Lexer, out *FileHandler) { +func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa7(in *jlexer.Lexer, out *FileHandler) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -512,7 +653,7 @@ func easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(in *jlexer.Lexer, out *F in.Consumed() } } -func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(out *jwriter.Writer, in FileHandler) { +func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa7(out *jwriter.Writer, in FileHandler) { out.RawByte('{') first := true _ = first @@ -552,23 +693,23 @@ func easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(out *jwriter.Writer, in // MarshalJSON supports json.Marshaler interface func (v FileHandler) MarshalJSON() ([]byte, error) { w := jwriter.Writer{} - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(&w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa7(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v FileHandler) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa5(w, v) + easyjsonC5a4559bEncodeGithubComChromedpCdprotoPwa7(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *FileHandler) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(&r, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa7(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *FileHandler) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa5(l, v) + easyjsonC5a4559bDecodeGithubComChromedpCdprotoPwa7(l, v) } diff --git a/pwa/pwa.go b/pwa/pwa.go index 2c280c49..ee4e4b65 100644 --- a/pwa/pwa.go +++ b/pwa/pwa.go @@ -12,6 +12,7 @@ import ( "context" "github.com/chromedp/cdproto/cdp" + "github.com/chromedp/cdproto/target" ) // GetOsAppStateParams returns the following OS state for the given manifest @@ -126,9 +127,62 @@ func (p *UninstallParams) Do(ctx context.Context) (err error) { return cdp.Execute(ctx, CommandUninstall, p, nil) } +// LaunchParams launches the installed web app, or an url in the same web app +// instead of the default start url if it is provided. Returns a tab / web +// contents based Target.TargetID which can be used to attach to via +// Target.attachToTarget or similar APIs. +type LaunchParams struct { + ManifestID string `json:"manifestId"` + URL string `json:"url,omitempty"` +} + +// Launch launches the installed web app, or an url in the same web app +// instead of the default start url if it is provided. Returns a tab / web +// contents based Target.TargetID which can be used to attach to via +// Target.attachToTarget or similar APIs. +// +// See: https://chromedevtools.github.io/devtools-protocol/tot/PWA#method-launch +// +// parameters: +// +// manifestID +func Launch(manifestID string) *LaunchParams { + return &LaunchParams{ + ManifestID: manifestID, + } +} + +// WithURL [no description]. +func (p LaunchParams) WithURL(url string) *LaunchParams { + p.URL = url + return &p +} + +// LaunchReturns return values. +type LaunchReturns struct { + TargetID target.ID `json:"targetId,omitempty"` // ID of the tab target created as a result. +} + +// Do executes PWA.launch against the provided context. +// +// returns: +// +// targetID - ID of the tab target created as a result. +func (p *LaunchParams) Do(ctx context.Context) (targetID target.ID, err error) { + // execute + var res LaunchReturns + err = cdp.Execute(ctx, CommandLaunch, p, &res) + if err != nil { + return "", err + } + + return res.TargetID, nil +} + // Command names. const ( CommandGetOsAppState = "PWA.getOsAppState" CommandInstall = "PWA.install" CommandUninstall = "PWA.uninstall" + CommandLaunch = "PWA.launch" )