简体   繁体   English

Chrome 扩展程序:如何根据 url 使图标变灰?

[英]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.PageStateMatchernew 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.

相关问题 Chrome 扩展开发:如何使图标 onClick 变灰? - Chrome Extension Dev: How to gray out the Icon onClick? 如何根据某些数据文件中保存的URL为Chrome扩展程序创建内容脚本? - how do you make content scripts for Chrome extensions based on saved URL's from some data file? 如何从 Chrome 扩展程序中删除错误? - How do you remove the errors from Chrome Extensions? Chrome扩展程序:是否有一种方法可以根据所访问的网址触发功能? - Chrome Extensions: Is there a way to trigger functions depending on the Url being visited? 运行夜班测试时如何加载多个chrome扩展? - How do you load multiple chrome extensions when running nightwatch tests? Vue.js:如何根据 data.table 行中项目的值更改图标和背景颜色? - Vue.js: How do you change an icon and background color depending on the value of an item in a data table row? 您如何使用 chrome 扩展检测 url 的变化? - How do you detect changes in url with a chrome extension? Chrome扩展程序:根据您所在的网站运行不同的.js文件? - Chrome Extensions: Running Different .js Files Depending on What Site You're On? 如何使用 chrome 扩展程序保存数据? - How do I save data with chrome extensions? 默认情况下,如何使下拉字段灰显? - How do I gray out drop down fields by default?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM