谷歌浏览器 - 将变量传递给扩展程序

时间:2017-06-03 16:32:44

标签: javascript google-chrome google-chrome-extension

目前我正在尝试将一个数组从context_script传递到我的popup.html,只需点击一下按钮即可。然而,我自己似乎无法解决一些并发症,几乎SO和其他网站中的每一个实现方法似乎都没有解决。

我真正需要的只是将上下文中的单个列表传递到处理扩展HTML界面的JavaScript页面。

background.js

"use strict";
function Master() {
    firebase.initializeApp(config);

    this.time = 0;

    this.http_usage = this.get_http();
    this.https_usage = this.get_https();
    this.todays_usage = this.get_today_sites();

    chrome.runtime.onMessage.addListener(function(message,sender,sendResponce) {
        sendResponce("Hello world");
    });

    this.init();
}

的manifest.json

{
  "name": "Statistics",
  "description": "Saves time and visitation data.",
  "version": "1.0",
  "manifest_version": 2,

  "permissions": [
    "activeTab",
    "tabs",
    "http://*/*",
    "https://*/*",
    "clipboardRead",
    "clipboardWrite",
    "background"
  ],
  "content_scripts": [
    {
      "matches": [
        "*://*/*"
      ],
      "js": [
        "config.js",
        "firebase.js",
        "background.js"
      ],
      "css": [
        "styles.css"
      ]
    }
  ],
  "browser_action": {
    "default_title": "Statistics",
    "default_popup": "popup.html"
  }
}

popup.js

function get_data(){
    let data = []
    chrome.runtime.sendMessage({data:"giblist"}, function (responce) {
    console.log(responce);
    ...
});

}

document.addEventListener('DOMContentLoaded', function () {
    console.log("Loaded page");
    firebase.initializeApp(config);
    document.getElementById("showStatistics").addEventListener("click", get_data);
});

0 个答案:

没有答案