Certainly! Below is a Tampermonkey script that allows you to specify a list of image URLs to find on a webpage and replace them with your desired image URLs. You can customize the imageReplacements object by adding your original image URLs as keys and the replacement image URLs as their corresponding values.
Steps to Use:
- Install Tampermonkey: If you haven't already, install the Tampermonkey extension for your browser.
- Create a New Script:
- Open the Tampermonkey dashboard.
- Click on the "Create a new script" button.
- Paste the Script: Replace the contents with the script provided below.
- Customize the Mapping: Edit the
imageReplacements object to include your specific image URL mappings.
- Save the Script: Click on "File" > "Save".
- Navigate to Your Target Webpage: The script will automatically run and replace the specified images.
Tampermonkey Script
// ==UserScript==
// @name Image Replacer
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Replace specified images on a webpage with your own images
// @author Your Name
// @match *://*/*
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
// Mapping of original image URLs to replacement image URLs
const imageReplacements = {
// "Original Image URL": "Replacement Image URL",
"https://images2.imgbox.com/e6/7f/k6eC0b8V_o.jpg": "https://example.com/path/to/your/replacement-image1.jpg",
"https://example.com/path/to/original-image2.png": "https://example.com/path/to/your/replacement-image2.png",
// Add more mappings as needed
};
/**
* Function to replace image sources based on the mapping.
*/
function replaceImages() {
// Select all img elements on the page
const images = document.querySelectorAll('img');
images.forEach((img) => {
// Get the current src of the image
const currentSrc = img.src;
// Check if the current src exists in the mapping
if (imageReplacements.hasOwnProperty(currentSrc)) {
// Get the replacement URL
const newSrc = imageReplacements[currentSrc];
// Replace the src attribute
img.src = newSrc;
// Optionally, replace other related attributes if needed
if (img.dataset.url) {
img.dataset.url = newSrc;
}
if (img.srcset) {
// If srcset is used, you might want to handle it accordingly
// For simplicity, this example replaces the entire srcset
img.srcset = newSrc;
}
console.log(`Replaced image: ${currentSrc} -> ${newSrc}`);
}
});
}
// Run the replaceImages function after the DOM is fully loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', replaceImages);
} else {
replaceImages();
}
// Optional: Observe the DOM for dynamically added images and replace them
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (node.tagName === 'IMG') {
const img = node;
const currentSrc = img.src;
if (imageReplacements.hasOwnProperty(currentSrc)) {
const newSrc = imageReplacements[currentSrc];
img.src = newSrc;
if (img.dataset.url) {
img.dataset.url = newSrc;
}
if (img.srcset) {
img.srcset = newSrc;
}
console.log(`Replaced dynamically added image: ${currentSrc} -> ${newSrc}`);
}
} else if (node.querySelectorAll) {
// If the added node contains img elements
const imgs = node.querySelectorAll('img');
imgs.forEach((img) => {
const currentSrc = img.src;
if (imageReplacements.hasOwnProperty(currentSrc)) {
const newSrc = imageReplacements[currentSrc];
img.src = newSrc;
if (img.dataset.url) {
img.dataset.url = newSrc;
}
if (img.srcset) {
img.srcset = newSrc;
}
console.log(`Replaced dynamically added image: ${currentSrc} -> ${newSrc}`);
}
});
}
});
}
});
});
// Start observing the document body for changes
observer.observe(document.body, { childList: true, subtree: true });
})();
Explanation of the Script
-
Metadata Block: Defines the script's name, description, version, and the webpages it should run on (in this case, all webpages). You can modify the @match line to target specific websites.
-
Image Replacement Mapping:
-
replaceImages Function:
- Selects all
<img> elements on the page.
- Iterates through each image, checks if its
src matches any key in the imageReplacements mapping.
- If a match is found, it replaces the
src, and optionally data-url and srcset attributes with the new image URL.
- Logs the replacement action to the console for debugging purposes.
-
DOM Content Loaded:
- Ensures that the
replaceImages function runs after the DOM is fully loaded.
- This guarantees that all images have been loaded into the DOM before attempting replacements.
-
Mutation Observer (Optional):
- Watches for dynamically added images (e.g., images loaded via AJAX after the initial page load).
- When new images are added to the DOM, it checks and replaces them if they match the mapping.
- This ensures that all relevant images are replaced, even if they appear after the initial page load.
Customization
- Adding More Image Mappings: Simply add more key-value pairs to the
imageReplacements object.
const imageReplacements = {
"original_image_url1": "replacement_image_url1",
"original_image_url2": "replacement_image_url2",
// Add as many as you need
};
- Targeting Specific Websites: Modify the
@match line in the metadata block to restrict the script to specific websites.
// For example, to target only example.com
// @match *://*.example.com/*
Example Based on Your Input
Given your example HTML:
<img src="https://images2.imgbox.com/e6/7f/k6eC0b8V_o.jpg" data-url="https://images2.imgbox.com/e6/7f/k6eC0b8V_o.jpg" class="bbImage" data-zoom-target="1" style="" alt="k6eC0b8V_o.jpg" title="" width="" height="" loading="lazy">
To replace this image with, say, https://example.com/new-image.jpg, add the following entry to the imageReplacements object:
const imageReplacements = {
"https://images2.imgbox.com/e6/7f/k6eC0b8V_o.jpg": "https://example.com/new-image.jpg",
// Add more mappings if needed
};
Final Notes
- Performance Considerations: For webpages with a large number of images, this script runs efficiently by only processing images present in the DOM and those dynamically added.
- Security: Ensure that the replacement images are from trusted sources to avoid introducing malicious content.
- Testing: After setting up the script, visit the target webpage and inspect the console (press
F12 or Ctrl+Shift+I) to see log messages confirming image replacements.
Feel free to modify and extend the script as per your requirements!