Chrome 扩展 - getUserMedia 抛出“NotAllowedError:因关闭而失败"; [英] Chrome Extension - getUserMedia throws "NotAllowedError: Failed due to shutdown"

查看:162
本文介绍了Chrome 扩展 - getUserMedia 抛出“NotAllowedError:因关闭而失败";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套件可以记录用户的网络摄像头和麦克风.它在网页上效果很好,但在 Chrome 扩展程序中:

I have a suite which records the user's webcam and mic. It works great on a webpage, but in a Chrome Extension the line:

navigator.mediaDevices.getUserMedia({video: true, audio: true})
    .then(this.record.bind(this))
    .catch(VidRA.error);

正在投掷

NotAllowedError: Failed due to shutdown

我已经搜索过,几乎找不到任何可以解释这一点的内容.有没有其他人遇到过这个问题,或者有没有人知道我能做些什么?

I've searched and found almost nothing that might explain this. Has anyone else come across this or does anyone know what I can do about it?

推荐答案

哇,这是一个雷区.

首先,这似乎是一个错误(感谢@wOxxOm).

Firstly, it seems this is a bug (thanks, @wOxxOm).

所以我们需要围绕它编码.

So we need to code around it.

由于后台脚本在请求媒体访问时会产生这个bug,所以我们需要在别处请求它.如果您尝试从弹出 JS 中尝试,则会生成相同的错误,因此会留下 内容脚本.

Since the background script generates this bug when requesting media access, we need to request it elsewhere. The same error is generated if you try from the popup JS, so that leaves content scripts.

步骤如下:

  • 内容脚本请求访问媒体设备

  • Content script requests access to media devices

成功后,内容脚本通知后台脚本

On success, content script notifies background script

收到消息后,后台脚本请求访问媒体设备;由于内容脚本已经成功,后台脚本现在也会成功

On receipt of message, background script requests acccess to media devices; since content script has already succeeded, background script will now also succeed

至关重要的是,内容脚本必须在扩展程序的上下文中运行,而不是当前网页(在活动选项卡中).也就是说,内容脚本必须通过以 chrome://,由 chrome.runtime.getURL() 生成.

Crucially, the content script must run in the context of the extension, NOT the current webpage (in the active tab.) That is, the content script must do so via a URL beginning chrome://, generated by chrome.runtime.getURL().

这样一来,用户只需要获得一次权限,因为他们的决定是在逐个域的基础上记住的.在扩展程序的上下文中运行脚本,而不是当前网页,这意味着总是在同一个基于扩展程序的域中请求许可(并记住决定),而不是针对每个访问过的网站域再次询问.

This is so the user is asked for permissions only once, since their decision is remembered on a domain-by-domain basis. Running the script in the context of the extension, not the current webpage, means permission is always asked (and the decision remembered) in the same, extension-based domain, not again for every website domain visited.

我的方法是有一个持久性内容脚本(在 content_scripts 下的清单中指定),其工作是在打开扩展程序时将 iframe 注入当前选项卡的页面.

My approach was to have a persistent content script (specified in the manifest under content_scripts) whose job, when the extension is opened, is to inject an iframe into the current tab's page.

从扩展中加载一个页面到 iframe 中,我们称之为 iframe.html,它最终负责请求访问.

Into that iframe is loaded a page from the extension, let's call it iframe.html, which is ultimately responsible for requesting access.

呸...

这篇关于Chrome 扩展 - getUserMedia 抛出“NotAllowedError:因关闭而失败";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆