// ==UserScript== // @name 选中自动朗读 // @namespace http://tampermonkey.net/ // @version 0.2 // @description 任意网页选中自动朗读 // @license 选中自动朗读 // @author lgldlk // @match *://*/* // @icon https://www.google.com/s2/favicons?sz=64&domain=chrome.com // @grant none // @downloadURL none // ==/UserScript== // 定义一个防抖函数 function debounce(fn, delay) { let timeout; return function () { clearTimeout(timeout); timeout = setTimeout(() => { fn.apply(this, arguments); }, delay); }; } (function () { 'use strict'; const speakFunc = debounce(function () { let text = String(document.getSelection()); if (!text.length || text.length > 200) return; speechSynthesis.cancel(); speechSynthesis.speak(new SpeechSynthesisUtterance(text)); }, 300); document.addEventListener('selectionchange', speakFunc); })();