Search code examples
javascriptimageurlgreasemonkeytampermonkey

Way to remove part of web page's URL or simply redirect to altered URL in Tampermonkey?


For my job, I have to download tons of images, some of which contain the ".weblp" file extension.

Basically, I go to a site and open up different images in different tabs. Many of which follow this format:

https://example/example.jpg.webp

I merely want to automatically remove ".webp" so I can download the images as .jpg files. I'm not sure if there's a way to automatically load the page sans ".webp" or if it would need to load and then refresh. Either way is fine. So the end result would look like:

https://example/example.jpg

I think it can be done in Javascript with Tampermonkey using regular expressions, but I'm not sure where to start. Or maybe the site can simply be redirected to the URL without the ".webp"?

Is there a way to do this in Tampermonkey? Any and all help would be deeply appreciated.


Solution

  • It is easy enough to change the img src from https://example/example.jpg.webp to https://example/example.jpg however, the image will show ONLY if https://example/example.jpg actually exists.

    If the web site has both .webp & .jpg versions that can be loaded, it will work, otherwise it wont.

    You need to check that first.

    If both are available, then here is a basic example:

    // ==UserScript==
    // @name          SomeJS
    // @match         https://example.com/*
    // @author        erosman
    // @version       1.0
    // ==/UserScript==
    
    document.querySelectorAll('img[src$=".jpg.webp"]').forEach(item => 
        item.src = item.src.replace('.webp', ''));