Creating a Firefox extension that listens for user manual clicks on links, tests these links for a specific pattern, and then redirects to a modified URL if the pattern matches involves writing a content script that intercepts click events and a background script for managing redirections. Here's a step-by-step guide to achieve this functionality.
Step 1: Define Your Extension Structure
Your extension will comprise at least three files:
manifest.json: Extension's metadata and permissions.
background.js: Background script for handling redirections.
content.js: Content script for intercepting clicks.
Step 2: Setup the Manifest File
Create manifest.json with necessary permissions for web navigation and active tab manipulation.
{
"manifest_version": 3,
"name": "Selective Link Redirector",
"version": "1.0",
"description": "Intercepts specific link clicks and redirects if necessary.",
"permissions": [
"webNavigation",
"activeTab"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/icon-16.png",
"48": "images/icon-48.png",
"128": "images/icon-128.png"
}
}
}
Adjust your permissions and files as needed. Note that manifest_version 3 is used here, which is the latest at the time of writing.
Step 3: Create the Content Script
The content script, content.js, will listen for clicks on links and message the background script if a link matching certain criteria is clicked.
document.addEventListener('click', (event) => {
let target = event.target;
// Ensuring we only capture clicks on 'a' elements
if (target.tagName !== 'A') {
target = target.closest('a');
if (!target) return;
}
const href = target.href;
// Checking if the link matches our pattern (example)
if (href.includes("example.com")) {
event.preventDefault(); // Prevent default action
// Sending message to the background script with the URL
browser.runtime.sendMessage({redirect: href});
}
});
Step 4: Handling Redirects in the Background Script
In background.js, listen for messages from the content script and execute the redirection.
browser.runtime.onMessage.addListener((message) => {
if (message.redirect) {
const url = new URL(message.redirect);
// Here we change the subdomain as required, e.g., from www to sub
const newUrl = url.href.replace("//www.", "//sub.");
// Redirect the current tab to the new URL
browser.tabs.update({url: newUrl});
}
});
Step 5: Testing Your Extension
- Open Firefox and navigate to
about:debugging.
- Click "This Firefox" (or "Load Temporary Add-on" in older versions).
- Select your
manifest.json file to load the extension.
- Now navigate to pages and click on links that match your defined pattern to test the redirection.
Considerations
- Be mindful of privacy and security. Only intercept and manipulate user actions when absolutely necessary, and always keep user consent and transparency in mind.
- The example provided here is basic. Depending on your exact requirements (such as which subdomains or patterns to match), you may need to adjust the code, especially the logic in
content.js for matching links and in background.js for altering URLs.
- Testing on various websites and configurations is crucial, as real-world scenarios may introduce complexities not covered in simple examples.