Skip to content

Instantly share code, notes, and snippets.

@efimovalex
Last active February 14, 2023 10:55
Show Gist options
  • Save efimovalex/9f9b815b0d5b1b7889a51d46860faf8a to your computer and use it in GitHub Desktop.
Save efimovalex/9f9b815b0d5b1b7889a51d46860faf8a to your computer and use it in GitHub Desktop.
MIT License
Copyright (c) 2022 Efimov Ioan-Alexandru
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
func (s *Service) processPage(id string, job Job) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), s.ContextTimeout)
defer cancel()
url := fmt.Sprintf("http://%s", s.CDTAddress)
devt := devtool.New(url)
tab, err := devt.CreateURL(ctx, "https://google.com")
if err != nil {
return err
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), s.ContextTimeout)
devt.Close(ctx, tab)
cancel()
}()
// Initiate a new RPC connection to the Chrome Debugging Protocol target.
conn, err := rpcc.DialContext(ctx, tab.WebSocketDebuggerURL)
if err != nil {
// wait for chrome instance to restart in case it failed
time.Sleep(5 * time.Second)
return err
}
defer conn.Close() // Leaving connections open will leak memory.
// Give enough capacity to avoid blocking any event listeners
c := cdp.NewClient(conn)
if err = c.Security.Enable(ctx); err != nil {
return err
}
emArgs := emulation.NewSetDeviceMetricsOverrideArgs(1152, 720, 2, false)
if err = c.Emulation.SetDeviceMetricsOverride(ctx, emArgs); err != nil {
return err
}
secIgnArgs := security.NewSetIgnoreCertificateErrorsArgs(true)
if err = c.Security.SetIgnoreCertificateErrors(ctx, secIgnArgs); err != nil {
return err
}
if err = c.Runtime.Enable(ctx); err != nil {
return err
}
networkEnableArgs := network.NewEnableArgs()
if err = c.Network.Enable(ctx, networkEnableArgs); err != nil {
return err
}
// Create the Navigate arguments with the optional Referrer field set.
URL := "http://Your.URL"
navArgs := page.NewNavigateArgs(URL)
_, err = c.Page.Navigate(ctx, navArgs)
if err != nil {
return err
}
response, err := c.Network.ResponseReceived(ctx)
if err != nil {
return err
}
resp, err := response.Recv()
if err != nil {
return err
}
if resp.Response.Status != http.StatusOK {
return fmt.Errorf("Page responded with status %d on %s", resp.Response.Status, URL)
}
// wait for js to load and change the window.status to 'ready_to_print'
expression := `
new Promise((resolve, reject) => {
var start = Date.now();
check = (resolve, reject) => {
if (window.status == 'ready_to_print') {
resolve(window.status);
} else {
setTimeout(check.bind(this, resolve, reject));
}
};
setTimeout(() => {
window.status = 'ready_to_print';
}, 25000);
return check(resolve, reject);
});`
evalArgs := runtime.NewEvaluateArgs(expression).SetAwaitPromise(true).SetReturnByValue(true)
_, err = c.Runtime.Evaluate(ctx, evalArgs)
if err := cdp.ErrorCause(err); err != nil {
if err, ok := err.(*rpcc.ResponseError); ok {
s.Logger.Err("rpcc error", "err", err)
return err
}
return err
}
// Capture a screenshot of the current page.
screenshotArgs := page.NewCaptureScreenshotArgs().
SetFormat("jpeg").
SetQuality(100)
normal, err := c.Page.CaptureScreenshot(ctx, screenshotArgs)
if err != nil {
return err
}
img, err := jpeg.Decode(bytes.NewReader(normal.Data))
if err != nil {
s.Logger.Err("decoding jpeg image", "err", err.Error())
return err
}
// Upload img to aws.
return nil
}
@austincollinpena
Copy link

Can you add a license to this?

@efimovalex
Copy link
Author

added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment