Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

make a tampermonkey script to replace the inside image src with "blank" on each instance…

  1. User

    make a tampermonkey script to replace the inside image src with "blank" on each instance of the imgwrapper or imgwrapper_big on every iha.ee page

  2. Assistant

    To create a Tampermonkey script that replaces the src attribute of img tags inside elements with the class imgwrapper or imgwrapper_big on every iha.ee page, you can follow these steps:

    1. Install Tampermonkey extension for your browser if it's not already installed.

    2. Click on the Tampermonkey extension icon and select "Create a new script...".

    3. Replace the default code with the following custom script:

    // ==UserScript==
    // @name         IHA EE Image Replacer
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  Replace image sources in imgwrapper elements on IHA EE pages with a blank image.
    // @author       You
    // @match        *://*.iha.ee/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Select all elements with the class imgwrapper or imgwrapper_big
        const imgWrappers = document.querySelectorAll('.imgwrapper, .imgwrapper_big');
    
        // Define the new src you want to replace with
        const newSrc = 'blank';
    
        // Loop through each element and replace the image src
        imgWrappers.forEach(wrapper => {
            // Get the img tag within the wrapper
            const img = wrapper.querySelector('img');
            // Replace the src attribute
            if (img) {
                img.src = newSrc;
            }
        });
    })();
    
    1. Save the script by clicking on the disk icon or press Ctrl + S (Cmd + S on Mac).

    2. Go to a page on iha.ee to test the script. It should replace the src attribute of images within imgwrapper or imgwrapper_big elements with the string "blank".

    Remember that Tampermonkey scripts can manipulate content on web pages, so ensure that your actions comply with the website's terms of service and do not infringe on any copyrights or terms of use.

    Note: The value 'blank' in newSrc is not a valid URL for an image source and will result in a broken image icon on the page. If you want to use an actual blank image, you'll need to use a valid image URL for a transparent 1x1 pixel, for example:

    const newSrc = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
    

    Replace newSrc in the script with this base64-encoded image if you want to display a transparent image instead of a broken image icon.

  3. User

    you seem to be replacing the background image aswell, the whole purpose of setting it blank was the reveal the background image which the overlay was abstructing.

  4. Assistant

    I apologize for the oversight. If you only want to replace the src attribute of img tags inside .imgwrapper and .imgwrapper_big without affecting the background-image CSS style, you can modify the Tampermonkey script as follows:

    // ==UserScript==
    // @name         IHA EE Image Source Blanker
    // @namespace    http://tampermonkey.net/
    // @version      0.2
    // @description  Replace image sources within imgwrapper elements on IHA EE pages without hiding the background.
    // @author       You
    // @match        *://*.iha.ee/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Select all elements with the class imgwrapper or imgwrapper_big
        const imgWrappers = document.querySelectorAll('.imgwrapper, .imgwrapper_big');
    
        // Loop through each element and set the image src to blank
        imgWrappers.forEach(wrapper => {
            // Find the image tags 'img' within the wrapper
            const images = wrapper.getElementsByTagName('img');
            // Loop through the images and set their src to blank
            for (let img of images) {
                // Set src to an empty string to retain layout but remove the visible image
                img.src = '';
            }
        });
    })();
    

    This script will loop through each element with the class imgwrapper or imgwrapper_big and only replace the src attribute of contained img tags, leaving the background-image style intact.

    By setting the src attribute to an empty string (''), the img tags will not display any image, allowing the background image to be visible.

    Make sure to save the script in Tampermonkey and then refresh the page on iha.ee to see the effects. This will reveal the originally hidden background images without displaying the overlay images that were possibly obstructing the view.

  5. User

    I want it to check if the image wrapper contains a simlar styling with the background image, if so then blank the imagge inside it, not the background image. style="background-image: url('https://img2.iha.ee/249/148637e947209e366961.jpg');"

  6. Assistant

    If you need the Tampermonkey script to compare if the style attribute's background-image URL is similar to the src attribute of the img tag inside .imgwrapper or .imgwrapper_big and blank out the img only if they match, the following script will do that:

    // ==UserScript==
    // @name         IHA EE Conditional Image Source Blanker
    // @namespace    http://tampermonkey.net/
    // @version      0.3
    // @description  Replace image sources within imgwrapper elements on IHA EE pages only when it matches the background-image url.
    // @author       You
    // @match        *://*.iha.ee/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Function to extract background-image URL from the style
        function extractBackgroundImageUrl(style) {
            const match = /url\(['"]?(.*?)['"]?\)/.exec(style);
            return match ? match[1] : null;
        }
    
        // Select all elements with the class imgwrapper or imgwrapper_big
        const imgWrappers = document.querySelectorAll('.imgwrapper, .imgwrapper_big');
    
        // Loop through each element and check for matching background-image and src
        imgWrappers.forEach(wrapper => {
            const style = wrapper.getAttribute('style');
            if (style) {
                const backgroundImageUrl = extractBackgroundImageUrl(style);
                let img = wrapper.querySelector('img');
                if (img && backgroundImageUrl && img.src.endsWith(backgroundImageUrl.split('/').pop())) {
                    // If the image src ends with the same value as the background image, blank the img's src
                    img.src = 'about:blank'; // Use 'about:blank' to effectively remove the image
                } 
            }
        });
    })();
    

    This script checks each .imgwrapper or .imgwrapper_big to see if the background-image URL matches the filename at the end of the img src attribute. If they match, it will set the src attribute of the img to about:blank, which effectively "blanks" the image. The background-image will still be visible.

    Please note that the detection is based on image filenames extracted from URLs, assuming that if the filenames match, then the images are considered similar. If more rigorous checking is needed (e.g., comparing the entire URL), you might need to adjust the comparison logic.

  7. User

    it does not seem to work, what about we try annother approach, take this:

  8. Assistant

    Sure, we will modify the Tampermonkey script to follow these steps:

    1. Extract the background-image URL from the inline style attribute of .imgwrapper_big (and .imgwrapper if necessary).
    2. Remove the style attribute from the wrapper div entirely.
    3. Replace the src attribute of the child img tag with the URL that was originally set as the background-image.

    Here is the updated Tampermonkey script:

    // ==UserScript==
    // @name         IHA EE Image Extractor and Replacer
    // @namespace    http://tampermonkey.net/
    // @version      0.4
    // @description  Extract the background image and use it as the image source within image wrappers on IHA EE pages.
    // @author       You
    // @match        *://*.iha.ee/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Function to extract background-image URL from the style attribute
        function extractBackgroundImageUrl(style) {
            const match = /url\(['"]?(.*?)['"]?\)/.exec(style);
            return match ? match[1] : null;
        }
    
        // Select all elements with the class imgwrapper_big and imgwrapper
        const imgWrappers = document.querySelectorAll('.imgwrapper_big, .imgwrapper');
    
        imgWrappers.forEach(wrapper => {
            // Extract the background-image URL
            const backgroundImageUrl = extractBackgroundImageUrl(wrapper.getAttribute('style'));
    
            // Remove the style attribute
            wrapper.removeAttribute('style');
    
            // Find the child image and set its src to the extracted background-image URL
            const img = wrapper.querySelector('img');
            if (img && backgroundImageUrl) {
                img.src = backgroundImageUrl;
            }
        });
    })();
    

    Please copy and paste this script into a new script in Tampermonkey and then visit a page on iha.ee to see the changes. This script will remove the inline style from imgwrapper_big and imgwrapper elements, and the src of the img tag will then be updated with the URL that was previously specified as the background-image. This change will be made for all matched elements on the page.

  9. User

    you definitelly got the regex wrong, its extracting "('" instead of the background-image url

  10. Assistant

    Apologies for the confusion. It appears the regular expression isn't matching the URL correctly. Let's adjust the regular expression to properly capture the URL from the style attribute.

    Here's the updated Tampermonkey script that should properly extract the background-image URL:

    // ==UserScript==
    // @name         IHA EE Image Extractor and Replacer
    // @namespace    http://tampermonkey.net/
    // @version      0.5
    // @description  Extract the background-image URL and replace the img src within .imgwrapper and .imgwrapper_big on IHA EE pages.
    // @author       You
    // @match        *://*.iha.ee/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Function to extract background-image URL from the style attribute
        function extractBackgroundImageUrl(style) {
            const match = /background-image:\s*url\(['"]?(.+?)['"]?\)/i.exec(style);
            return match ? match[1] : null;
        }
    
        // Select all elements with the class imgwrapper_big and imgwrapper
        const imgWrappers = document.querySelectorAll('.imgwrapper_big, .imgwrapper');
    
        imgWrappers.forEach(wrapper => {
            // Extract the background-image URL
            const backgroundImageUrl = extractBackgroundImageUrl(wrapper.getAttribute('style'));
            
            // Only proceed if backgroundImageUrl is not null
            if (backgroundImageUrl) {
                // Remove the style attribute from the wrapper
                wrapper.removeAttribute('style');
    
                // Find the child image and set its src to the extracted background-image URL
                const img = wrapper.querySelector('img');
                if (img) {
                    img.src = backgroundImageUrl;
                }
            }
        });
    })();
    

    Please load this updated script into your Tampermonkey extension and ensure it is enabled, then navigate to a page on iha.ee to see the results. The script should now be correctly extracting the URL from the background-image property within the inline style attribute, removing the style attribute, and then updating the src of the img tags within .imgwrapper_big and .imgwrapper elements accordingly.

  11. User

    it is still getting it wrong, maybe just extract the string between t he single quotes to simplify

  12. Assistant

    Understood, we can simplify the regular expression to just capture the content inside the single quotes ('...') after url( in the style attribute. Here is the corrected script:

    // ==UserScript==
    // @name         IHA EE Image Extractor and Replacer
    // @namespace    http://tampermonkey.net/
    // @version      0.6
    // @description  Extract the background-image URL and replace the img src within .imgwrapper and .imgwrapper_big on IHA EE pages.
    // @author       You
    // @match        *://*.iha.ee/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Function to extract background-image URL from the style attribute
        function extractBackgroundImageUrl(style) {
            // Simplified regex to capture the content inside single quotes after url(
            const match = /url\('([^']+)'\)/i.exec(style);
            return match ? match[1] : null;
        }
    
        // Select all elements with the class imgwrapper_big and imgwrapper
        const imgWrappers = document.querySelectorAll('.imgwrapper_big, .imgwrapper');
    
        imgWrappers.forEach(wrapper => {
            // Extract the background-image URL
            const backgroundImageUrl = extractBackgroundImageUrl(wrapper.getAttribute('style'));
            
            // Only proceed if backgroundImageUrl is not null
            if (backgroundImageUrl) {
                // Remove the style attribute from the wrapper
                wrapper.removeAttribute('style');
    
                // Find the child image and set its src to the extracted background-image URL
                const img = wrapper.querySelector('img');
                if (img) {
                    img.src = backgroundImageUrl;
                }
            }
        });
    })();
    

    Use this script in Tampermonkey, and it should work as expected when you visit pages on iha.ee. The script will extract the URL from the background-image style and set it as the src attribute for the respective img tags within .imgwrapper_big and .imgwrapper elements.

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