Как заставить расширение для хрома работать только на определенных страницах?
Мне нужно чтобы расширение работало только на страницах с .example.com и на локальных страницах. Не могу понять как это сделать, сейчас работает на всех.
manifets.json
{
"manifest_version": 3,
"name": "Test helper",
"description": "Помощник для тестирования",
"version": "1.0.0",
"icons": {"128": "icon_128.png"},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": ["jquery.min.js", "main.js" ],
"css": ["css/style.css"]
}
],
"action": {
"default_popup": "popup.html"
},
"permissions": ["activeTab", "scripting"]
}
background.js
// Wrap in an onInstalled callback in order to avoid unnecessary work
// every time the background script is run
chrome.runtime.onInstalled.addListener(() => {
// Page actions are disabled by default and enabled on select tabs
chrome.action.disable();
// Clear all rules to ensure only our expected rules are set
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
// Declare a rule to enable the action on example.com pages
let exampleRule = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {hostSuffix: '.example.com'},
})
],
actions: [new chrome.declarativeContent.ShowAction()],
};
// Finally, apply our new array of rules
let rules = [exampleRule];
chrome.declarativeContent.onPageChanged.addRules(rules);
});
});
Ответы (1 шт):
Автор решения: Бармалей
→ Ссылка
Решил это удалив бэкграунд и добавив в манифест
"content_scripts": [
{
"matches": ["https://*.example.com/","http://127.0.0.1/*","file:///*"],
}
],