gpt4 book ai didi

google-chrome - 在选项卡中显示来自 chrome.desktopCapture.chooseDesktopMedia 的视频?

转载 作者:行者123 更新时间:2023-12-02 04:14:24 27 4
gpt4 key购买 nike

我正在 Chrome 扩展中尝试 chrome.desktopCapture.chooseDesktopMedia,我可以很好地获得桌面流。

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

var objectUrl = URL.createObjectURL(stream);

我似乎无法解决的是如何将其设置为注入(inject)页面上视频元素的 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 控制台中得到以下内容:

Not allowed to load local resource: blob:chrome-extension://panahgiakgfjeioddhenaabbacfmkclm/48ff3e53-ff6a-4bee-a1dd-1b8844591a91

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

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

最佳答案

在内容脚本中,DOM 与页面共享,因此任何 DOM 操作(例如设置视频 src)均受 same-origin policy页面的名称,而不是扩展名。

如果你想显示选项卡的内容,那么你必须传递 tab.Tab反对chrome.desktopCapture.chooseDesktopMedia 。该对象可以通过多种方式获取,包括message passingtabs蜜蜂。这是使用 extension button 的示例:

背景.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);
}
});
});
});

list .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"
]
}

关于google-chrome - 在选项卡中显示来自 chrome.desktopCapture.chooseDesktopMedia 的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24760955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com