My Gyazo Redirector

Enhanced Gyazo Redirector with Fixes

当前为 2024-12-27 提交的版本,查看 最新版本

// ==UserScript==
// @name        My Gyazo Redirector
// @namespace   Violentmonkey Scripts
// @match       https://gyazo.com/*
// @grant       GM_xmlhttpRequest
// @grant       GM_log
// @version     1.1
// @description Enhanced Gyazo Redirector with Fixes
// ==/UserScript==

(() => {
  const gyazoId = location.pathname.slice(1); // Extract the ID from the URL
  const isValidGyazoId = /^[a-f0-9]{32}$/i.test(gyazoId); // Validate: exactly 32 hex characters
  console.log(`Extracted Gyazo ID: ${gyazoId}`);

  if (!isValidGyazoId) {
    console.error("Invalid Gyazo ID found in the URL. Stopping script.");
    return;
  }

  const possibleUrls = [
    `https://i.gyazo.com/${gyazoId}.mp4`, // Video
    `https://i.gyazo.com/${gyazoId}.gif`, // GIF
  ];

  const checkUrls = (urls) => {
    if (urls.length === 0) {
      console.warn("No matching media URL found. Falling back to OEmbed.");
      fetchOEmbedUrl();
      return;
    }

    const [url, ...remainingUrls] = urls;
    console.log(`Checking URL: ${url}`);
    GM_xmlhttpRequest({
      method: 'HEAD',
      url: url,
      onload: (response) => {
        console.log(`Response for ${url}:`, response.status);
        if (response.status === 200) {
          console.log(`Redirecting to: ${url}`);
          location.replace(url); // Redirect if valid
        } else {
          console.log(`URL not found: ${url}`);
          checkUrls(remainingUrls);
        }
      },
      onerror: () => {
        console.error(`HEAD request failed for ${url}. Trying next.`);
        checkUrls(remainingUrls);
      },
    });
  };

  const fetchOEmbedUrl = () => {
    const oEmbedUrl = `https://api.gyazo.com/api/oembed?url=${encodeURIComponent(location.href)}`;
    console.log(`Fetching OEmbed from: ${oEmbedUrl}`);
    GM_xmlhttpRequest({
      method: 'GET',
      url: oEmbedUrl,
      onload: (response) => {
        try {
          const data = JSON.parse(response.responseText);
          console.log("OEmbed response:", data);
          if (data.url) {
            console.log(`Redirecting to OEmbed URL: ${data.url}`);
            location.replace(data.url);
          } else {
            console.error("No valid media URL found in OEmbed response.");
          }
        } catch (e) {
          console.error("Error parsing OEmbed response:", e);
        }
      },
      onerror: () => {
        console.error("Failed to fetch OEmbed data.");
      },
    });
  };

  checkUrls(possibleUrls);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址