Chrome extension content script
Author: s | 2025-04-25
More Related Answers ; content scripts get url chrome extension; chrome extension content script; chrome extension run script on any page; chrome extension content script add css
Chrome Extension Content Script on
Example Broswer ExtensionThis is an example of a browser extension that demonstrates the basics of working with content scripts, background scripts, and browser actions. The extension changes the background color of all paragraphs () on a webpage.When a webpage is loaded, the content script (content.js) runs and changes the background color of all elements to gray (#CCC).When the user clicks the extension button in the Chrome toolbar, the background script (background.js) sends a message to the content script.The content script receives the message and changes the background color of all elements to purple (#F0C).Filesmanifest.json: The configuration file that defines the extension's permissions, scripts, and behavior.background.js: The background script that listens for the browser action button click and sends a message to the content script.content.js: The content script that manipulates the DOM by changing paragraph colors and listens for messages from the background script.icon.png: The icon for the browser action (not included, you need to add your own icon).Installation and Testing (for Chrome)Clone or Download the repository containing the extension files.Open Chrome and navigate to chrome://extensions/.Enable Developer mode by toggling the switch in the top right corner.Click on "Load unpacked" and select the folder containing the extension files.The extension icon should now appear in the Chrome toolbar.Viewing LogsContent Script Logs: Open the webpage where the content script runs, right-click, and select Inspect to open Developer Tools. Go to the Console tab to see logs from content.js.Background Script Logs: Open chrome://extensions/, find your extension, and click on the "Service worker" link to More Related Answers ; content scripts get url chrome extension; chrome extension content script; chrome extension run script on any page; chrome extension content script add css Chrome extension run content script on ajax change. 0. chrome extensions - Content Scripts on demand. 7. Execute web page js from chrome extension content script. 2. Chrome extensions Content scripts are a powerful feature of Chrome extensions that allow you to interact directly with web pages. This guide will walk you through creating and implementing content scripts in your Chrome extension.What are Content Scripts?Content scripts are JavaScript files that run in the context of web pages. They can read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.If you're new to Chrome extension development, start with our guide on How to Create Your First Chrome Extension.Creating and Implementing Content Scripts1. Create the Content Script FileFirst, create a new file in your extension's directory, typically named content.js.2. Write Your Content ScriptHere's a basic example of a content script:console.log('Content script loaded');// Change the background color of the pagedocument.body.style.backgroundColor = 'lightblue';// Listen for messages from the extensionchrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === 'getPageTitle') { sendResponse({title: document.title}); }});This script logs a message, changes the page's background color, and listens for messages from the extension.3. Register the Content Script in manifest.jsonTo tell Chrome about your content script, register it in your manifest.json file:{ "manifest_version": 3, "name": "My Extension", "version": "1.0", "description": "An extension with a content script", "content_scripts": [ { "matches": [""], "js": ["content.js"] } ]}4. Communicating with Content ScriptsYou can send messages to your content script from other parts of your extension, like background scripts:chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { chrome.tabs.sendMessage(tabs[0].id, {action: "getPageTitle"}, (response) => { console.log('Page title:', response.title); });});Best Practices for Content ScriptsKeep them lightweight: Content scripts shouldComments
Example Broswer ExtensionThis is an example of a browser extension that demonstrates the basics of working with content scripts, background scripts, and browser actions. The extension changes the background color of all paragraphs () on a webpage.When a webpage is loaded, the content script (content.js) runs and changes the background color of all elements to gray (#CCC).When the user clicks the extension button in the Chrome toolbar, the background script (background.js) sends a message to the content script.The content script receives the message and changes the background color of all elements to purple (#F0C).Filesmanifest.json: The configuration file that defines the extension's permissions, scripts, and behavior.background.js: The background script that listens for the browser action button click and sends a message to the content script.content.js: The content script that manipulates the DOM by changing paragraph colors and listens for messages from the background script.icon.png: The icon for the browser action (not included, you need to add your own icon).Installation and Testing (for Chrome)Clone or Download the repository containing the extension files.Open Chrome and navigate to chrome://extensions/.Enable Developer mode by toggling the switch in the top right corner.Click on "Load unpacked" and select the folder containing the extension files.The extension icon should now appear in the Chrome toolbar.Viewing LogsContent Script Logs: Open the webpage where the content script runs, right-click, and select Inspect to open Developer Tools. Go to the Console tab to see logs from content.js.Background Script Logs: Open chrome://extensions/, find your extension, and click on the "Service worker" link to
2025-04-09Content scripts are a powerful feature of Chrome extensions that allow you to interact directly with web pages. This guide will walk you through creating and implementing content scripts in your Chrome extension.What are Content Scripts?Content scripts are JavaScript files that run in the context of web pages. They can read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.If you're new to Chrome extension development, start with our guide on How to Create Your First Chrome Extension.Creating and Implementing Content Scripts1. Create the Content Script FileFirst, create a new file in your extension's directory, typically named content.js.2. Write Your Content ScriptHere's a basic example of a content script:console.log('Content script loaded');// Change the background color of the pagedocument.body.style.backgroundColor = 'lightblue';// Listen for messages from the extensionchrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === 'getPageTitle') { sendResponse({title: document.title}); }});This script logs a message, changes the page's background color, and listens for messages from the extension.3. Register the Content Script in manifest.jsonTo tell Chrome about your content script, register it in your manifest.json file:{ "manifest_version": 3, "name": "My Extension", "version": "1.0", "description": "An extension with a content script", "content_scripts": [ { "matches": [""], "js": ["content.js"] } ]}4. Communicating with Content ScriptsYou can send messages to your content script from other parts of your extension, like background scripts:chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { chrome.tabs.sendMessage(tabs[0].id, {action: "getPageTitle"}, (response) => { console.log('Page title:', response.title); });});Best Practices for Content ScriptsKeep them lightweight: Content scripts should
2025-04-25No YouTube Recommendations Chrome ExtensionOverviewNo YouTube Recommendations is a simple Chrome extension designed to improve your productivity by hiding YouTube's sidebar recommendations. With a single click on the extension icon, you can toggle the visibility of the recommendations on and off, helping you to stay focused on your intended content.FeaturesToggle Recommendations Visibility: Click the extension icon to hide or show YouTube recommendations.Dynamic Content Handling: The extension uses a MutationObserver to handle dynamic content changes on YouTube, ensuring recommendations remain hidden when you navigate between videos.Minimalist Design: The icon indicates the state of the extension (on or off) using a visual cue, making it easy to see whether recommendations are currently visible or blocked.Badge Indicator: A badge with "OFF" text appears when YouTube recommendations are blocked.InstallationClone or Download this repository:git clone Chrome and go to chrome://extensions/.Enable Developer mode by clicking the toggle in the top-right corner.Click on Load unpacked and select the folder containing this repository.The extension will now be added to Chrome.How to UseClick the extension icon in the Chrome toolbar to toggle recommendations on YouTube.When the extension is active (no badge), recommendations are visible.When the badge shows OFF, recommendations are hidden.How It WorksThe extension uses a content.js script to hide or show YouTube's recommendations based on a stored state.A background.js script manages the state and updates the extension's icon and badge text.The content.js script includes a MutationObserver that keeps recommendations hidden even when navigating dynamically on YouTube.LicenseThis project is licensed under the MIT License. See the LICENSE file for details.
2025-04-06Status: ArchivedThis repository has been archived and is no longer maintained.This sample is deprecated. For a Firebase v3.x sample see: Sample Chrome ExtensionThis is a demonstration of using Firebase in a Chrome extension with Firebase v2.x .Extension UsageInstalling the extension adds an icon next to the chrome address bar, which when clicked opens a popup that displays the number of times the icon has been clicked (by all users of the extension).Using Firebase in an ExtensionThe key to using Firebase in a Chrome extension is adding the following content security policy to your manifest.json:{ "content_security_policy": "script-src 'self' object-src 'self'"}Note that:You must use the wildcard domain ( not since internally the Firebase client library may need to connect to other subdomains.You must use https:// when including firebase.js and when referencing any Firebase database URLs, since Chrome extensions don't allow http script includes.Once you've done that, you can use Firebase just as you would in any other web app. See our Getting Started guide for details.LicenseMIT.
2025-04-14