Certainly! Below is the complete content.js file with all the updates. You can copy and paste this code into your project:
// content.js
// Use an Immediately Invoked Function Expression (IIFE) to avoid polluting the global namespace
(function() {
'use strict';
// Check if the content script has already been injected
if (window.hasRun) {
return;
}
window.hasRun = true;
/**
* Function to modify the page content.
* Add your custom logic inside this function.
*/
function modifyPageContent() {
// Example: Change the background color of the page
document.body.style.backgroundColor = '#f0f0f0';
// Example: Add a custom message at the top of the page
const message = document.createElement('div');
message.textContent = 'This is a custom message added by the content script.';
message.style.position = 'fixed';
message.style.top = '0';
message.style.left = '0';
message.style.width = '100%';
message.style.padding = '10px';
message.style.backgroundColor = '#333';
message.style.color = '#fff';
message.style.textAlign = 'center';
message.style.zIndex = '1000';
document.body.appendChild(message);
// Additional content modifications can be added here
}
// Run the modifyPageContent function when the DOM is fully loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', modifyPageContent);
} else {
modifyPageContent();
}
/**
* Listen for messages from the background script or popup.
* You can define actions based on the received command.
*/
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.command === 'modify') {
modifyPageContent();
sendResponse({ status: 'Page modified' });
} else if (request.command === 'reset') {
// Example: Reset the page modifications
location.reload();
sendResponse({ status: 'Page reset' });
}
// Add more commands as needed
});
// Additional functions or event listeners can be added here
})();
Explanation of Updates:
-
IIFE (Immediately Invoked Function Expression): Wrapping the code inside an IIFE ensures that your variables and functions do not conflict with any variables on the web page.
-
window.hasRun Check: Prevents the content script from running multiple times on the same page, which can happen if the script is injected more than once.
-
modifyPageContent Function: This is where you add your custom logic to manipulate the page. The example changes the background color and adds a custom message at the top.
-
DOM Loaded Check: Ensures that the modifyPageContent function runs after the DOM is fully loaded.
-
Message Listener: The content script listens for messages from the background script or popup. Based on the command received, it can execute different functions (e.g., modify, reset).
-
Use of chrome.runtime.onMessage vs browser.runtime.onMessage: Depending on whether you're developing for Chrome or Firefox, you might need to adjust this API. The chrome namespace is used in Chrome, while browser is used in Firefox (with a polyfill available for cross-browser compatibility).
Next Steps:
-
Customize the modifyPageContent Function: Replace or augment the example code with the specific modifications you want to apply to the web page.
-
Add Additional Commands: In the message listener, you can handle additional commands by adding more else if statements.
-
Test the Script: Load your extension into the browser and test to ensure that the content script behaves as expected.
Note: Ensure that your extension has the necessary permissions in the manifest.json file, such as permissions for the sites you want to modify and the tabs permission if needed.
If you have any specific features or updates you'd like to include, feel free to let me know, and I can adjust the code accordingly.