zoukankan      html  css  js  c++  java
  • 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,未成功。

  • 相关阅读:
    python中如何将两个list合并成一个list,不用for语句
    python print的用法
    Pandas Timestamp 和 python 中 datetime 的互相转换
    python eval, exec. compile
    python 用 __all__ 暴露接口
    python functiontools 模块
    Python 修饰符, 装饰符
    Python 字典(Dictionary) update()方法
    Python :random 随机数生成
    Pandas dataframe 标记删除重复记录
  • 原文地址:https://www.cnblogs.com/experiments-of-ORLAN/p/3736511.html
Copyright © 2011-2022 走看看