Last active
August 17, 2018 19:43
-
-
Save progrium/6e205cba1cf33521019f159f4ab1a952 to your computer and use it in GitHub Desktop.
Facebook no longer allows news feed posts to be added programmatically via API. But we can still post them programmatically... https://medium.com/@progrium/the-only-way-you-can-automate-facebook-posts-now-bd3a40fd1c4b
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
#!/usr/bin/env node | |
// Usage: | |
// Login manually, saving your authenticated session in "data": | |
// $ ./fbpost --login | |
// | |
// Now you can post whatever you want via argument: | |
// $ ./fbpost "Hello world, this is a post!" | |
// | |
(async() => { | |
const puppeteer = require('puppeteer'); | |
const arg = process.argv[2]; | |
const browser = await puppeteer.launch({ | |
headless: (arg !== "--login"), | |
userDataDir: "data" | |
}); | |
const page = (await browser.pages())[0]; | |
await page.goto('https://www.facebook.com'); | |
if (arg === "--login") { | |
// wait for homepage to load after a manual login | |
await page.waitFor('div[aria-label="Create a post"]', {"timeout": 180000}); | |
} else { | |
// use keyboard shortcut to open and focus on create new post | |
await page.keyboard.press('KeyP'); | |
// wait for emoji icon as proxy for "loaded and post ready" | |
await page.waitFor('div[aria-label="Create a post"] a[aria-label="Insert an emoji"]') | |
// keyboard shortcut put focus in place so we can just type | |
await page.keyboard.type(arg); | |
// click submit. TODO: wait for URLs to be expanded | |
await page.click('div[aria-label="Create a post"] button[type=submit]'); | |
// can’t find reliable way to detect that it posted successfully, | |
// but if we close too soon it won’t finish the post request | |
await new Promise(res => setTimeout(res, 2000)); | |
} | |
await browser.close(); | |
})(); |
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
{ | |
"dependencies": { | |
"puppeteer": "^1.5.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment