Chrome extensions are powerful tools that allow developers to enhance the browsing experience by adding custom functionalities to websites. One common use case is the injection of scripts into web pages, and the executeScript
API in Chrome extensions makes this process seamless.
Overview
In this example, we’ll create a Chrome extension named “Coded Brainy executeScript Extension” to demonstrate the usage of executeScript
. The extension will inject a script to rotate the Google logo on the active tab when a button is clicked in the extension popup.
Overview
In this example, we’ll create a Chrome extension named “Coded Brainy executeScript Extension” to demonstrate the usage of executeScript
. The extension will inject a script to rotate the Google logo on the active tab when a button is clicked in the extension popup.
Manifest File (manifest.json)
The manifest file defines the extension’s properties, including the name, version, description, and permissions. It also specifies the background service worker and the action popup.
{ | |
"manifest_version": 3, | |
"name": "Coded Brainy executeScript Extension", | |
"version": "1.0", | |
"description": "Chrome Extension Example to show executeScript usage", | |
"background": { | |
"service_worker":"worker.js" | |
}, | |
"permissions": ["scripting", "activeTab"], | |
"action": { | |
"default_popup": "popup.html", | |
"default_icon": { | |
"16": "images/icon16.png", | |
"48": "images/icon48.png", | |
"128": "images/icon128.png" | |
} | |
} | |
} | |
In this manifest, we’ve set up a service worker (worker.js
) as the background script and specified the necessary permissions for scripting and accessing the active tab.
Background Script (worker.js)
The background script listens for messages from the popup and executes the execute-script
type, injecting a content script into the active tab.
chrome.runtime.onMessage.addListener(async (msg, sender, sendResponse) => { | |
if(msg.type === 'execute-script') { | |
// Get the current tab. | |
const tabs = await chrome.tabs.query({ active: !0, currentWindow: !0 }); | |
const tabId = tabs[0]; | |
await chrome.scripting.executeScript({ | |
target: { tabId: tabId.id }, | |
function: () => { | |
// Apply the style change in the current tab | |
const logo = document.querySelector('img[alt="Google"]'); | |
if (logo) { | |
logo.style.transition = 'transform 2s'; | |
logo.style.transform = 'rotate(360deg)'; | |
} | |
} | |
}); | |
} | |
sendResponse(true); | |
}) |
This script defines an event listener for messages and, upon receiving a message of type execute-script
, retrieves the active tab and injects a content script using executeScript
.
Popup Script (popup.js)
The popup consists of a button triggering the execution of the content script.
// Function to send message to worker.js | |
var listData = async () => { | |
chrome.runtime.sendMessage({type:'execute-script'},function(resp){ | |
window.close(); | |
}); | |
}; | |
document.addEventListener('DOMContentLoaded', function () { | |
// Button event listeners | |
document.getElementById("execBtn").addEventListener("click", listData); | |
}); |
This script sends a message to the background script when the button is clicked, triggering the execution of the content script.
Conclusion
The example extension showcases the usage of executeScript
to dynamically modify a web page. While this specific demonstration rotates the Google logo, the executeScript
API provides a powerful mechanism for injecting custom scripts and styles, opening up endless possibilities for enhancing the user experience in Chrome extensions.
Feel free to customize and expand upon this example to suit your specific needs. Remember to adhere to Chrome’s extension development guidelines and seek appropriate permissions when modifying website content.
Creating Chrome extensions with executeScript
empowers developers to build feature-rich and interactive browser extensions. As you explore and experiment, you’ll discover numerous ways to leverage this API for creating unique and valuable extensions.
The source code for this project is available on Github.