我想实现一个屏幕共享web应用程序,它将使用desktopCapture Chrome API在网页上显示用户屏幕。我创建了Chrome扩展,并在后台运行了一个事件监听器。我的问题是,当我尝试从网页发送消息到扩展(获取userMedia id)我没有收到任何扩展端。我也试图将getUserMedia id返回到网页以显示提要。我附上了我所拥有的。由于发送消息到后台脚本
清单
{
"name": "Class Mate Manifest",
"description": "Extension that allows for user to share their screen",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": [
"desktopCapture",
"tabs"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "index.html"
}
}
background.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(request.greeting);
if(request.greeting == yes){
chrome.desktopCapture.chooseDesktopMedia(["screen", "window"], sendResponse);
return true;
}
});
webpage.js
function gotStream(stream) {
console.log("Received local stream");
var video = document.querySelector("video");
video.src = URL.createObjectURL(stream);
localstream = stream;
// stream.onended = function() { console.log("Ended"); };
}
function getUserMediaError() {
console.log("getUserMedia() failed.");
}
function onAccessApproved(id) {
console.log(id);
if (!id) {
console.log("Access rejected.");
return;
}
navigator.webkitGetUserMedia({
audio:false,
video: { mandatory: { chromeMediaSource: "desktop", chromeMediaSourceId: id } }
}, gotStream, getUserMediaError);
}
chrome.runtime.sendMessage({greeting: "yes"}, onAccessApproved);
是的你是对的。我能够使用您提到的外部连接方法从我的网页发送消息。但是现在一旦我发送消息,扩展就会正确接收消息,但不会通过回调函数发回任何消息。所以我的Chrome扩展是假设发回一个ID用于共享屏幕。但在网页上,回调方法甚至没有运行。有什么想法吗? – bigC5012 2014-09-30 02:35:31
所以我修正了我之前评论中的错误。现在,网页成功地向扩展程序发送消息。该扩展接收该消息并询问用户他们希望捕获什么屏幕。然后,background.js文件将正确的ID发送到网页。一旦我的网页在AccessAproved上运行,它无法加载视频流并运行getUserMediaError函数。我在控制台上输出错误信息,并说NavigateUserMediaError。我不知道这个错误意味着什么或者我的代码出了什么问题? – bigC5012 2014-09-30 21:37:15
我会提出一个新的问题。 – Xan 2014-09-30 21:39:20