谷歌浏览器扩展程序 - 消息+调用弹出页面和内容脚本之间的功能 [英] Google Chrome Extension- Messages + call to function between popup page and content script

查看:714
本文介绍了谷歌浏览器扩展程序 - 消息+调用弹出页面和内容脚本之间的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌浏览器扩展程序中,我想在内容脚本和弹出页面之间传递消息。我还希望在内容脚本中的某个功能被弹出页面发送到内容脚本时触发。



Google Chrome扩展程序中的这一部分针对消息的开发人员文档指出,消息可以从内容脚本传递到扩展页面,反之亦然(引自 http://code.google.com/chrome/extensions/messaging.html



这是否意味着我可以在内容脚本和弹出页面之间发送/接收消息?因为我看到的用法是用于后台页面和内容脚本之间的通信。



或者我必须设置一个3路消息传递路径 - 即,
内容脚本< - >背景页面>弹出页面。如果是这种情况,我该如何在背景页面< - >弹出页面之间设置消息传递?



我如何在内容脚本中激活一个函数,从弹出页面发送信号到内容脚本后?这还需要后台脚本吗?

解决方案

内容脚本只能发送消息到后台页面。如果您想从内容脚本向弹出窗口发送消息,您必须:


  1. 从内容发送消息脚本到后台页面。

      chrome.runtime.sendMessage(... message ...,function(){/ *响应* /}); 


  2. 在后台接收邮件。

      chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){...}); 


  3. 将消息传递给弹出窗口。要获取对弹出窗口的全局窗口对象的引用,请使用 chrome.extension.getViews 。注意:与大多数其他chrome API不同,此方法是同步的:

      var popupWindows = chrome.extension.getViews({type :'弹出'}); 
    if(popupWindows.length){//找到一个弹出窗口
    // details是来自onMessage事件的对象(参见2)
    popupWindows [0] .whatever(message,sendResponse );
    }


  4. 在弹出窗口中,定义 方法。

     函数whatever(message,sendResponse){
    //做某事,例如,发回消息
    sendResponse(消息);
    }


  5. sendResponse 4)被调用, details.sendResponse (见3)被调用。这反过来,从1调用 function(){/ * response * /}


    注意:Chrome 26+仅支持 chrome.runtime.sendMessage / onMessage 。使用 chrome.extension.sendMessage 也可以支持旧版浏览器。


    In a Google Chrome extension I want to pass messages between a content script and a popup page. I also want a function in the content script to be fired when a signal to do this is sent by the popup page to the content script.

    The section in Google Chrome Extensions developer docs for messages, states that messages can be passed "from your content script to an extension page, or vice versa" (quoted from http://code.google.com/chrome/extensions/messaging.html )

    Does this mean that I can send/receive messages between the content script and the popup page? Because the usage I have seen is for communication between background page and content script.

    Or do I have to set up a 3 - way message passing route-- viz. content script <--> background page <--> popup page. And if this is the case, how do I set up messaging between background page <--> popup page?

    And how do I fire a function in the content script, after sending signal to content script from the popup page? Does this also require background script to be present?

    解决方案

    Content scripts can only send a message to the background page. If you want to send a message from a content script to a popup, you have to:

    1. Send the message from the Content script to the background page.

      chrome.runtime.sendMessage( ...message... , function() { /*response*/ });
      

    2. Receive the message at the background.

      chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { ... });
      

    3. Pass the message to the popup. To get a reference to the global window object of the popup, use chrome.extension.getViews. Note: Unlike most of the other chrome APIs, this method is synchronous:

      var popupWindows = chrome.extension.getViews({type:'popup'});
      if (popupWindows.length) { // A popup has been found
          // details is the object from the onMessage event (see 2)
          popupWindows[0].whatever(message, sendResponse);
      }
      

    4. In the popup, define the whatever method.

      function whatever(message, sendResponse) {
          // Do something, for example, sending the message back
          sendResponse(message);
      }
      

    5. When sendResponse (4) is called, details.sendResponse (see 3) is called. This, in turn, calls function() { /*response*/ } from 1.

    Note: chrome.runtime.sendMessage/onMessage is only supported in Chrome 26+. Use chrome.extension.sendMessage if you want to support older browsers as well.

    这篇关于谷歌浏览器扩展程序 - 消息+调用弹出页面和内容脚本之间的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆