“Requested device not found” when using chrome.tabCapture.capture

南楼画角 提交于 2020-01-06 14:01:52

问题


Problem

I want to capture the audio output of a tab automatically. I'm currently thinking of doing this using Puppeteer (headful), by loading an extension that uses chrome.tabCapture.capture. From my Puppeteer script, I evaluate code within the extensions background.js to get the tab capture started. However, chrome.runtime.lastError.message is set to Requested device not found.

The extension works as expected outside of Puppeteer and in a Chrome browser.

Any idea why I'm getting Requested device not found?

What does the extension's background.js look like?

function startRecording() {
    chrome.tabCapture.capture(options, stream => {
        if (stream === null) {
            console.log(`Last Error: ${chrome.runtime.lastError.message}`);
            return;
        }
        try {
            const recorder = new MediaRecorder(stream);
        } catch (err) {
            console.log(err.message);
        }
        recorder.addEventListener('dataavailable', event => {
            const { data: blob, timecode } = event;
            console.log(`${timecode}: ${blob}`);
        });
        const timeslice = 60 * 1000;
        recorder.start(timeslice);
    });
}

What does the relevant part of your Puppeteer script look like?

...

const targets = await browser.targets();
const backgroundPageTarget = targets.find(target => target.type() === 'background_page' && target.url().startsWith('chrome-extension://abcde/'));
const backgroundPage = await backgroundPageTarget.page();

const test = await backgroundPage.evaluate(() => {
    startRecording();
    return Promise.resolve(42);
});

...

Extension Manifest:

{
    "name": "Test",
    "description": "",
    "version": "1.0",
    "icons": {
        "128": "icon.png"
    },
    "manifest_version": 2,
    "browser_action": {
        "default_popup": "test.html"
    },
    "background": {
        "scripts": [
            "background.js"
        ],
        "persistent": true
    },
    "content_scripts": [
        {
            "matches": [
                "<all_urls>"
            ],
            "all_frames": false,
            "js": [
                "contentScript.js"
            ]
        }
    ],
    "permissions": [
        "activeTab",
        "tabs",
        "tabCapture",
        "storage"
    ]
}

来源:https://stackoverflow.com/questions/53725650/requested-device-not-found-when-using-chrome-tabcapture-capture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!