chrome extensions 中的交互

background与content_script的交互

  其所指为当前网页与扩展插件之间的交互。使用chrome.* API进行通信。

  background向content_script发送消息

    background.js

     1 chrome.tabs.sendMessage(tabId,{greeting:'hello'}); 

    content_script.js

    

1 chrome.extension.onMessage.addListener(
2     function(request, sender, sendResponse) {
3         console.log(request.greeting);
4         }
5 );

    附:tabId的获取方法

      background.js

      

1 chrome.tabs.onUpdated.addListener(
2     function(tabId, changeInfo,tab) {
3         console.log(tabId);
4     }
5 );  

  content_script向background发送消息

    content_script.js

    

1 chrome.extension.sendMessage({greeting:'hello'});

    background.js

    

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log(request.greeting);
        }
);

   区别:前者向tabs发送,后者向extension发送。

 

background与popup的交互

  其所指为扩展前后台的交互。

  popup.html中不能附加任何javascript代码,包括不限于onclick。应在popup.js中进行定义。

  popup获取background值

    bcakground.js

    

1 var greeting='hello';

    popup.js

    

console.log(chrome.extension.getBackgroundPage().greeting);

  background操作popup

    使用getViews,未成功。

posted on 2014-05-19 11:39  ORLAN 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/experiments-of-ORLAN/p/3736511.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值