Imgur to Rimgo redirect

Redirect Imgur links to a random Rimgo instance

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
    }
  });
})();