تصحيح أخطاء مكوّنات الإضافات
استخدم أدوات المطوّرين في Chrome وEdge لتصحيح أخطاء service workers الخلفية والنوافذ المنبثقة وبرامج المحتوى النصية بكفاءة
تصحيح أخطاء مكوّنات الإضافات درس مجاني في Browser Extensions Development (Chrome & Edge) على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Browser Extensions Development (Chrome & Edge)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Welcome to Extension Debugging
Debugging is the process of finding and fixing errors in your code. For browser extensions, it's crucial to understand how to debug different parts, like background scripts, popups, and content scripts.
Since extensions run in unique environments, the standard web debugging techniques need a slight adjustment. We'll explore how to use Chrome and Edge Developer Tools effectively.
Opening Extension DevTools
To debug your extension, you'll primarily use the browser's Developer Tools (DevTools). The way you open them depends on which part of your extension you want to inspect.
- Go to
chrome://extensions(oredge://extensions). - Ensure "Developer mode" is enabled.
- Find your extension and click "Inspect views" for background scripts or specific popups.
Inspecting Background Scripts
Background service workers are event-driven scripts that run in the background. They don't have a visible UI, so their DevTools instance is separate.
- On the
chrome://extensionspage, under your extension, look for the "Inspect views" link, specifically for "service worker" or "background page." - Clicking this link opens a dedicated DevTools window where you can see console logs, set breakpoints, and inspect variables for your background script.
Hands-on: Background Script
Let's see a simple background script in action and how to debug it. This script just logs a message when the extension starts up.
After loading this extension, open its background service worker DevTools from chrome://extensions. You'll see "Background script loaded!" and other messages in the console.
// background.js
console.log("Background script loaded!");
chrome.runtime.onInstalled.addListener(() => {
console.log("Extension installed or updated!");
});
chrome.action.onClicked.addListener((tab) => {
console.log("Extension icon clicked!");
// This would typically trigger a popup or other action
});Inspecting Popups & Options
Popups and options pages are essentially regular HTML pages with their own JavaScript and CSS. This means you debug them just like any other webpage.
- For a popup: Right-click anywhere on the popup when it's open and select "Inspect" or "Inspect Element."
- For an options page: Open the options page (usually from the extension management page or a direct link) and then right-click and "Inspect."
This opens a standard DevTools window for that specific UI component.
Hands-on: Popup UI
Here's the JavaScript for a basic popup. When you open the popup, this script runs and logs a message. You can inspect this popup's elements and console output directly.
To debug: Click your extension icon to open the popup, then right-click on the popup and choose "Inspect". Check the Console and Elements tabs.
// popup.js
document.addEventListener('DOMContentLoaded', () => {
console.log('Popup script loaded!');
const button = document.getElementById('myButton');
if (button) {
button.addEventListener('click', () => {
alert('Button clicked!');
console.log('Button was clicked!');
});
}
});Inspecting Content Scripts
Content scripts run in the context of a web page. This means they can interact with the page's DOM, but also that their DevTools are linked to the page itself.
- Open the web page where your content script is injected.
- Open the page's DevTools (usually F12 or right-click -> "Inspect").
- In the "Sources" tab, you'll often find your content script under a special
chrome-extension://ormoz-extension://URL.
Be aware that content scripts run in an isolated world, meaning they don't directly share global variables with the page's own JavaScript.
Hands-on: Content Script
This content script changes the background color of the body and logs a message when injected into a web page. Load this as part of an extension configured to run on a specific site (e.g., *://*/*).
To debug: Navigate to a web page where your script is injected, open the page's DevTools, and check the Console. You can also find the script in the Sources tab.
// content.js
console.log("Content script injected!");
// Example: Change background color
document.body.style.backgroundColor = "lightblue";
// Example: Add a button to the page
const newButton = document.createElement("button");
newButton.textContent = "Extension Button";
newButton.style.position = "fixed";
newButton.style.bottom = "10px";
newButton.style.right = "10px";
newButton.addEventListener("click", () => {
alert("Button from extension clicked!");
});
document.body.appendChild(newButton);Essential Debugging Tools
Beyond just seeing errors, DevTools offer powerful features:
- Breakpoints: Pause execution at specific lines of code to inspect variables and step through your program. Click on the line number in the "Sources" tab to set one.
- Console API: Use
console.log(),console.warn(),console.error()for quick output. Also,console.dir()for object inspection. - Network Tab: Monitor all network requests made by your extension or the page it interacts with.
- Elements Tab: Inspect and modify the HTML and CSS of your popup, options page, or the web page (for content scripts).
Advanced Debugging Tips
Here are a few more tips for effective debugging:
- Source Maps: If you're using a build tool (like Webpack or Rollup), ensure source maps are generated for easier debugging of original source code.
debugger;statement: Adddebugger;directly in your JavaScript code to programmatically trigger a breakpoint when DevTools are open.- Preserve Log: In the DevTools console settings, enable "Preserve log" to prevent the console from clearing on navigation or service worker restarts.
- Conditional Breakpoints: Right-click a breakpoint to add conditions, so it only pauses when a specific expression is true.
Debugging Component Quiz
You've learned how to access DevTools for different parts of your extension. Let's test your understanding.
Debugging Extension Recap
Great job! You've learned the essentials of debugging browser extensions.
- Background service workers are debugged via their dedicated "Inspect views" link on
chrome://extensions. - Popups and options pages are debugged like regular web pages: right-click and "Inspect."
- Content scripts are debugged by opening DevTools for the web page they are injected into.
- Utilize breakpoints, console logging, and other DevTools features to efficiently find and fix issues in your extension.
Mastering these techniques is key to building robust and error-free extensions!
الأسئلة الشائعة
هل درس «تصحيح أخطاء مكوّنات الإضافات» مجاني؟
نعم — نص درس «تصحيح أخطاء مكوّنات الإضافات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Browser Extensions Development (Chrome & Edge)، انتقل إلى CoddyKit PRO. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
ماذا ستتعلم في «تصحيح أخطاء مكوّنات الإضافات»؟
استخدم أدوات المطوّرين في Chrome وEdge لتصحيح أخطاء service workers الخلفية والنوافذ المنبثقة وبرامج المحتوى النصية بكفاءة تتمرن على Browser Extensions Development (Chrome & Edge) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تصحيح أخطاء مكوّنات الإضافات
- كتابة اختبارات الوحدة للإضافات
- استراتيجيات تحسين الأداء
- التسجيل والإبلاغ عن الأخطاء والتشخيص