Skip to content
The Chat Archive
gpt-4.1-mini-2025-04-14· OpenAI LMArena Human Preference 140K

Create script to tempermonkey plugin. Which will find all <img> and replace source of…

  1. User

    Create script to tempermonkey plugin. Which will find all and replace source of images. Replacment should be based on map<old value, new_value>

  2. Assistant

    Certainly! Below is a simple Tampermonkey userscript that finds all <img> elements on a page and replaces their src attribute based on a predefined mapping object.


    Tampermonkey Script: Replace Image Sources Based on Map

    // ==UserScript==
    // @name         Replace Image Sources
    // @namespace    http://tampermonkey.net/
    // @version      1.0
    // @description  Replace image sources based on a mapping
    // @author       You
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Define your mapping here: old src -> new src
        const srcMap = {
            "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 mappings as needed
        };
    
        // Function to replace image sources
        function replaceImageSources() {
            const images = document.querySelectorAll('img');
            images.forEach(img => {
                if (img.src in srcMap) {
                    img.src = srcMap[img.src];
                    console.log(`Replaced image src: ${img.src}`);
                }
            });
        }
    
        // Run on page load
        window.addEventListener('load', replaceImageSources);
    
        // Optional: Also run after DOM changes (if images load dynamically)
        const observer = new MutationObserver(replaceImageSources);
        observer.observe(document.body, { childList: true, subtree: true });
    })();
    

    How to use:

    1. Install Tampermonkey extension in your browser.
    2. Create a new script and paste the above code.
    3. Modify the srcMap object to include your old image URLs as keys and new image URLs as values.
    4. Save and reload the page where you want the images replaced.

    If you want the matching to be based on partial URLs or other attributes, let me know and I can adjust the script!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.