Dynamiczne reguły i debugowanie declarativeNetRequest
Dodadzą Państwo reguły i zaktualizują je w czasie działania, zarządzą identyfikatorami oraz priorytetami reguł i będą obserwować dopasowane żądania na potrzeby debugowania.
Dynamiczne reguły i debugowanie declarativeNetRequest to bezpłatna lekcja Browser Extensions Development (Chrome & Edge) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Browser Extensions Development (Chrome & Edge), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Browser Extensions Development (Chrome & Edge) zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Static vs Dynamic Rules
declarativeNetRequest supports two rule kinds. Static rules ship in JSON files in the manifest. Dynamic rules are added at runtime and persist across restarts.
Dynamic rules let your extension adapt to user choices without an update.
Declaring the Permission
You need the declarativeNetRequest permission. For dynamic rules at runtime, that is all you require.
{
"permissions": ["declarativeNetRequest"]
}The Shape of a Rule
Each rule has an id, a priority, an action, and a condition. The condition decides which requests match.
{
"id": 1,
"priority": 1,
"action": { "type": "block" },
"condition": { "urlFilter": "ads.example.com" }
}Adding Dynamic Rules
Use updateDynamicRules to add rules. Pass them in the addRules array.
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
id: 1, priority: 1,
action: { type: 'block' },
condition: { urlFilter: 'tracker.com', resourceTypes: ['script'] }
}]
})Removing Rules by ID
The same call removes rules by listing their IDs in removeRuleIds. Adds and removes can happen together.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1]
})Unique Rule IDs
Every dynamic rule needs a unique numeric id. Reusing an existing ID without removing it first causes an error.
const existing = await chrome.declarativeNetRequest.getDynamicRules()
const nextId = Math.max(0, ...existing.map(r => r.id)) + 1Priorities Resolve Conflicts
When multiple rules match, priority decides which wins. Higher priority takes precedence; allow rules can override block rules at the same level.
{ "id": 2, "priority": 2, "action": { "type": "allow" }, "condition": { "urlFilter": "safe.com" } }Session Rules
If you want rules that vanish when the browser closes, use updateSessionRules instead. They never persist to disk.
chrome.declarativeNetRequest.updateSessionRules({
addRules: [tempRule]
})Observing Matched Requests
For debugging, enable the feedback permission and listen to onRuleMatchedDebug to see which rule fired for each request.
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((info) => {
console.log('Matched rule ' + info.rule.ruleId)
})Inspecting Current Rules
Call getDynamicRules to list everything currently active. This is essential when a rule misbehaves and you need to confirm what is installed.
const rules = await chrome.declarativeNetRequest.getDynamicRules()
console.log(rules.length + ' active rules')Staying Within Limits
There is a cap on the number of dynamic rules. Audit and remove stale rules instead of piling on, and group conditions where possible to stay efficient.
Quick Check
Test your dynamic rules knowledge.
Recap
You learned dynamic request rules:
- Add and remove with
updateDynamicRules - Keep IDs unique and use
priorityfor conflicts - Use session rules for temporary state
- Debug with
onRuleMatchedDebugandgetDynamicRules - Respect rule count limits
Runtime rules let your extension adapt without shipping an update.
Często zadawane pytania
Czy lekcja „Dynamiczne reguły i debugowanie declarativeNetRequest” jest bezpłatna?
Tak — pełny tekst „Dynamiczne reguły i debugowanie declarativeNetRequest” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Browser Extensions Development (Chrome & Edge), przejdź na CoddyKit PRO. Kurs Browser Extensions Development (Chrome & Edge) zawiera 4 lekcji w sumie.
Co nauczysz się w „Dynamiczne reguły i debugowanie declarativeNetRequest”?
Dodadzą Państwo reguły i zaktualizują je w czasie działania, zarządzą identyfikatorami oraz priorytetami reguł i będą obserwować dopasowane żądania na potrzeby debugowania. Ćwiczysz Browser Extensions Development (Chrome & Edge) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Browser Extensions Development (Chrome & Edge)?
Nie wymagamy żadnego doświadczenia. Browser Extensions Development (Chrome & Edge) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Dynamiczne reguły i debugowanie declarativeNetRequest”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Browser Extensions Development (Chrome & Edge)?
Tak. Każda lekcja Browser Extensions Development (Chrome & Edge) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Blokowanie żądań sieciowych
- Modyfikowanie nagłówków żądań
- Przekierowywanie i przepisywanie adresów URL
- Dynamiczne reguły i debugowanie declarativeNetRequest