Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

How to I setup my chrome extension to notify me if the DOM contents change

  1. User

    How to I setup my chrome extension to notify me if the DOM contents change

  2. Assistant

    To set up your Chrome extension to notify you when the DOM contents change, you can use the MutationObserver API provided by JavaScript. Here's a step-by-step guide to help you:

    1. Create a manifest file named manifest.json with the necessary permissions:
    {
      "name": "DOM Change Notifier",
      "version": "1.0",
      "manifest_version": 2,
      "permissions": [
        "activeTab",
        "storage"
      ],
      "background": {
        "scripts": ["background.js"],
        "persistent": false
      },
      "content_scripts": [
        {
          "matches": ["http://*/*", "https://*/*"],
          "js": ["content.js"]
        }
      ]
    }
    
    1. Create a background script background.js:
    // Background script is responsible for managing the DOM change detection
    
    chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
      if (message === "startObserving") {
        // Start observing DOM changes when requested by the content script
        startObserving();
      } else if (message === "stopObserving") {
        // Stop observing DOM changes when requested by the content script
        stopObserving();
      }
    });
    
    let observer = null;
    
    function startObserving() {
      observer = new MutationObserver(function(mutations) {
        // Notify the content script whenever a DOM change is detected
        chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, { DOMChanged: true });
        });
      });
    
      observer.observe(document, { childList: true, subtree: true });
    }
    
    function stopObserving() {
      if (observer) {
        observer.disconnect();
        observer = null;
      }
    }
    
    1. Create a content script content.js:
    // Content script is responsible for communicating with the background script,
    // capturing DOM changes, and notifying the user accordingly
    
    // Request the background script to start observing DOM changes
    chrome.runtime.sendMessage("startObserving");
    
    // Listen for messages from the background script
    chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
      if (message.DOMChanged) {
        // DOM change detected, perform necessary actions (e.g., display a notification)
        console.log("DOM changed!");
      }
    });
    
    // Optionally, you can also request the background script to stop observing DOM changes when needed:
    // chrome.runtime.sendMessage("stopObserving");
    
    1. Load the extension from the Chrome browser:
      • Open the "Extensions" page by typing chrome://extensions in the address bar.
      • Enable "Developer mode" in the top-right corner if it's not already enabled.
      • Click on the "Load unpacked" button and select the folder containing the manifest file (manifest.json).

    That's it! Your Chrome extension is now set up to notify you whenever the DOM contents change. You can customize the notification logic inside the content script as per your requirements.