使用chrome.tabCapture.capture时出现“找不到请求的设备”

时间:2018-12-11 13:57:45

标签: selenium google-chrome-extension puppeteer

问题

我想自动捕获选项卡的音频输出。我目前正在考虑通过加载使用chrome.tabCapture.capture的扩展名来使用Puppeteer(头部)进行此操作。从我的Puppeteer脚本中,我评估扩展background.js中的代码以开始捕获选项卡。但是,chrome.runtime.lastError.message设置为Requested device not found

该扩展程序可以在Puppeteer外部和Chrome浏览器中正常工作。

知道我为什么得到Requested device not found吗?

扩展名的background.js是什么样的?

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);
    });
}

Puppeteer脚本的相关部分是什么样的?

...

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);
});

...

扩展清单:

{
    "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"
    ]
}

0 个答案:

没有答案