-
-
Save bizfreak22/6ac6ff1883297e4df8b57f973395f200 to your computer and use it in GitHub Desktop.
This is a code snippet that allows you to login to facebook via puppeteer.
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
const puppeteer = require('puppeteer'); | |
const fs = require('fs'); | |
const config = require('./config.json'); | |
const cookies = require('./cookies.json'); | |
(async () => { | |
console.log('Hello World'); | |
//insert code here | |
let browser = await puppeteer.launch({ headless: false }); | |
const context = browser.defaultBrowserContext(); | |
context.overridePermissions("https://www.facebook.com", []); | |
let page = await browser.newPage(); | |
await page.setDefaultNavigationTimeout(100000); | |
await page.setViewport({ width: 1200, height: 800 }); | |
if (!Object.keys(cookies).length) { | |
await page.goto("https://www.facebook.com/login", { waitUntil: "networkidle2" }); | |
await page.type("#email", config.username, { delay: 30 }) | |
await page.type("#pass", config.password, { delay: 30 }) | |
await page.click("#loginbutton"); | |
await page.waitForNavigation({ waitUntil: "networkidle0" }); | |
await page.waitFor(15000); | |
try { | |
await page.waitFor('[data-click="profile_icon"]'); | |
} catch (err) { | |
console.log("failed to login"); | |
process.exit(0); | |
} | |
let currentCookies = await page.cookies(); | |
fs.writeFileSync('./cookies.json', JSON.stringify(currentCookies)); | |
} else{ | |
//User Already Logged In | |
await page.setCookie(...cookies); | |
await page.goto("https://www.facebook.com/", { waitUntil: "networkidle2" }); | |
} | |
//Close Browser | |
//await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment