Imgur direct image/media page without HTML

Prevents Imgur from redirecting direct media URLs to their HTML page, keeping the correct file extension for images, GIFs and videos. Thanks to /u/american_spacey (https://www.reddit.com/r/firefox/comments/1exswyv/comment/lja7yc7/) for improvements.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Imgur direct image/media page without HTML
// @icon        https://www.google.com/s2/favicons?sz=64&domain=imgur.com
// @description Prevents Imgur from redirecting direct media URLs to their HTML page, keeping the correct file extension for images, GIFs and videos. Thanks to /u/american_spacey (https://www.reddit.com/r/firefox/comments/1exswyv/comment/lja7yc7/) for improvements.
// @include     /^https?:\/\/imgur\.com\/[A-Za-z0-9]+$/
// @include     /^https?:\/\/i\.imgur\.com\/[A-Za-z0-9]+\.(jpeg|png|jpg|gif|mp4)$/
// @exclude     *imgur.com/vidgif
// @exclude     *imgur.com/jobs
// @exclude     *imgur.com/about
// @exclude     *imgur.com/apps
// @exclude     *imgur.com/tos
// @exclude     *imgur.com/privacy
// @exclude     *imgur.com/removalrequest
// @exclude     *imgur.com/advertise
// @exclude     *imgur.com/blog
// @exclude     *imgur.com/random
// @exclude     *imgur.com/search
// @exclude     *imgur.com/*.webm
// @exclude     *imgur.com/upload
// @version     2.0
// @license     MIT
// @grant       none
// @run-at      document-start
// @namespace https://greatest.deepsurf.us/users/1355202
// ==/UserScript==

const contentTypes = ["image/gif", "video/mp4", "image/jpeg", "image/png"];

const fix_page = async function () {
  const currentUrl = window.location.href;
  if (!/^https?:\/\/imgur\.com\/[A-Za-z0-9]+$/.test(currentUrl)) {return;}
  const imageId = currentUrl.match(/imgur\.com\/([A-Za-z0-9]+)/)[1];
  for (const contentType of contentTypes) {
    const extension = contentType.split("/")[1];
    const url = `https://i.imgur.com/${imageId}.${extension}`;
    const response = await fetch(url, {method: "HEAD"});
    if (response.ok && response.headers.get("content-type").split(",").includes(contentType)) {
      window.location.replace(url);
      break;
    }
  }
};

fix_page();