Chrome扩展程序有时无法加载后台脚本

时间:2015-06-18 18:09:44

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

我遇到了Chrome扩展程序的问题,但有时只是。

这是我的简单清单文件:

{
  "manifest_version": 2,

  "name": "WorkPuls",
  "description": "Test extension",
  "version": "1.1",
  "icons": {  "16": "icon16.png",
              "48": "icon48.png",
              "128": "icon128.png" },
  "permissions": [
    "tabs",
    "activeTab"
  ],
  "background": {
    "scripts": ["test.js"]
  }
}

大多数情况下test.js已正确加载,但有时会将其作为空白文件加载。有没有人对此提出建议?

Test.js:

function sendActiveTabUrl(url){
    var connection = new WebSocket('ws://127.0.0.1:13374');
    connection.onopen = function(){
        connection.send(url);
        connection.close();
    }
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if( changeInfo.status == "complete")
        chrome.tabs.getSelected(null, function(tab) {
            sendActiveTabUrl(tab.url);
        });
});

chrome.tabs.onActivated.addListener(function(info) {
    var tab = chrome.tabs.get(info.tabId, function(tab) {
        sendActiveTabUrl(tab.url);
    });
});

chrome.windows.onFocusChanged.addListener(function(windowId) {
    if(windowId == -1){
        return;
    }
    chrome.tabs.getSelected(windowId, function(tab){
        sendActiveTabUrl(tab.url);
    });
});

setInterval(function(){
    chrome.windows.getAll({'populate':true}, function(windows){
        for(var i in windows){
            if(windows[i].focused){
                chrome.tabs.getSelected(windows[i].id, function(tab){
                    sendActiveTabUrl(tab.url);
                });
            }
        }
    })
},1500);

0 个答案:

没有答案