I'm learning to write JavaScript extensions, and chose a basic application to start with. The extension I'm writing simply adds the attribute target='_blank'
in each link. The extension gets activated on clicking the icon. It works perfectly, but I'm adding listeners to update, replace and activate events, and none of them is working. Already checked some SO posts but it didn't help (say this, this, and many more). Googled enough to reach saturation point and post a question. Please help.
Here is my code.
background.js
chrome.browserAction.onClicked.addListener(function(tab){
chrome.storage.local.get('click',function(result){
if(result.click == 0){
chrome.storage.local.set({"click":1},function(){
mainProc(tab.id,result.click);
});
} else if (result.click == 1) {
chrome.storage.local.set({"click":0},function(){
mainProc(tab.id,result.click);
});
} else if (result.click == undefined) {
chrome.storage.local.set({"click":1},function(){
mainProc(tab.id,result.click);
});
}
})
});
chrome.tabs.onActivated.addListener(function(activeInfo){
chrome.storage.local.get("click",function(result){
if(result.click == 1){
mainProc(activeInfo.tabId,result.click);
}
})
})
chrome.tabs.onUpdated.addListener(function(tabid,changeInfo,tab) {
if(changeInfo.status == "complete"){
chrome.storage.local.get("click",function(result){
if(result.click == 1){
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
mainProc(tabs[0].id,result.click);
});
}
})
}
})
chrome.tabs.onReplaced.addListener(function(addedTabId,removedTabId){
chrome.storage.local.get("click",function(result){
if(result.click == 1){
mainProc(addedTabId,result.click); //tried with removedTabId too, just to be sure
}
})
})
function changeIcon(clicked){
if(clicked == 1){
chrome.browserAction.setIcon({path: 'light.png'});
}else {
chrome.browserAction.setIcon({path: 'dark.png'});
}
}
function activate(id){
chrome.storage.local.get("click",function(result){
if(result.click == 1){
mainProc(id,result.click);
}
})
}
function clickListener(id){
chrome.storage.local.get("click",function(result){
if (result.click == 0) {
chrome.storage.local.set({"click":1},function(){
mainProc(id,result.click);
});
} else if (result.click == 1) {
chrome.storage.local.set({"click":0},function(){
mainProc(id,result.click);
});
} else if (result.click == null) {
chrome.storage.local.set({"click":1},function(){
mainProc(id,result.click);
});
} else if (result.click == undefined) {
chrome.storage.local.set({"click":1},function(){
mainProc(id,result.click);
});
}
})
}
function mainProc(id,click){
changeIcon(click);
chrome.tabs.sendMessage(id,{clicked:click});
}
script.js
chrome.runtime.onMessage.addListener(function(msg,sender){
var a = document.getElementsByTagName("a");
if(msg.clicked == 1){
console.log("clicked:",msg.clicked);
for(var i = 0;i<a.length;i++){
a[i].setAttribute("target","_blank");
a[i].className += " newtab";
}
} else if (msg.clicked == 0) {
console.log("clicked:",msg.clicked);
for(var i = 0;i<a.length;i++){
if(a[i].classList.contains("newtab")){
a[i].classList.remove("newtab");
a[i].removeAttribute("target");
}
}
}
});
manifest.json
{
"manifest_version":2,
"name":"newtab",
"description": "This extension lets you open each link in new tab",
"version":"1.0.0",
"author": "ritik saxena",
"browser_action":{
"default_icon":"dark.png",
"default_title": "newTab"
},
"background":{
"persistent":true,
"scripts":["background.js"]
},
"content_scripts":[{
"js":["script.js"],
"matches":["<all_urls>"]
}],
"permissions":[
"tabs",
"storage"
]
}