Chrome Extension: Background js cannot receive message from Popup js occasionally
<p>There is a button on the popup. When clicked, popup.js will send a message to background.js. It works well almost all the time. But after a long time not operating chrome, when I click the button, the message is sent(alert("Start to send message") executed) but cannot be received by background.js(<strong>alert("Start to render") not executed</strong>). When I click one more, it will sometimes work. If it still doesn't work, I will click again, the third time has much more chance to success. That's very strange.</p>

<p>I use jquery in the popup.html, but I don't think it matter.
Javascript code in popup.js</p>

<pre><code>$('#create).html(chrome.i18n.getMessage('create'))
.on('click', function(){
    _current = null;
    chrome.runtime.sendMessage({
        cmd: 'SITEMAP_DEFINE'
    });
    alert("Start to send message");
    window.close();
</code></pre>

<p>})</p>

<p>Javascript code in background.js</p>

<pre><code>var curTabId; //current tab in which scraper is going to scraping
var devtoolPort;
var coord;
var sitemap;
var finish = false;


function sendMessage(msg) {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, msg);
  });
}

//detective devtools
chrome.runtime.onConnect.addListener(function (port) {
    if (port.name == "devtools-papa") {
        devtoolPort = port;
    }
});

//tab update
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(finish === false &amp;&amp; coord &amp;&amp; changeInfo.status == 'loading'){
        coord.onUpdate(tab);
    }
})
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
    if(finish === false &amp;&amp; coord &amp;&amp; removeInfo.isWindowClosing === false){
        coord.onRemove(tabId);
    }
})
chrome.runtime.onMessage.addListener(function(msg, sender, callback){
    var p = msg.params;

    //scrape
    if(msg.cmd == 'START_SCRAPE'){
        sitemap = p.sitemap;
        if(sitemap){
            finish = false;
            coord = new Coordinator(sitemap, 0);
            var page = coord.sitemap.pages[0];
            coord.openTab(page.id, page.link);
        }
    }
    else if(msg.cmd == 'TEST_SCRAPE'){
        sitemap = p.sitemap;
        if(sitemap){
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                var tab = tabs[0];
                sitemap.pages[0].link = tab.url;
                finish = false;
                coord = new Coordinator(sitemap, 20);
                var page = coord.sitemap.pages[0];
                coord.openTab(page.id, page.link);
            })
        }
    }
    else if(msg.cmd == 'SCRAPE_READY'){
        print(sender);
        coord.readyForScrape(sender.tab.id);
    }
    else if(msg.cmd == 'TAB_CLICK'){
        coord.currentPage = sitemap.getPage(p.page);
    }

    //define
    else if(msg.cmd == 'START_SELECT'){
        if(msg.tabId){
            chrome.tabs.sendMessage(msg.tabId, msg);
        }else{
            sendMessage(msg);
        }
    }
    else if(msg.cmd == 'END_SELECT'){
        if(devtoolPort){
            devtoolPort.postMessage(msg);
        }
        sendMessage(msg);
    }
    else if(msg.cmd == 'SITEMAP_DEFINE'){
        alert("Start to render");
        var url = p &amp;&amp; p.url;
        sitemap = p &amp;&amp; p.sitemap;
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            var tab = tabs[0];
            if(url &amp;&amp; url != tab.url){
                chrome.tabs.create({url: url}, function(newTab){
                    doInjectScript(newTab.id);
                })
            }else{
                injectScript(tab.id);
            }
        });
    }
    else if(msg.cmd == 'SITEMAP_DEFINE_END'){
        var _sm = p.sitemap;
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            var tab = tabs[0];
            _sm.pages[0].link = tab.url;
            saveSitemap(_sm);
        })
    }
    else if(msg.cmd == 'SITEMAP_DELETE'){
        deleteSitemap(p.sitemap);
    }
    else if(msg.cmd == 'DEFINE_PAGE_LOADED'){
        callback(sitemap);
    }
    else{
        sendMessage(msg);
    }
});

var print = function(msg){
    if(curTabId){
        chrome.tabs.sendMessage(curTabId, {
            cmd: 'LOG',
            params: msg
        });
    }else{
        sendMessage(msg)
    }
}

function saveSitemap(_sm){
    var id = _sm.id;
    chrome.storage.local.get("sitemaps", function(db){
        var _sitemaps = db["sitemaps"];
        if(!_sitemaps || !_sitemaps.length){
            _sitemaps = [_sm];
        }else{
            var _idx = _sitemaps.findIndex(function(s){return s.id == id});
            if(_idx == -1){
                _sitemaps.push(_sm);
            }else{
                _sitemaps.splice(_idx, 1, _sm);
            }
        }
        var _db = {'sitemaps':_sitemaps}
        chrome.storage.local.set(_db, function(){
            alert(chrome.i18n.getMessage('saved'));
        });
    })
}

function deleteSitemap(id){
    chrome.storage.local.get("sitemaps", function(db){
        var _sitemaps = db["sitemaps"];
        var index = _sitemaps.findIndex(function(s){return s.id == id})
        _sitemaps.splice(index, 1);
        var _db = {'sitemaps':_sitemaps}
        chrome.storage.local.set(_db, function(){
            alert(chrome.i18n.getMessage('deleted'));
        });
    })
}

function injectScript(tabId){
    var flag;
    chrome.tabs.sendMessage(tabId, {
        cmd: 'SELF_CHECK'
    }, function(r){
        flag = r;
    });
    setTimeout(function(){
        if(flag){
            chrome.tabs.executeScript(tabId, {file: "injected/define/initUI.js"}, function() {});
        }else{
            doInjectScript(tabId);
        }
    }, 200)
}

//Injected scripts will be removed once the tab reload again.
function doInjectScript(tabId){
    chrome.tabs.executeScript(tabId, {file: "injected/listener.js"});
    chrome.tabs.executeScript(tabId, {file: "lib/jquery/jquery-2.1.1.min.js"}, function() {
        chrome.tabs.executeScript(tabId, {file: "lib/model.js"});
        chrome.tabs.executeScript(tabId, {file: "lib/css-selector-generator.js"}, function() {});
        chrome.tabs.executeScript(tabId, {file: "injected/SelectTool.js"}, function() {});
        chrome.tabs.executeScript(tabId, {file: "injected/define/initUI.js"}, function() {});
        chrome.tabs.insertCSS(tabId, {file: "injected/injected.css", runAt: "document_end"});
    });
}


function createResultTab(title, fields, data){
    chrome.tabs.create({url: chrome.extension.getURL('result/table.html')}, function(newTab){
        var tabId = newTab.id;
        setTimeout(function(){
            chrome.tabs.sendMessage(tabId, {
                cmd: 'RESULT',
                params:{
                    data: data,
                    title: title,
                    fields: fields
                }
            });
        }, 200)
    })
}
</code></pre>

<p>Thanks in advance for your help.</p>