My Lightshot Redirector

Redirect Lightshot pages to the direct image URL

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         My Lightshot Redirector
// @namespace    Lightshot Scripts
// @match        https://prnt.sc/*
// @grant        none
// @version      1.0
// @description  Redirect Lightshot pages to the direct image URL
// @icon         https://t2.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://prnt.sc&size=128
// ==/UserScript==

'use strict';

(() => {
  const MAX_RETRIES = 5; // Number of retries before giving up
  const REDIRECT_DELAY = 100; // Delay in milliseconds between retries
  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 img = document.querySelector('#screenshot-image'); // Find the image element
    if (img && img.src) {
      console.log(`Redirecting to: ${img.src}`);
      setTimeout(() => (globalThis.location.href = img.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((mutations, observer) => {
        if (detectAndRedirect()) observer.disconnect();
      }).observe(document.body, { childList: true, subtree: true });
    } else {
      overlay.remove();
    }
  };

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