简体   繁体   English

网页访问Chrome扩展程序并使用desktopCature

[英]Webpage accessing Chrome Extension and using desktopCature

I am implementing a webpage that simply has a video player that needs to play the stream of the users selected screen. 我正在实现一个仅包含视频播放器的网页,该播放器需要播放用户选择的屏幕流。 So far I have implemented the chrome extension to run a Listener on the background.js file. 到目前为止,我已经实现了chrome扩展,以便在background.js文件上运行侦听器。 Then on the webpage.js I send a message to the chrome extension in which the extension then runs the chooseDestopMedia method and allows the user to select which screen to share. 然后在webpage.js上,我向chrome扩展发送一条消息,在该扩展中,该扩展随后运行chooseDestopMedia方法,并允许用户选择要共享的屏幕。 The background.js then returns the corresponding ID to webpage.js via the callback method. 然后background.js通过回调方法将相应的ID返回给webpage.js。 The ID is received properly on webpage.js, but once the webpage.js runs the gotStream method it throws an error. 该ID已在webpage.js上正确接收,但是一旦webpage.js运行gotStream方法,它将引发错误。 So far some reason there is a problem receiving the video stream and I am not sure why. 到目前为止,某些原因导致视频流接收出现问题,我不确定为什么。 The error that is printed on the console is "NavigatorUserMediaError". 控制台上打印的错误是“ NavigatorUserMediaError”。 I have attached the code below... 我已附上以下代码...

manifest.json 的manifest.json

{
"name": "Class Mate Manifest",
"description": "Extension that allows for user to share their screen",
"version": "1",
"manifest_version": 2,

"externally_connectable": {
"matches": [ "https://localhost/PresenterPage/presenterpage.html" ]
},
"background": {
  "scripts": ["background.js"]
},
"permissions": [
"desktopCapture",
"tabs",
"https://localhost/PresenterPage/presenterpage.html"
],
"browser_action": {
  "default_icon": "icon.png",
"default_popup": "index.html"
    }
}

webpage.js 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(err) {
console.log("getUserMedia() failed." + err);
}

function onAccessApproved(id) {
if (!id) {
console.log("Access rejected.");
return;
}


navigator.webkitGetUserMedia({
  audio:false,
  video:
  { 
    mandatory: 
    {
        chromeMediaSource: 'desktop', 
        chromeMediaSourceId: id 
    }
  }
}, gotStream, getUserMediaError);

}
var editorExtensionId = "phdabcobbnbidjflhchajebbldjblmjf";
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: "yes"},onAccessApproved);

background.js background.js

chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
console.log(sender.tab);
chrome.desktopCapture.chooseDesktopMedia(
  ["screen", "window"],
  sender.tab,
  function(id) {
  console.log(id);
    sendResponse({"id": id});
    console.log(id);
  } 
  );
  return true;
}
);

If you had read the documentation , you would know that in order for a tab to access the media stream, you need to specify which tab you want to give access to when calling chooseDesktopMedia. 如果您已阅读该文档 ,您将知道为了使选项卡访问媒体流,您需要指定在调用choiceDesktopMedia时要授予访问权限的选项卡。

And please for the love of all that is good make sure you validate which tab is asking to see the user's desktop before you let it do that. 并请您热爱所有美好的事物,请确保您先确认哪个选项卡要求查看用户的桌面,然后再执行此操作。

Part 2 : 第2部分

The reason your new code doesn't work is that all you're doing with the tabs returned by tabs.query is logging then. 您的新代码无法正常工作的原因是,您对tabs.query返回的选项卡所做的全部工作都在进行日志记录。 Logging something does not magically make it go into the place in the code where it's supposed to go. 记录某些内容并不能使它神奇地进入代码应有的位置。

You need to pass the tab received by the callback to chooseDesktopMedia yourself. 您需要将回调收到的选项卡传递给自己,以便选择DesktopMedia。 In practice this means moving most of your code inside the callback (or a separate function). 实际上,这意味着将大多数代码移到回调(或单独的函数)中。

Plus you need to validate the tab properly for security reasons, I hope you left that out for brevity. 另外,出于安全原因,您需要正确验证选项卡,希望您简洁起见。

I was able to solve this problem by using a content script. 通过使用内容脚本,我能够解决此问题。 The communication between the website, extension, and content-script is much smoother. 网站,扩展名和内容脚本之间的通信更加顺畅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM