我正在学习编写JavaScript扩展,并选择了一个基本的应用程序。我正在编写的扩展名只是在每个链接中添加了属性target='_blank'
。单击图标后激活扩展程序。它工作得很好,但我正在添加监听器来更新,替换和激活事件,而且它们都没有工作。已经检查了一些SO帖子,但它没有帮助(比如说this,this等等)。用Google搜索到达饱和点并发布问题。请帮忙。
这是我的代码。
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"
]
}