背景脚本chrome.tabs在chrome扩展中是未定义的?

时间:2017-07-04 15:24:26

标签: google-chrome google-chrome-extension

我试过了:

background.js

time_in_minutes = []

for t in data['Time']:
    minutes = 0
    # t = '00:00:51'
    h_m_s = t.split(':')
    # h_m_s = ['00', '00', '51']
    s_m_h = list(enumerate(reversed(h_m_s), -1))
    # s_m_h = [(-1, '51'), (0, '00'), (1, '00')]
    for i, x in s_m_h:
        # i = -1
        # x = '51'
        minutes += int(x) * 60 ** i
        # minutes = 0.85
    time_in_minutes.append(minutes)

foo.js:

chrome.browserAction.onClicked.addListener(function (activeTab) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.farewell);
      });
    });
});

清单:

chrome.runtime.onMessage.addListener(

  function (request, sender, sendResponse) {
    new contentFunction()

    console.log(sender.tab ?
      "from a content script:" + sender.tab.url :
      "from the extension");
    if (request.greeting == "hello") {
      sendResponse({ farewell: "goodbye" });
    }
  });

enter image description here

0 个答案:

没有答案