Chrome Extension - getUserMedia throws “NotAllowedError: Failed due to shutdown”

拥有回忆 提交于 2020-01-01 05:24:06

问题


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

is throwing

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?


回答1:


Wow, this is a minefield.

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

So we need to code around it.

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.

The steps, then, are these:

  • Content script requests access to media devices

  • On success, content script messages 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

Crucially, the content script must run in the context of the extension, NOT the current webpage (in the active tab.) 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 domain (the extension domain, i.e. chrome://....)

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

Into that iframe is loaded a page from the extension, let's call it iframe.html.

In that page, there's JavaScript which requests media access.

Phew...



来源:https://stackoverflow.com/questions/50991321/chrome-extension-getusermedia-throws-notallowederror-failed-due-to-shutdown

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