Below is a ready-to-paste Tampermonkey (or Violentmonkey/Greasemonkey) userscript.
Drop it into a new script, adjust the mapping object and you’re done 🚀
// ==UserScript==
// @name IMG Source Replacer
// @namespace https://your-domain.example/
// @version 0.1
// @description Replace <img> sources according to a mapping table
// @author You
// @match *://*/* // ➜ run on every site; narrow it if needed
// @grant none
// ==/UserScript==
(() => {
'use strict';
/********************************************************************
* 1. Configure your mapping here
* – key : ORIGINAL src (full URL or exact string in the src)
* – value : NEW src you want to replace it with
********************************************************************/
const imgMap = {
// 'original-url' : 'replacement-url'
'https://example.com/old1.jpg': 'https://cdn.example.com/new1.jpg',
'old-logo.png' : 'https://i.imgur.com/NEWlogo.png',
// add as many lines as you need …
};
/* *******************************************************************
* 2. Helper: replaces one <img> element if its src matches
********************************************************************/
function replaceImg(img) {
const currentSrc = img.getAttribute('src'); // exact value in DOM
if (currentSrc && imgMap.hasOwnProperty(currentSrc)) {
img.setAttribute('src', imgMap[currentSrc]);
}
}
/* *******************************************************************
* 3. Run once for everything that’s already on the page
********************************************************************/
function replaceExisting() {
document.querySelectorAll('img').forEach(replaceImg);
}
/* *******************************************************************
* 4. Observe DOM so late-loaded images or SPA navigation are covered
********************************************************************/
function observeDOM() {
const observer = new MutationObserver(mutations => {
for (const m of mutations) {
// New nodes
if (m.type === 'childList') {
m.addedNodes.forEach(node => {
if (node.nodeType !== 1) return;
if (node.tagName === 'IMG') {
replaceImg(node); // single <img>
} else {
// query nested images
node.querySelectorAll?.('img').forEach(replaceImg);
}
});
}
// An existing <img> changed its src attribute
if (m.type === 'attributes' && m.attributeName === 'src') {
replaceImg(m.target);
}
}
});
observer.observe(document.body, {
childList : true,
subtree : true,
attributes : true, // watch for src changes
attributeFilter: ['src']
});
}
/* *******************************************************************
* 5. Bootstrap when DOM is ready
********************************************************************/
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
replaceExisting();
observeDOM();
});
} else {
replaceExisting();
observeDOM();
}
})();
How it works (quick recap):
- The imgMap object is your lookup table:
originalSrc → replacementSrc.
replaceExisting() changes every current <img> right after the page finishes loading.
- A single
MutationObserver keeps an eye on:
• new nodes being added to the DOM
• existing images whose src attribute changes
Ensuring images loaded later (e.g., via AJAX, infinite scroll, or single-page-apps) are swapped as well.
Customizing tips:
• Need only to match a filename fragment (e.g., any URL containing “logo.png”)? Replace imgMap.hasOwnProperty(currentSrc) with something like:
Object.entries(imgMap).forEach(([oldSrc, newSrc]) => currentSrc.includes(oldSrc) && img.setAttribute('src', newSrc));
• Want to restrict to certain sites? Change the @match line(s) accordingly.
Happy hacking! 🖼️🔄