【问题标题】:Sending message to background script向后台脚本发送消息
【发布时间】:2014-11-23 07:04:39
【问题描述】:

我正在尝试实现一个屏幕共享网络应用程序,该应用程序将使用 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;
 }
 });

网页.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);

【问题讨论】:

    标签: javascript google-chrome web google-chrome-extension video-capture


    【解决方案1】:

    您不能像在任意网页代码的内容脚本中使用消息一样简单地使用消息传递。

    文档中有两个与网页通信的指南,分别对应两种方式:(externally_connectable)(custom events with a content script)

    假设您想允许http://example.com 向您的分机发送消息。

    1. 您需要在清单中专门将该网站列入白名单:

        "externally_connectable" : {
          matches: [ "http://example.com" ]
        },
      
    2. 您需要obtain a permanent extension ID。假设生成的 ID 是abcdefghijklmnoabcdefhijklmnoabc

    3. 您的网页需要检查是否允许发送消息,然后使用预定义的 ID 发送消息:

      // Website code
      // This will only be true if some extension allowed the page to connect
      if(chrome && chrome.runtime && chrome.runtime.sendMessage) {
        chrome.runtime.sendMessage(
          "abcdefghijklmnoabcdefhijklmnoabc",
          {greeting: "yes"},
          onAccessApproved
        );
      }
      
    4. 您的扩展程序需要收听外部消息并且可能还要检查它们的来源:

      // Extension's background code
      chrome.runtime.onMessageExternal.addListener(
        function(request, sender, sendResponse) {
          if(!validate(request.sender)) // Check the URL with a custom function
            return;
          /* do work */
        }
      );
      

    【讨论】:

    • 是的,你是对的。我能够使用您提到的外部连接方法从我的网页发送消息。但是现在,一旦我发送消息,扩展程序会正确接收消息,但不会通过回调函数发回任何内容。所以我的 chrome 扩展是假设发回一个 id 用于共享屏幕。但是在网页端,甚至没有运行回调方法。有什么想法吗?
    • 所以我在之前的评论中修正了我的错误。现在网页成功地向扩展发送了一条消息。扩展程序接收消息并询问用户他们希望捕获的屏幕。然后 background.js 文件将正确的 id 发送到网页。一旦我的网页在 AccessAproved 上运行,它就无法加载视频流并运行 getUserMediaError 函数。我在控制台上打印出错误,它显示 NavigateUserMediaError。我不知道这个错误是什么意思或者我的代码有什么问题?
    • 我会提出一个新问题。
    • 你是什么意思? 1)第4点:这是你通常放在manifest.json > background.js > here的代码吗? 2)第3点:这是您在http://example.com/index.htmlhttps://example.com/index.html中输入的代码吗?
    • @YumYumYum 我希望我现在澄清了。
    猜你喜欢
    • 2013-06-27
    • 2015-05-03
    • 2014-07-16
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2020-10-12
    相关资源
    最近更新 更多