Chrome插件开发入门教程
ChromeExTips
如何disable(灰化)chrome插件的pageAction?
Chrome documents this behavior in their docs: https://developer.chrome.com/docs/extensions/reference/action/#emulating-pageactions-with-declarativecontent. The following describes how to achieve it in manifest v3.
// background.js
chrome.runtime.onInstalled.addListener(() => {
// disable the action by default
chrome.action.disable();
// remove existing rules so only ours are applied
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// add a custom rule
chrome.declarativeContent.onPageChanged.addRules([
{
// define the rule's conditions
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: "reddit.com" },
}),
],
// show the action when conditions are met
actions: [new chrome.declarativeContent.ShowAction()],
},
]);
});
});
This will require the declarativeContent
permission in manifest.json
{
...
"permissions": ["declarativeContent"]
...
}
chrome.pageAction例子
chrome.browserAction例子
page action 和 browser action 的区别是什么?
Browser Action buttons are intended to be used when your extension can be used most of the time, or on most pages. They also allow you to provide some immediately visible status information to the user by having a badge containing a couple/few characters over the icon and changing the color of the background used for that badge.
Page Action buttons are intended for use when your extension is often/usually not available for use. For instance, if it's only usable on a few domains or URLs.