选项卡侦听器未被后台脚本中的消息激活

时间:2016-05-18 18:26:49

标签: javascript google-chrome-extension

我正在向tab中发送一条消息,我从background.js获得了一个内容脚本(getTradingData.js),代码如下:

alert("Automated TradingView Extension is running");

chrome.tabs.query({
  url: 'https://www.tradingview.com/*'
}, function(tabs) {
  if (tabs.length == 1) {
    chrome.tabs.sendMessage(tabs[0].id, {subject: "testConnection"}, function(response) {
      alert(response); //THIS RETURNS UNDEFINED
      if (response.msg == "getTradingDataScriptHere") {
        alert("Script Already Injected. Do not reinject"); //THIS IS NOT RUNNING
      } else {
        chrome.tabs.executeScript(tabs[0].id, {file: "jquery-2.2.3.min.js"});
                chrome.tabs.executeScript(tabs[0].id, {file: "jquery.waituntilexists.min.js"});
                chrome.tabs.executeScript(tabs[0].id, {file: "getTradingData.js"});
                alert("Injected all Nessessary Scripts for Auto Trading View to work"); //THIS IS NOT RUNNING
      }
    });
  } else {
    alert("Please have one and only one tradingview chart page opened.");
  }
});

var price = "Waiting For Price"

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.subject == "getPrice") {
      sendResponse({
        price: price
      });
    } else if (request.from == "getTradingData" && request.subject == "scriptLoaded") {
        //getTradingData.js Script has Fully Loaded onto Website

    } else if (request.from == "getTradingData" && request.subject == "updatePrice") {
        price = request.price
    }
  });

但是响应返回为undefined。所以基本上我没有得到回复。

以下是我在getTradingData.js中应该响应消息的内容:

 alert("getTradingData.js is Running");

//Send message to let the extension know the script has been injected on site
chrome.runtime.sendMessage({
    from:    'getTradingData',
    subject: 'scriptLoaded'
});

chrome.runtime.onConnect.addListener(function(port) { //THIS DOESN'T WORK EITHER
  console.assert(port.name == "tradingdata");
  port.onMessage.addListener(function(request) {
    if (request.msg == "Knock knock")
      port.postMessage({subject: "price"});
    else if (msg.answer == "Madame")
      port.postMessage({question: "Madame who?"});
    else if (msg.answer == "Madame... Bovary")
      port.postMessage({question: "I don't get it."});
  });
});

//to check if script already injected
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    alert("got message"); //THIS IS NOT RUNNING
    if (request.subject == "testConnection") {
        sendResponse({msg: "getTradingDataScriptHere"});
    }
 });

//wait till item has loaded
$(".dl-header-figures").waitUntilExists(function(){
    alert($(".dl-header-figures").text());
    updatePrice();
});

function updatePrice(){
    alert("updating price");
    chrome.runtime.sendMessage({
        from:    'getTradingData',
        subject: 'updatePrice',
        price: $(".dl-header-figures").text()
    });
}

//TODO: Use long lived connections for this to work: https://developer.chrome.com/extensions/messaging
// setInterval(updatePrice(), 3000);

然而,这永远不会被激活,我从来没有得到警告“得到消息”。

这是我的manifest.json的样子:

{
  "manifest_version": 2,

  "name": "Automated TradingView Strategy",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": ["jquery-2.2.3.min.js", "background.js"]
  },
  "content_scripts": [
    {
      "matches": ["https://www.tradingview.com/chart/*", "http://www.tradingview.com/*"],
      "js": ["jquery-2.2.3.min.js", "jquery.waituntilexists.min.js", "getTradingData.js"]
    }
  ],
  "permissions": [
    "activeTab",
    "tabs",
    "*://*.tradingview.com/*",
    "https://ajax.googleapis.com/"
  ]
}

我做错了什么?我怎样才能让它发回回复。即使我刷新扩展应该重新加载background.js而不重新加载已经注入了内容脚本的选项卡,因为没有激活监听器,我得不到任何响应。

1 个答案:

答案 0 :(得分:2)

你想在后台脚本中做什么?

加载扩展时,

chrome.tabs.query只运行一次,因为清单,您注入chrome.tabs.executeScript的脚本应该已经注入。

我不确切地知道您要做什么,但是,每次更新标签时都可以收听事件(标签在创建后更新) - {{1 }}

更新了background.js:

chrome.tabs.onUpdated.addListener