Jay
Jay

Reputation: 3489

Auto allow webcam access using Puppeteer for Node.js

I'm setting up a test that involves starting a webcam video session.

So far all is working fine and doesn't require any user interaction except for granting access to the webcam.

When the third party library I'm using makes the call: navigator.mediaDevices.getUserMedia({audio: true, video: true}) the browser opens a prompt asking the user to allow access.

What I'm looking for is a way to grant access without user interaction.

I've tried puppeteer's page.on('dialog'... but that doesn't get called for the webcam access prompt.

Please let me know if you have any ideas?

Upvotes: 11

Views: 10160

Answers (1)

user2080739
user2080739

Reputation: 436

Google Chrome has a launch option --use-fake-ui-for-media-stream that allows the user to skip a prompt of getUserMedia. And you can set it with puppeteer like below.

const puppeteer = require('puppeteer')
;(async () => {
    const browser = await puppeteer.launch({
        args: [ '--use-fake-ui-for-media-stream' ]
    })
    const page = await browser.newPage()
    await page.goto('http://localhost/start-video-test.html')
    const startVideoButton = await page.$('#startVideoButton')
    startVideoButton.click()
    // video session starts without prompt
    return browser.close()
})()

Upvotes: 32

Related Questions