Last active
February 14, 2023 10:55
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 characters
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. |
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 characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you add a license to this?