coolenglish-英文-外掛-浮動朗讀按鈕

coolenglish-選取文字後,按浮動按鈕朗讀

// ==UserScript==
// @name         coolenglish-英文-外掛-浮動朗讀按鈕
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  coolenglish-選取文字後,按浮動按鈕朗讀
// @author       issac
// @match        *://*/*
// @license      GPL-3.0 License
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let selectedText = "";

    // 建立浮動按鈕
    const readBtn = document.createElement('button');
    readBtn.textContent = "📢 朗讀";
    Object.assign(readBtn.style, {
        position: "fixed",
        bottom: "20px",
        right: "20px",
        zIndex: 99999,
        padding: "10px",
        background: "#4CAF50",
        color: "#fff",
        border: "none",
        borderRadius: "5px",
        cursor: "pointer",
        display: "none"
    });
    document.body.appendChild(readBtn);

    // 點擊朗讀按鈕
    readBtn.addEventListener('click', () => {
        if (selectedText) {
            const utterance = new SpeechSynthesisUtterance(selectedText);
            utterance.lang = "en-US";
            speechSynthesis.speak(utterance);
        }
    });

    // 監聽文字選取
    document.addEventListener('selectionchange', () => {
        const text = window.getSelection().toString().trim();
        if (text) {
            selectedText = text;
            readBtn.style.display = "block";
        } else {
            selectedText = "";
            readBtn.style.display = "none";
        }
    });

})();