Greasy Fork

Greasy Fork is available in English.

一键生成直连短链 (快捷键 dd 版)

连续按两次 'd' 触发生成 Is.gd 直连短链,自动跳过输入框

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         一键生成直连短链 (快捷键 dd 版)
// @namespace    https://github.com/
// @version      4.0
// @description  连续按两次 'd' 触发生成 Is.gd 直连短链,自动跳过输入框
// @author       Gemini
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @connect      is.gd
// ==/UserScript==

(function() {
    'use strict';

    let isProcessing = false;
    let lastKeyTime = 0;
    const DOUBLE_PRESS_DELAY = 400; // 两次按键的最大间隔(毫秒)

    async function convertAndCopy() {
        if (isProcessing) return;
        isProcessing = true;
        
        showStatus('⌛ 正在生成...');

        const apiUrl = `https://is.gd/create.php?format=simple&url=${encodeURIComponent(window.location.href)}`;

        GM_xmlhttpRequest({
            method: 'GET',
            url: apiUrl,
            onload: function(response) {
                if (response.status === 200) {
                    const shortUrl = response.responseText.trim();
                    GM_setClipboard(shortUrl);
                    showStatus('✅ 短链已复制: ' + shortUrl);
                } else {
                    showStatus('❌ 失败: ' + response.status);
                }
                isProcessing = false;
            },
            onerror: () => { 
                showStatus('🌐 网络异常 (大陆部分地区受限)'); 
                isProcessing = false;
            }
        });
    }

    // 状态提示逻辑
    function showStatus(msg) {
        let tip = document.getElementById('isgd-status-tip');
        if (!tip) {
            tip = document.createElement('div');
            tip.id = 'isgd-status-tip';
            tip.style.cssText = 'position:fixed;top:20px;left:50%;transform:translateX(-50%);background:#333;color:white;padding:10px 20px;border-radius:20px;z-index:2147483647;font-size:13px;box-shadow:0 4px 12px rgba(0,0,0,0.3);transition: opacity 0.3s;';
            document.body.appendChild(tip);
        }
        tip.innerText = msg;
        tip.style.opacity = '1';
        
        // 自动隐藏逻辑
        clearTimeout(window.isgdTimer);
        window.isgdTimer = setTimeout(() => {
            tip.style.opacity = '0';
            setTimeout(() => tip.remove(), 300);
        }, 2500);
    }

    // 键盘监听逻辑
    window.addEventListener('keydown', function(e) {
        // 1. 冲突回避:如果焦点在输入框或可编辑区域,则不触发
        const activeElem = document.activeElement;
        const isInput = activeElem.tagName === 'INPUT' || 
                        activeElem.tagName === 'TEXTAREA' || 
                        activeElem.isContentEditable;
        
        if (isInput) return;

        // 2. 序列匹配:识别 "dd"
        if (e.key.toLowerCase() === 'd') {
            const currentTime = new Date().getTime();
            if (currentTime - lastKeyTime < DOUBLE_PRESS_DELAY) {
                // 成功触发
                convertAndCopy();
                lastKeyTime = 0; // 重置计数
            } else {
                lastKeyTime = currentTime;
            }
        } else {
            lastKeyTime = 0; // 按了其他键,重置序列
        }
    });

})();