Created
November 2, 2017 04:00
-
-
Save kiyoaki/88a5f424018089b7afc7effc011d92fd to your computer and use it in GitHub Desktop.
puppeteer sample
This file contains hidden or 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
const puppeteer = require("puppeteer"); | |
const sendgrid = require("sendgrid").mail; | |
const sendgridApiKey = process.env.SENDGRID_API_KEY; | |
const fromEmail = "[email protected]"; | |
const toEmail = "[email protected]"; | |
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