[英]Chrome Extensions: How do you gray out icon depending on the url?
chrome 是否有一个 api 来禁用(并因此变灰)某些 url 上的 chrome 扩展,或者我是否只需要一个 if 语句来检查 url 并相应地切换出图标?
You could use chrome.declarativeContent
, it allows you to show page action depending on the URL of a web page and the CSS selectors its content matches.您可以使用
chrome.declarativeContent
,它允许您根据网页的 URL 和与其内容匹配的 CSS 选择器显示页面操作。
You could create conditions ( yes, you could use regex) and actions ( ShowPageAction
SetIcon
) via a constructor like new chrome.declarativeContent.PageStateMatcher
and new chrome.declarativeContent.ShowPageAction()
.您可以通过
new chrome.declarativeContent.PageStateMatcher
和new chrome.declarativeContent.ShowPageAction()
等构造函数创建条件(是的,您可以使用正则表达式)和操作( ShowPageAction
SetIcon
new chrome.declarativeContent.ShowPageAction()
。 Detailed sample are listed in the api documentation .详细示例在api 文档中列出。
var rule2 = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'www.google.com', schemes: ['https'] },
css: ["input[type='password']"]
}),
new chrome.declarativeContent.PageStateMatcher({
css: ["video"]
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
};
chrome.runtime.onInstalled.addListener(function(details) {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([rule2]);
});
});
add to manifest.js:添加到 manifest.js:
"background": { "scripts": ["background.js"] },
"content_scripts" :[
{
"matches" : [
"*://*.example.com/*"
],
"js" : ["main.js"],
"run_at" : "document_idle"
}
]
main.js:主要.js:
chrome.runtime.sendMessage({type:'showPageAction'});
background.js:背景.js:
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.type === 'showPageAction'){
chrome.pageAction.show(sender.tab.id);
}
});
You use browserAction instead of browserAction , but there are some things to note:您使用browserAction而不是browserAction ,但有一些注意事项:
According to my test, you cannot use "<all_urls>" and "activeTab" in "content_scripts" and "Permissions", otherwise the icon will always be colored.根据我的测试,你不能在“content_scripts”和“Permissions”中使用“<all_urls>”和“activeTab”,否则图标会一直是彩色的。
"content_scripts": [
{
"matches": ["<all_urls>"], // error
"js": ["content.js"],
}
],
"permissions": ["<all_urls>", "activeTab", "storage", "tabs", "declarativeContent"], // error
narrow down缩小范围
"content_scripts": [
{
"matches": ["http://127.0.0.1/*"],
"js": ["content.js"],
}
],
"permissions": ["storage", "tabs", "declarativeContent"],
background.js背景.js
let rule1 = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: '127.0.0.1' },
}),
],
actions: [new chrome.declarativeContent.ShowPageAction()],
};
chrome.declarativeContent.onPageChanged.removeRules(undefined, data => {
chrome.declarativeContent.onPageChanged.addRules([rule1], data => {
console.log('addRules', data);
});
});
The removeRules operation is performed because the rule will be added repeatedly every time the extension is refreshed.
Reference question link参考问题链接
https://stackoverflow.com/a/63434774/9854149 https://stackoverflow.com/a/63434774/9854149
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.