2

我正在尝试chrome.desktopCapture.chooseDesktopMedia使用 chrome 扩展程序,并且可以很好地获得桌面流。

我正在使用以下内容将流转换为blob:后台脚本中的 -URL,如下所示:

var objectUrl = URL.createObjectURL(stream);

我似乎无法解决的是如何将其设置为注入页面上视频元素的 src 属性。

我尝试了以下方法,但每种方法都不起作用:

在 Background.js 中:

var video     = document.getElementById("video");
var objectUrl = URL.createObjectURL(stream);
video.src     = objectUrl;

在 Content.js 中

//objectUrl is a string received in a message from the background page by the content page
var video     = document.getElementById("video");
video.src     = objectUrl;

我在 javascript 控制台中得到以下信息:

不允许加载本地资源:blob:chrome-extension://panahgiakgfjeioddhenaabbacfmkclm/48ff3e53-ff6a-4bee-a1dd-1b8844591a91

如果我在一条消息中将 URL 一直发布到注入的页面,我也会得到相同的结果。这应该工作吗?我真的很感激这里的任何建议。

在我的清单中我也有 "web_accessible_resources": [ "*" ],但这只是为了看看它是否解决了这个问题(它没有)。

4

2 回答 2

5

在内容脚本中,DOM 是与页面共享的,因此任何 DOM 操作(例如设置视频src)都受页面同源策略的约束,而不是扩展。

如果要显示选项卡的内容,则必须将tab.Tab对象传递给chrome.desktopCapture.chooseDesktopMedia. 可以通过多种方式获取此对象,包括消息传递选项卡API。这是使用扩展按钮的示例:

背景.js

chrome.browserAction.onClicked.addListener(function(tab) {
    // NOTE: If you want to use the media stream in an iframe on an origin
    // different from the top-level frame (e.g. http://example.com), set
    //  tab.url = 'http://example.com'; before calling chooseDesktopMedia!
    //  (setting tab.url only works in Chrome 40+)
    chrome.desktopCapture.chooseDesktopMedia([
        'screen', 'window'//, 'tab'
    ], tab, function(streamId) {
        if (chrome.runtime.lastError) {
            alert('Failed to get desktop media: ' +
                chrome.runtime.lastError.message);
            return;
        }   

        // I am using inline code just to have a self-contained example.
        // You can put the following code in a separate file and pass
        // the stream ID to the extension via message passing if wanted.
        var code = '(' + function(streamId) {
            navigator.webkitGetUserMedia({
                audio: false,
                video: {
                    mandatory: {
                        chromeMediaSource: 'desktop',
                        chromeMediaSourceId: streamId
                    }   
                }   
            }, function onSuccess(stream) {
                var url = URL.createObjectURL(stream);
                var vid = document.createElement('video');
                vid.src = url;
                document.body.appendChild(vid);
            }, function onError() {
                alert('Failed to get user media.');
            }); 
        } + ')(' + JSON.stringify(streamId) + ')';

        chrome.tabs.executeScript(tab.id, {
            code: code
        }, function() {
            if (chrome.runtime.lastError) {
                alert('Failed to execute script: ' +
                    chrome.runtime.lastError.message);
            }   
        }); 
    }); 
});

清单.json

{
    "name": "desktopCapture.chooseDesktopMedia for a tab",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"]
    },
    "browser_action": {
        "default_title": "Show desktop capture request"
    },
    "permissions": [
        "desktopCapture",
        "activeTab"
    ]
}
于 2014-07-15T20:58:22.743 回答
0

ObjectURL 不能跨域共享。如果数据 URL 适用于您的视频流,则可以跨域共享数据 URL(我不确定)。

于 2014-07-15T18:59:49.447 回答