Javascript 如何根据页面url更改扩展图标,并在不同图标中发出不同的警报
我正在构建一个chrome扩展,如果url与某些特定模式不匹配,我需要显示一些指示禁用的图标,如果用户单击禁用的图标并且没有显示弹出窗口,我需要发出警报。图标是chrome浏览器操作图标。但如果用户单击指示已启用的图标,则会显示默认弹出窗口。Javascript 如何根据页面url更改扩展图标,并在不同图标中发出不同的警报,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我正在构建一个chrome扩展,如果url与某些特定模式不匹配,我需要显示一些指示禁用的图标,如果用户单击禁用的图标并且没有显示弹出窗口,我需要发出警报。图标是chrome浏览器操作图标。但如果用户单击指示已启用的图标,则会显示默认弹出窗口。 基本上,当页面url不匹配时,弹出窗口不应打开,并应发出警报。 我目前正在后台页面中使用此功能,但它看起来效率很低,大多数情况下它都可以工作,但只有在重新加载页面后才会显示警报: background.js var alertError =
基本上,当页面url不匹配时,弹出窗口不应打开,并应发出警报。
我目前正在后台页面中使用此功能,但它看起来效率很低,大多数情况下它都可以工作,但只有在重新加载页面后才会显示警报:
background.js
var alertError = function(arg){
alert('Something');
};
chrome.tabs.onActivated.addListener(function(info){
chrome.tabs.get(info.tabId, function(change){
if(change.url == undefined){
chrome.browserAction.setPopup({tabId: info.tabId, popup: ''});
chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: info.tabId});
chrome.browserAction.onClicked.removeListener(alertError);
chrome.browserAction.onClicked.addListener(alertError);
console.log('undefined');
}
else if(change.url.match(/https:\/\/google\.com\/*/) == null){
chrome.browserAction.setPopup({tabId: info.tabId, popup: ''});
chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: info.tabId});
chrome.browserAction.onClicked.removeListener(alertError);
chrome.browserAction.onClicked.addListener(alertError);
console.log('not matching');
}
else{
chrome.browserAction.setPopup({tabId: info.tabId, popup: '../html/popup.html'});
chrome.browserAction.setIcon({path: '../icons/icon.png', tabId: info.tabId});
console.log('matched');
}
});
});
chrome.tabs.onUpdated.addListener(function (tabId, change, tab){
if(change.url == undefined){
return;
}
else if(/https:\/\/google\.com\/*/) == null){
chrome.browserAction.setPopup({tabId: tabId, popup: ''});
chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: tabId});
chrome.browserAction.onClicked.removeListener(alertError);
chrome.browserAction.onClicked.addListener(alertError);
console.log('not matching');
}
else{
chrome.browserAction.onClicked.removeListener(alertError);
chrome.browserAction.setPopup({tabId: tabId, popup: '../html/popup.html'});
chrome.browserAction.setIcon({path: '../icons/icon.png', tabId: tabId});
console.log('matched');
}
});
编辑
清单文件:
{
"manifest_version": 2,
"name": "Something",
"description": "Something",
"version": "2.5",
"permissions": [
"tabs",
"<all_urls>",
"storage"
],
"background": {
"scripts" : ["js/localstoragedb.min.js", "js/background.js"]
},
"icons":{
"16" : "icons/icon16.png",
"48" : "icons/icon48.png",
"128" : "icons/icon128.png"
},
"browser_action": {
"default_title" : "Title",
"default_icon" : "icons/icon.png"
},
"web_accessible_resources": ["js/inject.js"]
}
{
“清单版本”:2,
“名字”:“某物”,
“描述”:“某物”,
“版本”:“2.5”,
“权限”:[
“标签”,
"",
“存储”
],
“背景”:{
“脚本”:[“js/localstoragedb.min.js”,“js/background.js”]
},
“图标”:{
“16”:“icons/icon16.png”,
“48”:“icons/icon48.png”,
“128”:“icons/icon128.png”
},
“浏览器操作”:{
“默认标题”:“标题”,
“默认图标”:“icons/icon.png”
},
“web可访问资源”:[“js/inject.js”]
}
我想我找到了一个解决方案:
在background.js中:
var alertError = function(arg){
if(arg.url.match(/https:\/\/google\.com\/*/) == null){
alert('Something');
}
};
chrome.browserAction.onClicked.addListener(alertError);
chrome.tabs.onActivated.addListener(function(info){
chrome.tabs.get(info.tabId, function(change){
if(change.url == undefined){
chrome.browserAction.setPopup({tabId: info.tabId, popup: ''});
chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: info.tabId});
console.log('undefined');
}
else if(change.url.match(/https:\/\/google\.com\/*/) == null){
chrome.browserAction.setPopup({tabId: info.tabId, popup: ''});
chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: info.tabId});
console.log('not matching');
}
else{
chrome.browserAction.setPopup({tabId: info.tabId, popup: '../html/popup.html'});
chrome.browserAction.setIcon({path: '../icons/icon.png', tabId: info.tabId});
console.log('matched');
}
});
});
chrome.tabs.onUpdated.addListener(function (tabId, change, tab){
if(tab.url == undefined){
return;
}
else if(tab.url.match(/https:\/\/google\.com\/*/) == null){
chrome.browserAction.setPopup({tabId: tabId, popup: ''});
chrome.browserAction.setIcon({path: '../icons/icon-disabled.png', tabId: tabId});
console.log('not matching');
}
else{
chrome.browserAction.setPopup({tabId: tabId, popup: '../html/popup.html'});
chrome.browserAction.setIcon({path: '../icons/icon.png', tabId: tabId});
console.log('matched');
}
});
在清单中:
"browser_action": {
"default_title" : "Title",
"default_icon" : "icons/icon.png",
"default_popup": "html/popup.html"
},
抱歉,但我仍然不清楚你的代码有什么问题:是不是只有在你重新加载页面时它才起作用?不是,如果你多次重新加载,同时尝试打开弹出窗口,它就不起作用了。在你的回答中,你能解释一下你更改了什么吗?必须“区分”问题和答案是一种痛苦。谢谢!我添加了check-in
alertError
功能,这样我就不需要添加和删除侦听器,也不需要添加默认弹出窗口。之前“不知何故”重新加载时,弹出窗口消失,警报未显示,但现在,由于警报有一个检查,它会在需要时显示,并且由于有一个默认弹出窗口,它从未失败。此外,如果url和选项卡更改,则会触发执行所需更改的事件(onUpdate和onActivated)。如果我错过了任何可能影响这一点的其他案例,请告诉我。