تعديل ترويسات الطلبات
نفّذ قواعد لتعديل ترويسات طلبات HTTP، بما يتيح سلوكًا مخصصًا أو مصادقة للطلبات الصادرة
تعديل ترويسات الطلبات درس مجاني في Browser Extensions Development (Chrome & Edge) على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Browser Extensions Development (Chrome & Edge)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What are Request Headers?
When your browser talks to a website, it sends messages called HTTP requests. These requests carry extra info, like metadata, in something called headers.
Headers tell the server things like what browser you're using (User-Agent), what kind of content you expect (Accept), or even authentication tokens (Authorization).
Efficient Header Control
We learned about declarativeNetRequest for blocking and redirecting. It's powerful because it lets the browser handle rules efficiently, without your extension's JavaScript always running.
Today, we'll use it to modify these request headers, adding, changing, or removing them before a request even leaves your browser!
Introducing `modifyHeaders`
To change headers, we use a specific actionType within our declarativeNetRequest rules: "modifyHeaders".
This action takes an array of requestHeaders. Each item in this array describes a single header modification you want to make.
Header Operations Explained
For each header modification, you specify an operation:
"set": Replaces an existing header's value or adds it if it doesn't exist."append": Adds a new value to a header. If the header already exists, it will have multiple values. If not, it adds it."remove": Deletes a header entirely from the request.
Essential Permissions
To use declarativeNetRequest for header modification, your extension needs specific permissions in its manifest.json file:
"declarativeNetRequest": The basic permission to use the API."declarativeNetRequestWithHostAccess": Needed if your rules modify headers on specific websites (hosts). This is usually the case!
Add a Custom Header
Let's add a custom header called X-CoddyKit-User to all outgoing requests. This could be useful for identifying requests from your extension.
This code goes into your extension's background.js file.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [1],
addRules: [{
id: 1,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "X-CoddyKit-User",
operation: "set",
value: "CoddyKitStudent"
}]
},
condition: {
urlFilter: "<all_urls>",
resourceTypes: ["main_frame", "sub_frame", "xmlhttprequest"]
}
}]
});
// In a real extension, this runs once on install/update.
// You'd observe the header in your browser's DevTools Network tab.Dissecting the Rule
The code uses chrome.declarativeNetRequest.updateDynamicRules to manage rules. Here's a quick breakdown:
id: A unique number for your rule.priority: Determines which rule wins if multiple rules apply.action: Defines what to do. Here,"modifyHeaders"with ourrequestHeaders.condition: Specifies when the rule applies (e.g.,<all_urls>for everywhere).
Change the User-Agent
You can use "set" to change standard headers too. Here, we'll modify the User-Agent header for requests going to example.com. This spoofs your browser's identity for that site.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [2],
addRules: [{
id: 2,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "User-Agent",
operation: "set",
value: "CoddyKitBrowser/1.0 (Extension)"
}]
},
condition: {
urlFilter: "*://example.com/*",
resourceTypes: ["main_frame", "sub_frame"]
}
}]
});
// This rule would change the User-Agent for example.com.Remove a Header
Sometimes, you might want to remove a header for privacy or to prevent certain tracking. Let's remove the Referer header for all image requests.
The Referer header tells a website where you came from.
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [3],
addRules: [{
id: 3,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [{
header: "Referer",
operation: "remove"
}]
},
condition: {
urlFilter: "<all_urls>",
resourceTypes: ["image"]
}
}]
});
// This rule would remove the Referer header for image requests.Quick Check: Header Operations
You want to add an API key as a custom header, X-API-Key, to a request. If the header somehow already exists, you want to make sure your key is also present, possibly as a second value, rather than replacing an existing one.
Recap & Next Steps
Great job! You've learned how to powerfully modify HTTP request headers using declarativeNetRequest.
- We explored the
"modifyHeaders"action. - Understood the
"set","append", and"remove"operations. - Saw how to add custom headers, change user agents, and remove sensitive info.
- Remember the necessary permissions:
declarativeNetRequestanddeclarativeNetRequestWithHostAccess.
Next, we'll dive into redirecting and rewriting URLs!
الأسئلة الشائعة
هل درس «تعديل ترويسات الطلبات» مجاني؟
نعم — نص درس «تعديل ترويسات الطلبات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Browser Extensions Development (Chrome & Edge)، انتقل إلى CoddyKit PRO. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
ماذا ستتعلم في «تعديل ترويسات الطلبات»؟
نفّذ قواعد لتعديل ترويسات طلبات HTTP، بما يتيح سلوكًا مخصصًا أو مصادقة للطلبات الصادرة تتمرن على Browser Extensions Development (Chrome & Edge) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Browser Extensions Development (Chrome & Edge)؟
لا تُشترط خبرة سابقة. Browser Extensions Development (Chrome & Edge) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تعديل ترويسات الطلبات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Browser Extensions Development (Chrome & Edge) هذا؟
نعم. كل درس في Browser Extensions Development (Chrome & Edge) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- حظر طلبات الشبكة
- تعديل ترويسات الطلبات
- إعادة توجيه عناوين URL وإعادة كتابتها
- القواعد الديناميكية وتصحيح أخطاء declarativeNetRequest