Imgur to Rimgo redirect

Redirect Imgur links to a random Rimgo instance

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Imgur to Rimgo redirect
// @namespace    0b9
// @version      0.1.4
// @description  Redirect Imgur links to a random Rimgo instance
// @author       0b9
// @match        https://imgur.com/*
// @match        https://i.imgur.com/*
// @match        https://imgur.io/*
// @match        https://*.imgur.io/*
// @exclude      https://imgur.com/user/*
// @exclude      https://imgur.com/signin
// @exclude      https://imgur.com/register
// @exclude      https://imgur.com/arcade
// @exclude      https://imgur.com/upload
// @exclude      https://imgur.com/meme-generator
// @exclude      https://imgur.com/privacy
// @exclude      https://imgur.com/rules
// @exclude      https://imgur.com/emerald
// @exclude      https://imgur.com/ccpa
// @exclude      https://imgur.com/tos
// @exclude      https://imgur.com/about
// @exclude      https://imgur.com/apps
// @icon         data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='512' height='512'%3E%3Cpath d='M349.15 398.825v-250m-235.975 13.992h250' fill='none' stroke='%23000' stroke-width='28'/%3E%3Ccircle cx='364.039' cy='147.961' r='34.785' fill='%231e88e5'/%3E%3C/svg%3E
// @grant        GM_xmlhttpRequest
// @connect      rimgo.codeberg.page
// ==/UserScript==

(function () {
  'use strict';

  const apiUrl = 'https://rimgo.codeberg.page/api.json';
  const instancediscovery = 'https://rimgo.codeberg.page';

  const redirectTo = (instanceUrl) => {
    const path = window.location.href.substring(window.location.href.indexOf("/", 8));
    const newUrl = `${instanceUrl}/${path.startsWith("/") ? path.slice(1) : path}`;
    window.location.replace(newUrl);
  };

  GM_xmlhttpRequest({
    method: "GET",
    url: apiUrl,
    onload: function (response) {
      try {
        const data = JSON.parse(response.responseText);
        const eligible = data.clearnet.filter(inst => inst.note.includes("✅ Data not collected"));
        const instance = eligible.length
          ? eligible[Math.floor(Math.random() * eligible.length)].url
          : instancediscovery;
        redirectTo(instance);
      } catch (e) {
        console.error("JSON parsing failed, redirecting to Rimgo instace list page", e);
        redirectTo(instancediscovery);
      }
    },
    onerror: function (err) {
      console.error("Request failed, redirecting to Rimgo instace list page", err);
      redirectTo(instancediscovery);
    }
  });
})();