Chrome扩展程序:browserAction单击按钮工作一次

时间:2016-04-24 14:19:58

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

我正在使用Chrome扩展程序,点击扩展程序图标时会打开链接。清单如下。

问题是,一旦获取数据并打开链接,它就不会在同一页面上再次执行。

{
    "manifest_version": 2,
    "name" : "xxx",
    "description" : "Open links",
    "version" : "1.1",
    "background" : {
        "scripts" : ["jquery.js","background.js"]
    },
    "content_scripts" : [{
        "matches" : ["*://xxx/*"],
        "js" : ["jquery.js","script.js"],
        "run_at": "document_end"
    }],
"icons":
    {
    "16": "icon16.png",
    "48": "icon48.png"
    },
    "web_accessible_resources": [
        "script.js",
        "jquery.js",
        "background.js"
    ],
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
    "browser_action": {
        "default_title": "xxx",
        "default_icon": "icon16.png"
      }
}

background.js

var l = 0;
var lnks = null;
chrome.browserAction.onClicked.addListener(function (tab)
{
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
    {
        chrome.tabs.sendMessage(tab.id, {method: "sendHTML"}, function(response)
            {

                var val = null;
                if(response.data != null) {
                    lnks = parse_links(response.data);
                    val = setInterval(function(){
                        var total = lnks.length;
                        if (l < lnks.length) {
                            console.log(lnks[l]);
                            chrome.tabs.create({ url: lnks[l] });
                            l++;
                        } else {
                            clearInterval(val);
                        }


                    }, 5000);

                }

            }
        );
    });

});

function openTab(url) {
    l++;
}

function parse_links(body) {
    var jq = $(body);
    var tble = jq.find("table");
    var legit_links = [];
    var link = null;

    jq.find('table a').each(function() {
        //val = setInterval(openTab,4000,lnks[i]);
        link = $(this).attr('href').trim();
        if(link.indexOf("?accountid") !== -1) {
            $(this).click();
            legit_links.push(link)

        }
    });

    return legit_links;

}

的script.js

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
//    alert('Message from View:\n'
//          + JSON.stringify(msg));
    if (msg.method === 'sendHTML')
    {
        sendResponse({ data:$("body").html()});
    }
});

1 个答案:

答案 0 :(得分:1)

您忘了清除browser action,然后在您下次点击lnks.length时,它仍然是l = 0的值。

clearInterval解决您的问题时设置viewHolder.address.setText(row[4]);//Read from CSV file