0Pricing
Browser Extensions Development (Chrome & Edge) · บทเรียน

การแทรกสคริปต์เนื้อหา

เรียนรู้วิธีแทรก JavaScript และ CSS ลงในหน้าเว็บเฉพาะ เพื่อให้ส่วนขยายปรับเปลี่ยนรูปลักษณ์และพฤติกรรมของหน้าเว็บได้

การแทรกสคริปต์เนื้อหา เป็นบทเรียน Browser Extensions Development (Chrome & Edge) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Browser Extensions Development (Chrome & Edge) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Content Scripts?

Welcome to Content Scripts! These are JavaScript files that run in the context of web pages loaded in the browser.

Think of them as a bridge. They allow your extension to interact with the web page's content, just like a script running directly on that page.

  • Read details from a page.
  • Modify the page's appearance.
  • Add or remove elements.

The Isolated World

Content scripts run in an 'isolated world'. This means they don't interfere with the page's own JavaScript environment.

Your script can access and modify the page's Document Object Model (DOM), but it can't directly access JavaScript variables or functions defined by the web page itself.

This isolation prevents conflicts and ensures your extension doesn't break the web page's functionality.

Static Injection via manifest.json

The simplest way to inject content scripts is by declaring them in your extension's manifest.json file.

These scripts will automatically load and run on web pages that match the patterns you specify. It's great for scripts that need to be active whenever a certain type of page loads.

Static JS Injection Example

Here's how you'd set up a content script in your manifest.json to inject a JavaScript file called content.js:

{  "manifest_version": 3,
  "name": "My Page Modifier",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["https://*.example.com/*"],
      "js": ["content.js"]
    }
  ]
}

Static CSS Injection Example

You can also inject CSS files using the same content_scripts field in your manifest.json. This is perfect for custom styling!

The CSS will apply to matching pages, modifying their visual appearance.

{  "manifest_version": 3,
  "name": "My Page Styler",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["*://*.google.com/*"],
      "css": ["content.css"]
    }
  ]
}

Programmatic Injection

Sometimes you need more control over when a script is injected. This is where programmatic injection comes in.

You can inject scripts dynamically based on user actions (like clicking a button in your popup) or specific conditions, rather than automatically on page load.

Using chrome.scripting.executeScript()

To inject scripts programmatically, you use the chrome.scripting.executeScript() API from your extension's background script or popup.

This method requires the scripting permission in your manifest.json.

  • It targets a specific tab.
  • You can inject a function or a file.
  • Perfect for on-demand interactions.

Programmatic Code Demo

This example shows a background script injecting a simple function into the active tab when the extension icon is clicked.

The injected function changes the page's background color. Try running it!

// manifest.json snippet:
// {"permissions": ["activeTab", "scripting"]}

// background.js
chrome.action.onClicked.addListener(async (tab) => {
  if (tab.url.startsWith("http")) {
    await chrome.scripting.executeScript({
      target: { tabId: tab.id },
      function: () => {
        document.body.style.backgroundColor = 'lightblue';
        console.log('Background changed!');
      }
    });
  }
});

Permissions & Best Practices

For programmatic injection, you'll typically need the activeTab and scripting permissions.

  • activeTab: Grants temporary host permissions to the currently active tab when the user invokes the extension.
  • scripting: Allows using the chrome.scripting API to inject code.

Always request the minimum necessary permissions for your extension's functionality.

Quick Check

Which of the following statements about content scripts is TRUE?

Recap: Injecting Content Scripts

You've learned how to inject content scripts into web pages!

  • Content scripts let your extension interact with web pages.
  • They run in an isolated world to prevent conflicts.
  • You can inject them statically via manifest.json for automatic loading.
  • Or, inject them programmatically using chrome.scripting.executeScript() for on-demand actions.

Next, we'll dive into how to modify the DOM of web pages using these powerful scripts!

คำถามที่พบบ่อย

บทเรียน “การแทรกสคริปต์เนื้อหา” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแทรกสคริปต์เนื้อหา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Browser Extensions Development (Chrome & Edge) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Browser Extensions Development (Chrome & Edge) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแทรกสคริปต์เนื้อหา”

เรียนรู้วิธีแทรก JavaScript และ CSS ลงในหน้าเว็บเฉพาะ เพื่อให้ส่วนขยายปรับเปลี่ยนรูปลักษณ์และพฤติกรรมของหน้าเว็บได้ คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การแทรกสคริปต์เนื้อหา
  2. การปรับเปลี่ยน DOM ของหน้าเว็บ
  3. การโต้ตอบกับข้อมูลหน้าเว็บ
  4. การแทรก CSS และการจัดรูปแบบแบบแยกส่วน
← กลับไปที่ Browser Extensions Development (Chrome & Edge)