My Gyazo Redirector

Gyazo Redirector with Retry and Delay

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        My Gyazo Redirector
// @namespace   Violentmonkey Scripts
// @match       https://gyazo.com/*
// @grant       none
// @version     1.2.1
// @description Gyazo Redirector with Retry and Delay
// @icon https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://gyazo.com&size=32
// ==/UserScript==

'use strict';

(() => {
  const MAX_RETRIES = 3;
  const REDIRECT_DELAY = 100;
  let retryCount = 0;

  const overlay = (() => {
    const div = document.createElement('div');
    Object.assign(div.style, {
      position: 'fixed', top: '0', left: '0', width: '100%',
      height: '100%', backgroundColor: '#121212',
      zIndex: '999999', pointerEvents: 'none',
    });
    return div;
  })();

  const detectAndRedirect = () => {
    const media = document.querySelector('video source') || document.querySelector('img.image-viewer');
    if (media) {
      console.log(`Redirecting to: ${media.src}`);
      setTimeout(() => (globalThis.location.href = media.src), REDIRECT_DELAY);
      return true;
    }
    return false;
  };

  const retryOrReload = () => {
    if (retryCount++ < MAX_RETRIES) {
      console.warn(`Retrying (${retryCount}/${MAX_RETRIES})...`);
      setTimeout(main, 1000);
    } else {
      console.error('Max retries reached, reloading.');
      globalThis.location.reload();
    }
  };

  const main = () => {
    if (!document.body.contains(overlay)) document.body.appendChild(overlay);
    if (!detectAndRedirect()) {
      new MutationObserver(() => {
        if (detectAndRedirect()) this.disconnect();
      }).observe(document.body, { childList: true, subtree: true });
    } else {
      overlay.remove();
    }
  };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', main);
  } else {
    main();
  }
})();