Chrome扩展程序 后台js与前台js数据交互

1.后台代码:

sendMessage

    chrome.extension.sendMessage(string extensionId, any message, function responseCallback)

    向扩展内的其它监听者发送一条消息。与chrome.extension.connect类似,但仅发送单条消息(响应回调函数可选)。 此消息发送后会触发扩展内每个页面的chrome.extension.onMessage事件。

参数

    extensionId optional string )您想发送消息的目标扩展的ID。若忽略,默认为本函数调用者所在的扩展。
    message any )
    responseCallback optional function )
参数
    response any )消息监听者返回的响应数据(格式是JSON)。若消息发送失败,回调函数也会被调用,响应数据为空,chrome.extension.lastError中记录错误信息描述。

回调

    回调参数应如下例所示:

    function(any response) {...};
    response any )消息监听者返回的响应数据(格式是JSON)。若消息发送失败,回调函数也会被调用,响应数据为空,chrome.extension.lastError中记录错误信息描述。
 
 
2.前台代码:
 

    chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
      console.log('收到消息' + request.msg);
    });

 

 

 

 

 

 

posted @ 2014-04-21 15:52  天外飞仙丶  阅读(1941)  评论(0编辑  收藏  举报