Reputation: 14998
I am working on Chrome Extension that opens link on clicking of extension Icon. Manifest is given below.
Issue is, once data is fetched and open links, it does not do again on same page.
{
"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()});
}
});
Upvotes: 1
Views: 233
Reputation: 10897
You forget to clear l
, then the next time you click browser action
, it remains the value of lnks.length
.
Set l = 0
when clearInterval
will solve your problem.
Upvotes: 1