Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@progrium
Last active August 17, 2018 19:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progrium/6e205cba1cf33521019f159f4ab1a952 to your computer and use it in GitHub Desktop.
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
#!/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();
})();
{
"dependencies": {
"puppeteer": "^1.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment