代码之家  ›  专栏  ›  技术社区  ›  Ratan Kumar

Chrome扩展后台脚本不工作

  •  1
  • Ratan Kumar  · 技术社区  · 11 年前

    AIM:当打开任何新的选项卡时,扩展会向服务器发出带有url的请求,并获得响应并更改图标颜色。

    背景.js:

    chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) 
     {
        url = "http://localhost/test.php?"+ $.param({"url":tab.url});
        $.get(url, function(responseText) {
        console.log("sent data");
      });
    });
    

    manifest.json:

     ..."background": { "scripts": ["background.js"] ,"persistent": false },
     "permissions": ["tabs","http://localhost/", "http://*/*"],....
    

    这行不通。

    但当与扩展页上的按钮绑定为:

    function send_url(){
      chrome.tabs.getSelected(null,function(tab){
      url = "http://localhost/test.php?"+ $.param({"url":tab.url});
      $.get(url, function(responseText) {
      console.log("url sent ");
      });
     });
    }
    

    这会将url发送到我的本地服务器! background.js缺少什么吗

    1 回复  |  直到 11 年前
        1
  •  2
  •   Ratan Kumar    11 年前

    这就是我想要的:

     "background": { "scripts": ["assets/js/jquery.min.js","background.js"] ,"persistent": false },