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

การปรับเปลี่ยน DOM ของหน้าเว็บ

จัดการ Document Object Model (DOM) ของหน้าเว็บ โดยเพิ่ม ลบ หรือเปลี่ยนแปลงองค์ประกอบและแอตทริบิวต์ขององค์ประกอบเหล่านั้น

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

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

Intro to DOM Modification

Welcome to modifying web page content! This lesson teaches you how to change what users see on a website using your extension.

We'll dive into the Document Object Model (DOM), which is like a tree structure representing all the elements on a web page. Your extension's content scripts can interact with this tree.

Finding Elements on a Page

Before you can change an element, you need to find it! JavaScript provides several ways to select elements from the DOM.

  • document.querySelector(): Finds the first element matching a CSS selector.
  • document.querySelectorAll(): Finds all elements matching a CSS selector.
  • document.getElementById(): Finds an element by its unique ID.

These are your primary tools for targeting specific parts of a web page.

Accessing Elements: Demo

Let's see how to grab an element. Imagine a web page has a paragraph with the ID 'myParagraph' and several other paragraphs.

Try running this code:

// This code would run in your content script.
// It assumes a page with a <p id="myParagraph"> and other <p> tags.

const specificParagraph = document.getElementById('myParagraph');
const allParagraphs = document.querySelectorAll('p');

if (specificParagraph) {
  console.log('Found specific paragraph:', specificParagraph.textContent);
}

console.log('Total paragraphs on page:', allParagraphs.length);

// For demonstration, we'll change a dummy element if it exists.
// In a real scenario, this would modify the live page.
const dummyDiv = document.createElement('div');
dummyDiv.id = 'coddykit-output';
document.body.append(dummyDiv);

if (specificParagraph) {
  dummyDiv.textContent = `Found by ID: ${specificParagraph.textContent}. Total Ps: ${allParagraphs.length}`;
} else {
  dummyDiv.textContent = `No #myParagraph found. Total Ps: ${allParagraphs.length}`;
}

Updating Text Content

Once you have an element, changing its text is straightforward. You can use the textContent property.

textContent sets or returns the text content of the specified node and all its descendants. It's generally preferred over innerText for performance and security.

Changing Text: Demo

Let's update the text of the first heading on a page. This is a common task for extensions that want to add information or warnings.

What do you think this will do to the page's main heading?

// This code would run in your content script.
// It assumes the page has at least one <h1> element.

const mainHeading = document.querySelector('h1');

if (mainHeading) {
  mainHeading.textContent = '🥳 This Heading Was Modified by CoddyKit!';
  mainHeading.style.color = 'blue'; // Add some style for visibility
} else {
  // If no h1, create one to show the effect
  const newHeading = document.createElement('h1');
  newHeading.textContent = '🥳 New Heading from CoddyKit!';
  newHeading.style.color = 'blue';
  document.body.prepend(newHeading);
}

Altering Element Attributes

Elements often have attributes like src for images, href for links, or class for styling. You can change these too!

  • element.setAttribute(name, value): Adds or updates an attribute.
  • element.removeAttribute(name): Removes an attribute.
  • element.classList.add(className): Adds a CSS class.
  • element.classList.remove(className): Removes a CSS class.

Attributes & Classes: Demo

Let's change an image's source and add a class to a paragraph. Imagine you want to replace a placeholder image or highlight certain text.

Try running this example:

// This code would run in your content script.
// It assumes a page has an <img> and a <p> element.

const firstImage = document.querySelector('img');
const firstParagraph = document.querySelector('p');

if (firstImage) {
  firstImage.setAttribute('src', 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
  firstImage.setAttribute('alt', 'CoddyKit Replaced Image');
  firstImage.style.border = '2px solid green'; // Make changes visible
}

if (firstParagraph) {
  firstParagraph.classList.add('coddykit-highlight');
  // Add some inline style for the added class for visibility
  firstParagraph.style.backgroundColor = '#ffff0050'; // Light yellow tint
  firstParagraph.style.padding = '5px';
  firstParagraph.textContent += ' (Highlighted by CoddyKit!)';
} else {
  const newP = document.createElement('p');
  newP.textContent = 'This is a new paragraph with a highlight!';
  newP.classList.add('coddykit-highlight');
  newP.style.backgroundColor = '#ffff0050';
  newP.style.padding = '5px';
  document.body.append(newP);
}

Creating & Adding New Elements

Extensions can also inject entirely new content into a page! This is how you might add custom buttons, sidebars, or notifications.

  • document.createElement(tagName): Creates a new element node.
  • parentNode.appendChild(childNode): Adds a node as the last child of a parent.
  • parentNode.prepend(childNode): Adds a node as the first child of a parent.
  • parentNode.insertBefore(newNode, referenceNode): Inserts a node before a reference node.

Injecting Elements: Demo

Let's create a new div element and add it to the very top of the web page's body.

This demonstrates how an extension can seamlessly integrate new UI elements.

// This code would run in your content script.

const newDiv = document.createElement('div');
newDiv.textContent = '👋 Hello from your CoddyKit Extension!';
newDiv.style.backgroundColor = '#ADD8E6'; // Light blue background
newDiv.style.padding = '10px';
newDiv.style.textAlign = 'center';
newDiv.style.borderBottom = '1px solid #87CEEB';
newDiv.style.position = 'sticky';
newDiv.style.top = '0';
newDiv.style.width = '100%';
newDiv.style.zIndex = '9999';

document.body.prepend(newDiv); // Add to the top of the body

// You can also remove elements:
// const elementToRemove = document.querySelector('.some-ad-class');
// if (elementToRemove) {
//   elementToRemove.remove();
// }

Quick Check: DOM Actions

Which of the following JavaScript methods can be used to add, remove, or modify elements on a web page?

Recap: Modifying Web Pages

Great job! You've learned the core techniques for interacting with and modifying a web page's structure and content using content scripts.

  • You can find elements using querySelector, querySelectorAll, and getElementById.
  • You can change text with textContent.
  • You can alter attributes and classes with setAttribute, removeAttribute, classList.add, and classList.remove.
  • You can create and inject new elements using createElement and methods like appendChild or prepend.
  • You can remove elements with element.remove().

These powerful tools allow your extension to truly customize the user's browsing experience. Next, we'll explore how to interact with the data on these pages!

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

บทเรียน “การปรับเปลี่ยน DOM ของหน้าเว็บ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การปรับเปลี่ยน DOM ของหน้าเว็บ”

จัดการ Document Object Model (DOM) ของหน้าเว็บ โดยเพิ่ม ลบ หรือเปลี่ยนแปลงองค์ประกอบและแอตทริบิวต์ขององค์ประกอบเหล่านั้น คุณปฏิบัติ Browser Extensions Development (Chrome & Edge) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Browser Extensions Development (Chrome & Edge) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Browser Extensions Development (Chrome & Edge) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การปรับเปลี่ยน DOM ของหน้าเว็บ” ใช้เวลานานแค่ไหน

บทเรียน 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)