KCE - Quick Link Preview

Opens a quick link preview with Alt+Click and adds a small pop-up window on Alt+Right-Click.

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

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

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

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

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

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

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

Advertisement:

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

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

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

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

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

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

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

Advertisement:

// ==UserScript==
// @name         KCE - Quick Link Preview
// @namespace    kce_quick_link_preview_userscript
// @version      7.0
// @description  Opens a quick link preview with Alt+Click and adds a small pop-up window on Alt+Right-Click.
// @author       KCE
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';

    const enabled       = GM_getValue("kce_quick_link_preview", true);
    const excludedSites = GM_getValue("kce_quick_link_previewExcludedSites", []);

    if (!enabled || excludedSites.includes(window.location.hostname)) return;

// Scriptin birden fazla kez çalışmasını önlemek için kontrol
    if (window.quickPreviewLoaded) {
        return;
    }
    window.quickPreviewLoaded = true;

    const MODAL_ID = 'quick-preview-modal';
    const POPUP_ID = 'quick-preview-popup';
    const CLOSE_BTN_HTML = `<button style="position:absolute; top:10px; right:10px; padding:8px 16px; background-color:#ff4444; color:#fff; border:none; border-radius:4px; cursor:pointer; z-index:10000;">Kapat</button>`;
    const IFRAME_STYLE = 'width:100%; height:100%; border:none;';

    // Önizleme pencereleri için CSS
    const style = document.createElement('style');
    style.innerHTML = `
        #${MODAL_ID}, #${POPUP_ID} {
            display: none;
            position: fixed;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0,0,0,0.5);
            z-index: 999999;
            border-radius: 8px;
            overflow: hidden;
            border: 2px solid transparent;
            transition: border-color 0.3s ease;
        }
        #${MODAL_ID} {
            top: 7.5%;
            left: 7.5%;
            width: 85%;
            height: 85%;
        }
        #${POPUP_ID} {
            bottom: 20px;
            left: 20px;
            width: 400px;
            height: 300px;
        }
        .loading-border { border-color: #ff9800 !important; } /* Turuncu: Yükleniyor */
        .success-border { border-color: #4caf50 !important; } /* Yeşil: Başarılı */
        .error-border { border-color: #f44336 !important; }   /* Kırmızı: Hata */
    `;
    document.head.appendChild(style);

    // Modalları oluşturma
    const modal = document.createElement('div');
    modal.id = MODAL_ID;
    document.body.appendChild(modal);

    const popup = document.createElement('div');
    popup.id = POPUP_ID;
    document.body.appendChild(popup);

    // Pencereyi temizleme ve kapatma fonksiyonu
    function closePreview(windowElement) {
        windowElement.style.display = 'none';
        windowElement.innerHTML = '';
        // iframesrc'i sıfırlayarak hafızayı temizle
        const iframe = windowElement.querySelector('iframe');
        if (iframe) iframe.src = 'about:blank';
    }

    // Pencere içeriğini yükleme fonksiyonu
    function loadContent(link, windowElement) {
        const currentIframe = windowElement.querySelector('iframe');
        if (currentIframe) currentIframe.remove();

        windowElement.innerHTML = CLOSE_BTN_HTML;
        const iframe = document.createElement('iframe');
        iframe.style.cssText = IFRAME_STYLE;
        iframe.src = link.href;

        windowElement.style.display = 'block';
        windowElement.classList.remove('success-border', 'error-border');
        windowElement.classList.add('loading-border');

        // Yüklenme durumu
        let loadTimeout = setTimeout(() => {
            windowElement.classList.remove('loading-border');
            windowElement.classList.add('error-border');
            iframe.style.display = 'none';
            windowElement.innerHTML = `<div style="padding: 20px; text-align: center; color: #333;">Yükleme Hatası: İçerik engellenmiş olabilir.</div>` + CLOSE_BTN_HTML;
        }, 10000); // 10 saniye sonra hata varsay

        iframe.onload = () => {
            clearTimeout(loadTimeout);
            windowElement.classList.remove('loading-border', 'error-border');
            windowElement.classList.add('success-border');
        };

        iframe.onerror = () => {
            clearTimeout(loadTimeout);
            windowElement.classList.remove('loading-border', 'success-border');
            windowElement.classList.add('error-border');
            iframe.style.display = 'none';
            windowElement.innerHTML = `<div style="padding: 20px; text-align: center; color: #333;">Yükleme Hatası: İçerik engellenmiş olabilir.</div>` + CLOSE_BTN_HTML;
        };

        windowElement.prepend(iframe);
        windowElement.querySelector('button').onclick = () => closePreview(windowElement);
    }

    // Fare olaylarını dinleme
    document.addEventListener('mousedown', (e) => {
        const link = e.target.closest('a');
        if (e.altKey && link && link.href) {
            e.preventDefault();
            e.stopPropagation();

            if (e.button === 0) { // Alt + Sol Tık
                closePreview(popup); // Pop-up'ı kapat
                loadContent(link, modal);
            } else if (e.button === 2) { // Alt + Sağ Tık
                closePreview(modal); // Büyük pencereyi kapat
                loadContent(link, popup);
            }
        } else {
            // Herhangi bir yere tıklandığında pop-up'ı kapat
            if (popup.style.display === 'block' && !popup.contains(e.target)) {
                closePreview(popup);
            }
        }
    }, true);

    // Klavyeyi dinleme (ESC tuşu)
    document.addEventListener('keydown', (e) => {
        if (e.key === 'Escape') {
            closePreview(modal);
            closePreview(popup);
        }
    });

    // Sayfa değiştiğinde pencereleri kapat
    window.addEventListener('beforeunload', () => {
        closePreview(modal);
        closePreview(popup);
    });

})();