動的ルールとdeclarativeNetRequestのデバッグ
実行時にルールを追加・更新し、ルールIDと優先度を管理し、デバッグのために一致したリクエストを確認します。
「動的ルールとdeclarativeNetRequestのデバッグ」はCoddyKit上の無料Browser Extensions Development (Chrome & Edge)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBrowser Extensions Development (Chrome & Edge)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Browser Extensions Development (Chrome & Edge)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「動的ルールとdeclarativeNetRequestのデバッグ」レッスンは無料ですか?
はい。「動的ルールとdeclarativeNetRequestのデバッグ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Browser Extensions Development (Chrome & Edge)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Browser Extensions Development (Chrome & Edge)コースには全4レッスンが含まれています。
「動的ルールとdeclarativeNetRequestのデバッグ」で何を学びますか?
実行時にルールを追加・更新し、ルールIDと優先度を管理し、デバッグのために一致したリクエストを確認します。 ブラウザで直接実行するハンズオンコードでBrowser Extensions Development (Chrome & Edge)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Browser Extensions Development (Chrome & Edge)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのBrowser Extensions Development (Chrome & Edge)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「動的ルールとdeclarativeNetRequestのデバッグ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このBrowser Extensions Development (Chrome & Edge)レッスンでコードを書いて実行できますか?
はい。すべてのBrowser Extensions Development (Chrome & Edge)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ネットワーク リクエストのブロック
- リクエストヘッダーの変更
- URLのリダイレクトと書き換え
- 動的ルールとdeclarativeNetRequestのデバッグ