Greasy Fork

Greasy Fork is available in English.

Cambridge Dictionary Quick Link (Icon + Shortcut)

Double click to show icon; Alt+Double click to go directly to Cambridge Dictionary (English-Chinese Traditional).

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Cambridge Dictionary Quick Link (Icon + Shortcut)
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Double click to show icon; Alt+Double click to go directly to Cambridge Dictionary (English-Chinese Traditional).
// @author       Gemini
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const ICON_ID = 'cambridge-quick-link-icon-final';
    // 使用 Google Favicon 服務獲取劍橋詞典的高畫質圖示
    // Use Google Favicon service to get the high-quality Cambridge Dictionary icon
    const ICON_URL = 'https://www.google.com/s2/favicons?domain=dictionary.cambridge.org&sz=64';

    function removeIcon() {
        const existingIcon = document.getElementById(ICON_ID);
        if (existingIcon) {
            existingIcon.remove();
        }
    }

    // 點擊其他地方時移除圖示
    // Remove icon when clicking elsewhere
    document.addEventListener('mousedown', function(e) {
        const icon = document.getElementById(ICON_ID);
        // 如果點擊的不是圖示本身,就將其移除
        // If the click is not on the icon itself, remove it
        if (icon && !icon.contains(e.target)) {
            removeIcon();
        }
    });

    document.addEventListener('dblclick', function(e) {
        let selectedText = window.getSelection().toString().trim();

        // 檢查是否為英文
        // Check if it is English
        if (selectedText && /^[a-zA-Z-]+$/.test(selectedText)) {

            // --- 新增功能:如果按住了 Alt 鍵 (New Feature: If Alt key is held down) ---
            if (e.altKey) {
                // 直接打開網址,不顯示圖示
                // Open URL directly, do not show icon
                const directUrl = `https://dictionary.cambridge.org/dictionary/english-chinese-traditional/${selectedText}`;
                window.open(directUrl, '_blank');
                return; // 結束函式,不再執行下方的顯示圖示代碼 End function, skip showing icon below
            }

            // --- 原有功能:顯示圖示 (Original Feature: Show Icon) ---

            removeIcon();

            const link = document.createElement('a');
            link.id = ICON_ID;
            link.href = `https://dictionary.cambridge.org/dictionary/english-chinese-traditional/${selectedText}`;
            link.target = '_blank';

            const img = document.createElement('img');
            img.src = ICON_URL;
            img.alt = 'Dict';

            // 樣式設定
            // Style settings
            Object.assign(img.style, {
                width: '24px',
                height: '24px',
                display: 'block',
                borderRadius: '4px'
            });

            link.appendChild(img);

            Object.assign(link.style, {
                position: 'absolute',
                left: `${e.pageX + 8}px`,
                top: `${e.pageY + 8}px`,
                zIndex: '2147483647',
                cursor: 'pointer',
                textDecoration: 'none',
                border: 'none',
                outline: 'none',
                backgroundColor: '#fff',
                padding: '2px',
                borderRadius: '5px',
                boxShadow: '0 2px 5px rgba(0,0,0,0.3)',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center'
            });

            document.body.appendChild(link);
        }
    });
})();