Skip to content

Instantly share code, notes, and snippets.

@Ariex
Created January 23, 2018 01:09
Show Gist options
  • Save Ariex/6f425fbcab09e13d8bec39aba7c4a9b5 to your computer and use it in GitHub Desktop.
Save Ariex/6f425fbcab09e13d8bec39aba7c4a9b5 to your computer and use it in GitHub Desktop.
const Page = require('puppeteer/lib/Page');
// the following 2 methods are originally from: https://github.com/GoogleChrome/puppeteer/issues/85#issuecomment-341743259, with some modification to fit puppeteerv1.0.0
async function newPageWithNewContext(browser) {
const { browserContextId } = await browser._connection.send('Target.createBrowserContext');
const { targetId } = await browser._connection.send('Target.createTarget', { url: 'about:blank', browserContextId });
const target = await browser._targets.get(targetId);
const client = await browser._connection.createSession(targetId);
const page = await Page.create(client, target, browser._ignoreHTTPSErrors, browser._appMode, browser._screenshotTaskQueue);
page.browserContextId = browserContextId;
return page;
}
async function closePage(browser, page) {
if (page.browserContextId != undefined) {
await browser._connection.send('Target.disposeBrowserContext', { browserContextId: page.browserContextId });
}
await page.close();
}
module.exports = {
beginIncognito: async browser => {
let page = await newPageWithNewContext(browser);
page.endIncognito = async() => closePage(browser, page);
return page;
}
};
@samjmck
Copy link

samjmck commented Feb 19, 2018

Thanks for this. However, I've noticed that the _targets property isn't set in the browser object – at least not in the version from npm.

@Raidus
Copy link

Raidus commented Mar 20, 2018

Thanks for the example. It seems that microsoft has changed their cookie names. MS0 has to be checked instead of MUID in my case.

@Ariex
Copy link
Author

Ariex commented Jul 3, 2018

official API available since v1.5.0: browser.createIncognitoBrowserContext()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment