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

ทำความเข้าใจสิทธิ์ขั้นสูง

สำรวจสิทธิ์ที่มีความละเอียดอ่อน เช่น `activeTab`, `scripting` และสิทธิ์โฮสต์ พร้อมเรียนรู้ว่าควรขอสิทธิ์เหล่านี้เมื่อใดและอย่างไร

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

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

Beyond Basic Permissions

Welcome! In this lesson, we'll dive into advanced browser extension permissions. These permissions grant powerful capabilities, allowing your extension to interact more deeply with web content and user data.

Understanding them is crucial for building robust extensions while maintaining user trust and security.

We'll cover:

  • Host Permissions
  • activeTab Permission
  • scripting Permission

Granting Web Access

Host permissions are fundamental. They define which websites your extension can interact with. This includes reading data, modifying content, or making network requests on those specific sites.

Think of them as "keys" that unlock access to certain web domains.

Manifesting Host Access

You declare host permissions in your manifest.json file under the host_permissions key. Each string is a URL pattern.

For instance, to allow access to all pages on google.com, you'd specify:

{
  "name": "My Extension",
  "version": "1.0",
  "manifest_version": 3,
  "host_permissions": [
    "https://www.google.com/*"
  ]
}

Broad Access with Wildcards

You can use wildcards (*) for broader access. For example, "<all_urls>" (or "*://*/*") grants access to all URLs, on all schemes (HTTP, HTTPS).

While powerful, using broad wildcards should be done with extreme caution, as it grants your extension significant control over the user's browsing experience on any site.

{
  "name": "My Extension",
  "version": "1.0",
  "manifest_version": 3,
  "host_permissions": [
    "<all_urls>"
  ]
}

Temporary Tab Privileges

The activeTab permission is a safer alternative to broad host permissions for one-off actions on the current page.

When the user invokes your extension (e.g., clicks its icon), activeTab grants your extension temporary host permissions to the currently active tab. These permissions last until the user navigates away or closes the tab.

{
  "name": "My Extension",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [
    "activeTab"
  ]
}

Getting Current Tab URL

With activeTab, your extension can access information about the current tab, like its URL or title, without needing permanent host permissions.

Try running this example. Imagine this code is in your extension's popup script:

// This script runs when the extension popup opens.
document.addEventListener('DOMContentLoaded', function() {
  chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
    if (tabs.length > 0) {
      const activeTab = tabs[0];
      console.log('Active tab URL:', activeTab.url);
      // Display in popup for demonstration
      document.body.innerHTML = `<p>Current URL: ${activeTab.url}</p>`;
    } else {
      document.body.innerHTML = `<p>No active tab.</p>`;
    }
  });
});

Programmatic Code Injection

The scripting permission allows your extension to programmatically inject JavaScript and CSS into web pages. This is how you modify a page's content or behavior.

It's an essential permission for content scripts, replacing the Manifest V2 tabs.executeScript API.

{
  "name": "My Extension",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [
    "scripting"
  ],
  "host_permissions": [
    "https://www.example.com/*"
  ]
}

Modifying a Page

Once you have scripting permission (and host permission for the target tab), you can inject code. This example injects a simple script to change the background color of the active tab.

Imagine this running in your background script or popup:

// This code would typically run from a background script
// or popup after a user action.
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
  if (tabs.length > 0) {
    const activeTabId = tabs[0].id;
    chrome.scripting.executeScript({
      target: { tabId: activeTabId },
      function: () => {
        // This function runs in the context of the web page
        document.body.style.backgroundColor = 'lightblue';
        console.log('CoddyKit: Page background changed!');
      }
    });
  }
});

Least Privilege Principle

Always follow the Principle of Least Privilege: request only the permissions your extension absolutely needs.

  • Required Permissions: Declared in manifest.json and requested at install.
  • Optional Permissions: Can be requested at runtime using chrome.permissions.request() only when the user needs that specific feature. This gives users more control and builds trust.

Permission Scenarios

Which permission(s) would you need for an extension that, when its icon is clicked, reads the current page's title and then injects a custom CSS file into that same page?

Advanced Permissions Summary

Great job! You've learned about powerful advanced permissions:

  • Host Permissions: Grant access to specific websites.
  • activeTab: Provides temporary host permissions to the active tab upon user invocation, ideal for one-off actions.
  • scripting: Enables programmatic injection of JavaScript and CSS.

Always use the least privileged approach to ensure security and user trust. Next, we'll explore secure coding practices!

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

บทเรียน “ทำความเข้าใจสิทธิ์ขั้นสูง” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ทำความเข้าใจสิทธิ์ขั้นสูง”

สำรวจสิทธิ์ที่มีความละเอียดอ่อน เช่น `activeTab`, `scripting` และสิทธิ์โฮสต์ พร้อมเรียนรู้ว่าควรขอสิทธิ์เหล่านี้เมื่อใดและอย่างไร คุณปฏิบัติ 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. แนวทางการเขียนโค้ดอย่างปลอดภัย
  3. นโยบายความปลอดภัยของเนื้อหา (CSP)
  4. สิทธิ์เสริมและคำขอขณะทำงาน
← กลับไปที่ Browser Extensions Development (Chrome & Edge)