Imgur Direct

See Imgur images & videos directly.

Versione datata 16/10/2020. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
//
// @name           Imgur Direct
// @version        1.0
// @namespace      https://greasyfork.org/en/users/667743-catspinner
// @description    See Imgur images & videos directly.
// @icon           https://imgur.com/favicon.ico
//
// @include        https://imgur.com/*
// @exclude        https://imgur.com/
// @exclude        https://imgur.com/*/*
// @exclude        https://imgur.com/gallery
//
// ==/UserScript==
//
/*=========================  Version History  ==================================

1.00    -    First public release.

==============================================================================*/


function runImgurDirect() {
  const imgname = location.href.replace('https://imgur.com/', 'https://i.imgur.com/'),
        imgname_jpeg = imgname + '.jpeg',
        imgname_png  = imgname + '.png',
        imgname_gif  = imgname + '.gif',
        imgname_mp4  = imgname + '.mp4',
        imgname_gifv = imgname + '.gifv';

  const url = location.href;
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.responseType = 'text';
  xhr.send();
  xhr.onload = function() {
    const page = xhr.response;
    var video = true;
    var trueimage = location.href;

    if (page.indexOf("og:video") == -1) {
      video = false;
    } if (page.indexOf(imgname_png) > -1) {
      trueimage = imgname_png;
    } else if (page.indexOf(imgname_jpeg) > -1) {
      trueimage = imgname_jpeg;
    }

    if (video) {
      if (countOccurrence(page, imgname_gif) > 2) {
        trueimage = imgname_gif;
      } else if (countOccurrence(page, imgname_mp4) > 2) {
        trueimage = imgname_mp4;
      } else if (countOccurrence(page, imgname_gifv) > 2) {
        trueimage = imgname_gifv;
      }
    }

    if (url !== trueimage) {
      window.location.replace(trueimage);
    } else {
      return;
    }
  }
}

function countOccurrence(string, subString) {
  var n    = 0,
      pos  = 0,
      step = subString.length;

  while (true) {
    pos = string.indexOf(subString, pos);
    if (pos >= 0) {
        ++n;
        pos += step;
    } else break;
  }
  return n;
}

runImgurDirect();