Okay, let's extend the script to allow you to specify CSS selectors and the background image URLs they should use.
We'll add a new mapping object, backgroundMap, where keys are CSS selectors (e.g., #myDiv, .myClass, p) and values are the new background image URLs.
// ==UserScript==
// @name Replace Images and Set Backgrounds
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Replace image sources based on a mapping and set background images for selected elements.
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// =================================================================================
// Configuration Section
// =================================================================================
// Define your image source replacements: old src -> new src
const imageSrcMap = {
"https://example.com/old-image1.jpg": "https://example.com/new-image1.jpg",
"https://example.com/old-image2.png": "https://example.com/new-image2.png",
// Add more image source mappings as needed
// "OLD_IMAGE_URL_FULL_PATH_OR_PARTIAL": "NEW_IMAGE_URL_FULL_PATH",
};
// Define elements to have background images: CSS_SELECTOR -> new_background_image_url
const backgroundMap = {
"#header-banner": "https://example.com/new-header-bg.jpg",
".product-card": "https://example.com/default-product-bg.png",
"div[data-promo='special']": "https://example.com/promo-bg.gif",
// Add more background mappings as needed
// "YOUR_CSS_SELECTOR": "NEW_BACKGROUND_IMAGE_URL",
};
// =================================================================================
// Core Logic Section
// =================================================================================
/**
* Replaces the 'src' attribute of <img> elements based on the imageSrcMap.
*/
function replaceImageSources() {
const images = document.querySelectorAll('img');
images.forEach(img => {
// Check if the current image src is in our map
// You might want to use img.src.includes() for partial matches
// or new URL(img.src).pathname for path-only matching
if (img.src in imageSrcMap) {
img.src = imageSrcMap[img.src];
console.log(`Tampermonkey: Replaced <img> src to ${img.src}`);
}
});
}
/**
* Sets the background-image for elements matching selectors in the backgroundMap.
*/
function setBackgroundImages() {
for (const selector in backgroundMap) {
const elements = document.querySelectorAll(selector);
const newImageUrl = backgroundMap[selector];
elements.forEach(el => {
// Set the background-image style directly
el.style.backgroundImage = `url('${newImageUrl}')`;
// Optional: Add more background properties for better control
// el.style.backgroundSize = 'cover'; // Makes image cover the element
// el.style.backgroundRepeat = 'no-repeat'; // Prevents image from repeating
// el.style.backgroundPosition = 'center'; // Centers the background image
console.log(`Tampermonkey: Set background for '${selector}' to ${newImageUrl}`);
});
}
}
/**
* Runs all replacement functions.
*/
function runAllReplacements() {
replaceImageSources();
setBackgroundImages();
}
// Run on page load
window.addEventListener('load', runAllReplacements);
// Optional: Also run after DOM changes (important for dynamically loaded content)
// This observer will re-run the replacements whenever new elements are added to the DOM.
const observer = new MutationObserver(runAllReplacements);
observer.observe(document.body, { childList: true, subtree: true });
// You might also want to run it immediately on script execution if elements are present before 'load'
runAllReplacements();
})();