Last active
January 10, 2019 06:18
Revisions
-
mafredri revised this gist
Aug 24, 2018 . 1 changed file with 3 additions and 7 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,12 @@ // This example gives permission to access geolocation // and fetches the current position via JavaScript. // Tested on: cdp v0.18.7 and Chrome Canary 70.0.3532.0, // and seems to work in both headless and headfull mode. package main import ( "context" "fmt" "log" "time" @@ -39,11 +39,7 @@ func run(timeout time.Duration) error { } } conn, err := rpcc.DialContext(ctx, pt.WebSocketDebuggerURL) if err != nil { return err } -
mafredri revised this gist
Aug 24, 2018 . 1 changed file with 20 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,18 @@ // This example gives permission to access geolocation // and fetches the current position via JavaScript. // Tested on: cdp v0.18.7 and Chrome Canary 70.0.3532.0. package main import ( "context" "fmt" "io" "log" "time" "github.com/mafredri/cdp" "github.com/mafredri/cdp/devtool" "github.com/mafredri/cdp/protocol/browser" "github.com/mafredri/cdp/protocol/emulation" "github.com/mafredri/cdp/protocol/page" "github.com/mafredri/cdp/protocol/runtime" @@ -34,7 +39,11 @@ func run(timeout time.Duration) error { } } newLogCodec := func(conn io.ReadWriter) rpcc.Codec { return &LogCodec{conn: conn} } conn, err := rpcc.DialContext(ctx, pt.WebSocketDebuggerURL, rpcc.WithCodec(newLogCodec)) if err != nil { return err } @@ -73,6 +82,15 @@ func run(timeout time.Duration) error { fmt.Printf("Page loaded with frame ID: %s\n", nav.FrameID) err = c.Browser.GrantPermissions(ctx, browser.NewGrantPermissionsArgs("https://www.google.com", []browser.PermissionType{ browser.PermissionTypeGeolocation, }), ) if err != nil { return err } eval, err := c.Runtime.Evaluate(ctx, runtime.NewEvaluateArgs(` new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition( @@ -89,6 +107,7 @@ func run(timeout time.Duration) error { SetAwaitPromise(true). SetReturnByValue(true), ) if err != nil { return err } -
mafredri revised this gist
Aug 20, 2018 . 1 changed file with 10 additions and 6 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -73,18 +73,22 @@ func run(timeout time.Duration) error { fmt.Printf("Page loaded with frame ID: %s\n", nav.FrameID) eval, err := c.Runtime.Evaluate(ctx, runtime.NewEvaluateArgs(` new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition( (pos) => { // The protocol won't return this object straight, so we clone it. const {latitude, longitude, accuracy} = pos.coords; resolve({latitude, longitude, accuracy}); }, reject, {timeout: 10000} ); }); `). SetAwaitPromise(true). SetReturnByValue(true), ) if err != nil { return err } -
mafredri created this gist
Aug 20, 2018 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ package main import ( "context" "fmt" "log" "time" "github.com/mafredri/cdp" "github.com/mafredri/cdp/devtool" "github.com/mafredri/cdp/protocol/emulation" "github.com/mafredri/cdp/protocol/page" "github.com/mafredri/cdp/protocol/runtime" "github.com/mafredri/cdp/rpcc" ) func main() { err := run(15 * time.Second) if err != nil { log.Fatal(err) } } func run(timeout time.Duration) error { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() devt := devtool.New("http://127.0.0.1:9222") pt, err := devt.Get(ctx, devtool.Page) if err != nil { pt, err = devt.Create(ctx) if err != nil { return err } } conn, err := rpcc.DialContext(ctx, pt.WebSocketDebuggerURL) if err != nil { return err } defer conn.Close() c := cdp.NewClient(conn) domContent, err := c.Page.DOMContentEventFired(ctx) if err != nil { return err } defer domContent.Close() if err = c.Page.Enable(ctx); err != nil { return err } overrideArgs := emulation.NewSetGeolocationOverrideArgs(). SetLatitude(10). SetLongitude(22). SetAccuracy(100) err = c.Emulation.SetGeolocationOverride(ctx, overrideArgs) if err != nil { return err } navArgs := page.NewNavigateArgs("https://www.google.com") nav, err := c.Page.Navigate(ctx, navArgs) if err != nil { return err } if _, err = domContent.Recv(); err != nil { return err } fmt.Printf("Page loaded with frame ID: %s\n", nav.FrameID) eval, err := c.Runtime.Evaluate(ctx, runtime.NewEvaluateArgs( `new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition( (pos) => { const c = pos.coords; resolve([c.latitude, c.longitude, c.accuracy].join(', ')); }, reject, {timeout: 10000} ); });`, ).SetAwaitPromise(true)) if err != nil { return err } if eval.ExceptionDetails != nil { return eval.ExceptionDetails } fmt.Println(eval.Result.String()) return nil }