Greasy Fork

来自缓存

Greasy Fork is available in English.

扇贝单词额外发音快捷键

支持多个页面的发音快捷键:3/c单词发音,4例句发音,5/b真题例句发音

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         扇贝单词额外发音快捷键
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  支持多个页面的发音快捷键:3/c单词发音,4例句发音,5/b真题例句发音
// @author       Jerry_zhao
// @match        https://web.shanbay.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 多页面选择器配置
    const pageSelectors = {
        // 原单词页
        defaultPage: {
            word: 'div[class*="Pronounce_pronounce"] img',
            sentence: 'div[class*="BayTrans_example"] div[class*="index_right"] img',
            exam: 'div[class*="index_main"] div[class*="index_audio"] img'
        },
        // 新单词页
        altPage: {
            word: 'div[class*="index_audioWrap"] img[alt="trumpet"]'
        }
    };

    // 智能元素查找
    function findActiveElements() {
        return {
            // 优先查找默认页面的元素
            word: document.querySelector(pageSelectors.defaultPage.word) ||
                  document.querySelector(pageSelectors.altPage.word),

            // 仅默认页面需要以下元素
            sentence: document.querySelector(pageSelectors.defaultPage.sentence),
            exam: document.querySelector(pageSelectors.defaultPage.exam)
        };
    }

    // 统一快捷键处理
    function handleHotkeys(event) {
        const { word, sentence, exam } = findActiveElements();
        const key = event.key.toLowerCase();

        if (event.repeat) return;

        switch(key) {
            case '3':
            case 'c':
                word?.click();
                break;
            case '4':
                sentence?.click();
                break;
            case '5':
            case 'b':
                exam?.click();
                break;
        }
    }

    // 初始化监听
    function init() {
        document.removeEventListener('keydown', handleHotkeys);
        document.addEventListener('keydown', handleHotkeys);
        console.log('快捷键已更新');
    }

    // 页面加载后启动
    window.addEventListener('load', () => {
        setTimeout(init, 1500);

        new MutationObserver(() => {
            init();
        }).observe(document.body, {
            childList: true,
            subtree: true
        });
    });
})();