Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
puppeteer sample
const puppeteer = require("puppeteer");
const sendgrid = require("sendgrid").mail;
const sendgridApiKey = process.env.SENDGRID_API_KEY;
const fromEmail = "noreply@example.com";
const toEmail = "to@example.com";
const targetUrl = "https://example.com";
puppeteer.launch().then(async browser => {
try {
const page = await browser.newPage();
await page.goto(targetUrl);
const message = await page.mainFrame().evaluate(() => {
try {
var e = document.getElementsByClassName("SAMPLE")[0].children;
for (var i = 0; i < e.length; i++) {
if ("SOLD OUT" === e[i].innerText) {
return "SOLD OUT";
}
}
return "NOW ON SALE";
} catch (error) {
console.log(error);
return "BUSY";
}
});
const subject = message;
const content = new sendgrid.Content("text/plain", message);
const mail = new sendgrid.Mail(fromEmail, subject, toEmail, content);
const sendgridClient = require("sendgrid")(sendgridApiKey);
const request = sendgridClient.emptyRequest({
method: "POST",
path: "/v3/mail/send",
body: mail.toJSON()
});
sendgridClient.API(request, function (error, response) {
if (error) {
console.log("Error response received");
}
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
console.log("status: " + status);
} catch (error) {
console.log(error);
} finally {
browser.close();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment