การบล็อกคำขอเครือข่าย
เรียนรู้การกำหนดกฎเพื่อบล็อกคำขอเครือข่ายที่ไม่ต้องการ เช่น โฆษณาหรือสคริปต์ติดตาม โดยใช้ `declarativeNetRequest`
การบล็อกคำขอเครือข่าย เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to Blocking Requests
Welcome to modifying web requests! This lesson introduces declarativeNetRequest, a powerful API for controlling network requests in your browser extension.
You'll learn how to define rules to block unwanted content like ads, tracking scripts, or specific resources, making web browsing cleaner and faster.
Why Block Network Requests?
Blocking network requests isn't just about privacy; it's also about performance and user experience.
- Remove Ads: Block requests to ad servers.
- Stop Trackers: Prevent tracking scripts from loading.
- Improve Performance: Less data downloaded means faster page loads.
- Enhance Security: Block malicious domains or content.
Declarative vs. Imperative
Traditional ad blockers often use imperative logic: JavaScript code that runs on every page to detect and block elements.
declarativeNetRequest is declarative. You define rules, and the browser handles the blocking efficiently, often without waking up your extension's JavaScript. This is faster and more resource-friendly!
Manifest Permission for Blocking
To use declarativeNetRequest, your extension needs to declare the permission in its manifest.json file.
This tells the browser your extension intends to modify network requests.
{
"manifest_version": 3,
"name": "My Blocker",
"version": "1.0",
"permissions": [
"declarativeNetRequest"
]
}Structure of a Blocking Rule
A blocking rule is an object with an id, an action, and a condition. The browser checks these conditions against every network request.
id: A unique number for the rule.action: What to do (e.g., 'block', 'redirect').condition: When to apply the action (e.g., 'urlFilter', 'resourceTypes').
The 'Block' Action Type
For this lesson, we'll focus on the "block" action. When a request matches a rule with this action, the browser simply prevents that request from completing.
The action part of your rule will look like this:
{
"action": {
"type": "block"
}
}Conditions: `urlFilter`
The urlFilter property in a rule's condition allows you to specify a pattern for URLs to match. It's like a mini-regex.
"*://*.example.com/*"matches any subdomain of example.com."*://ads.google.com/*"specifically targets Google ads."*track*"matches URLs containing 'track'.
Conditions: `resourceTypes`
You can also specify which types of resources to block using resourceTypes. This helps you target specific content like images, scripts, or fonts.
Common types include "script", "image", "stylesheet", "media", and "font".
Adding Dynamic Blocking Rules
Rules can be added dynamically by your extension using chrome.declarativeNetRequest.updateDynamicRules(). This is often done in your background script.
This example adds a rule to block requests containing "adserver" in their URL, specifically for scripts and images.
console.log("Background script running.");
const BLOCK_RULE_ID = 1;
chrome.runtime.onInstalled.addListener(() => {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [BLOCK_RULE_ID], // Remove if already exists
addRules: [{
id: BLOCK_RULE_ID,
priority: 1,
action: { type: "block" },
condition: {
urlFilter: "*://*adserver*/*",
resourceTypes: ["script", "image"]
}
}]
}, () => {
if (chrome.runtime.lastError) {
console.error("Error updating rules: ", chrome.runtime.lastError);
} else {
console.log("Blocking rule added!");
}
});
});
// To test, visit a page with 'adserver' in script/image URLs.Quick Check: Rule Matching
Consider the following blocking rule:
{
"id": 2,
"priority": 1,
"action": { "type": "block" },
"condition": {
"urlFilter": "*://*.tracker.com/*",
"resourceTypes": ["script", "image"]
}
}Which of these requests would be blocked?
Recap: Blocking Requests
Great job! You've learned the basics of blocking network requests with declarativeNetRequest.
- It's a powerful, performant, and privacy-friendly API.
- Rules consist of an
id,action(e.g.,"block"), andcondition. urlFilterandresourceTypesare key for defining conditions.- Dynamic rules can be added and managed using
updateDynamicRules().
Next, you'll explore modifying headers!
คำถามที่พบบ่อย
บทเรียน “การบล็อกคำขอเครือข่าย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การบล็อกคำขอเครือข่าย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การบล็อกคำขอเครือข่าย”
เรียนรู้การกำหนดกฎเพื่อบล็อกคำขอเครือข่ายที่ไม่ต้องการ เช่น โฆษณาหรือสคริปต์ติดตาม โดยใช้ `declarativeNetRequest` คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การบล็อกคำขอเครือข่าย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Browser Extensions Development (Chrome & Edge) นี้ได้ไหม
ได้ บทเรียน Browser Extensions Development (Chrome & Edge) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การบล็อกคำขอเครือข่าย
- การแก้ไขส่วนหัวคำขอ
- การเปลี่ยนเส้นทางและเขียน URL ใหม่
- กฎแบบไดนามิกและการดีบัก declarativeNetRequest