谷歌浏览器扩展程序 - 消息+调用弹出页面和内容脚本之间的功能 [英] Google Chrome Extension- Messages + call to function between popup page and content script
问题描述
在谷歌浏览器扩展程序中,我想在内容脚本和弹出页面之间传递消息。我还希望在内容脚本中的某个功能被弹出页面发送到内容脚本时触发。
Google Chrome扩展程序中的这一部分针对消息的开发人员文档指出,消息可以从内容脚本传递到扩展页面,反之亦然(引自 http://code.google.com/chrome/extensions/messaging.html )
这是否意味着我可以在内容脚本和弹出页面之间发送/接收消息?因为我看到的用法是用于后台页面和内容脚本之间的通信。
或者我必须设置一个3路消息传递路径 - 即,
内容脚本< - >背景页面>弹出页面。如果是这种情况,我该如何在背景页面< - >弹出页面之间设置消息传递?
我如何在内容脚本中激活一个函数,从弹出页面发送信号到内容脚本后?这还需要后台脚本吗?
解决方案内容脚本只能发送消息到后台页面。如果您想从内容脚本向弹出窗口发送消息,您必须:
-
从内容发送消息脚本到后台页面。
chrome.runtime.sendMessage(... message ...,function(){/ *响应* /});
-
在后台接收邮件。
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){...});
-
将消息传递给弹出窗口。要获取对弹出窗口的全局
窗口
对象的引用,请使用chrome.extension.getViews
。注意:与大多数其他chrome API不同,此方法是同步的:var popupWindows = chrome.extension.getViews({type :'弹出'});
if(popupWindows.length){//找到一个弹出窗口
// details是来自onMessage事件的对象(参见2)
popupWindows [0] .whatever(message,sendResponse );
}
-
在弹出窗口中,定义
函数whatever(message,sendResponse){
//做某事,例如,发回消息
sendResponse(消息);
}
- 当
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:
Send the message from the Content script to the background page.
chrome.runtime.sendMessage( ...message... , function() { /*response*/ });
Receive the message at the background.
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { ... });
Pass the message to the popup. To get a reference to the global
window
object of the popup, usechrome.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); }
In the popup, define the
whatever
method.function whatever(message, sendResponse) { // Do something, for example, sending the message back sendResponse(message); }
- When
sendResponse
(4) is called,details.sendResponse
(see 3) is called. This, in turn, callsfunction() { /*response*/ }
from 1.
Note:
chrome.runtime.sendMessage
/onMessage
is only supported in Chrome 26+. Usechrome.extension.sendMessage
if you want to support older browsers as well.这篇关于谷歌浏览器扩展程序 - 消息+调用弹出页面和内容脚本之间的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!