Greasy Fork

Greasy Fork is available in English.

选中自动朗读

任意网页选中自动朗读

当前为 2023-10-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         选中自动朗读
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  任意网页选中自动朗读
// @license     选中自动朗读
// @author       lgldlk
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chrome.com
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @sandbox      JavaScript
// ==/UserScript==

// 定义一个防抖函数
function debounce(fn, delay) {
  let timeout;
  return function () {
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      fn.apply(this, arguments);
    }, delay);
  };
}

(function () {
  'use strict';

  var menu_ALL = [
      ['menu_disable', '🟢 已启用 (点击对当前网站禁用)', '🔴 已禁用 (点击对当前网站启用)', []],
      ['menu_onlyChinese', '朗读所有语言(点击切换朗读中文之外的语言)', '朗读中文之外的语言(点击切换朗读所有语言) ', true],
    ],
    menu_ID = [];

  let isDisable = menu_disable('check');
  let allLanguage = GM_getValue('menu_onlyChinese') ?? menu_value('menu_onlyChinese');
  let limitTextLength = GM_getValue('menu_limitTextLength') ?? 500; // 超过多少字不朗读
  // 菜单开关
  function menu_switch(menu_status, Name, Tips) {
    if (menu_status == 'true') {
      GM_setValue(`${Name}`, false);
    } else {
      GM_setValue(`${Name}`, true);
    }
    if (Name == 'menu_onlyChinese') {
      allLanguage = GM_getValue('menu_onlyChinese');
    }

    registerMenuCommand(); // 重新注册脚本菜单
  }

  // 返回菜单值
  function menu_value(menuName) {
    for (let menu of menu_ALL) {
      if (menu[0] == menuName) {
        return menu[3];
      }
    }
  }
  for (let i = 0; i < menu_ALL.length; i++) {
    // 如果读取到的值为 null 就写入默认值
    if (GM_getValue(menu_ALL[i][0]) == null) {
      GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
    }
  }
  registerMenuCommand();
  // 注册脚本菜单
  function registerMenuCommand() {
    if (menu_ID.length != []) {
      for (let i = 0; i < menu_ID.length; i++) {
        GM_unregisterMenuCommand(menu_ID[i]);
      }
    }
    for (let i = 0; i < menu_ALL.length; i++) {
      // 循环注册脚本菜单
      menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
      if (menu_ALL[i][0] === 'menu_disable') {
        // 启用/禁用护眼模式 (当前网站)
        if (menu_disable('check')) {
          // 当前网站是否已存在禁用列表中
          menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][2]}`, function () {
            menu_disable('del');
          });
          return;
        } else {
          menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][1]}`, function () {
            menu_disable('add');
          });
        }
      } else {
        menu_ID[i] = GM_registerMenuCommand(`${menu_ALL[i][3] ? '🟢' : '🔴'} ${menu_ALL[i][1]}`, function () {
          menu_switch(`${menu_ALL[i][3]}`, `${menu_ALL[i][0]}`, `${menu_ALL[i][2]}`);
        });
      }
    }
    menu_ID[menu_ID.length] = GM_registerMenuCommand('📻 限制多少字以上不朗读(目前:' + limitTextLength + ')', function () {
      const aNumber = Number(window.prompt('请输入限制字数~', ''));
      if (isNaN(aNumber)) {
        alert('请输入数字');
        return;
      }
      if (aNumber < 0) {
        alert('请输入大于0的数字');
        return;
      }
      limitTextLength = aNumber;
      GM_setValue('menu_limitTextLength', aNumber);
      registerMenuCommand();
    });
    menu_ID[menu_ID.length] = GM_registerMenuCommand('📬  欢迎提出反馈和建议,我会非常重视您的意见。', function () {
      window.GM_openInTab('http://greasyfork.icu/zh-CN/scripts/471347/feedback', { active: true, insert: true, setParent: true });
    });
  }

  // 启用/禁用护眼模式 (当前网站)
  function menu_disable(type) {
    switch (type) {
      case 'check':
        return check();
      case 'add':
        add();
        break;
      case 'del':
        del();
        break;
    }

    function check() {
      // 存在返回真,不存在返回假
      let websiteList = menu_value('menu_disable'); // 读取网站列表
      if (websiteList.indexOf(location.host) === -1) return false; // 不存在返回假
      return true;
    }

    function add() {
      if (check()) return;
      let websiteList = menu_value('menu_disable'); // 读取网站列表
      websiteList.push(location.host); // 追加网站域名
      GM_setValue('menu_disable', websiteList); // 写入配置
      isDisable = true;
      registerMenuCommand();
    }

    function del() {
      if (!check()) return;
      let websiteList = menu_value('menu_disable'), // 读取网站列表
        index = websiteList.indexOf(location.host);
      websiteList.splice(index, 1); // 删除网站域名
      GM_setValue('menu_disable', websiteList); // 写入配置
      isDisable = false;
      registerMenuCommand();
    }
  }

  const speakFunc = debounce(function () {
    if (isDisable) return;
    let text = String(document.getSelection());
    if (!allLanguage) {
      if (text.match(/[\u4e00-\u9fa5]/g)?.length) return;
    }
    if (!text.length || text.length > limitTextLength) return;
    speechSynthesis.cancel();
    speechSynthesis.speak(new SpeechSynthesisUtterance(text));
  }, 300);
  document.addEventListener('selectionchange', speakFunc);
  /*
  我趣!芥末帅!尖叫!!上勾拳!下勾拳!左勾拳!扫堂腿!回旋踢!蜘蛛 ️吃耳屎,龙卷风 ️摧毁停车场 ️!羚羊蹬,山羊 跳!乌鸦坐飞机✈️!老鼠 走迷宫!大象 踢腿!愤怒 的章鱼 !巨斧砍大树 !彻底疯!彻底疯狂!彻底疯狂!彻底疯狂!彻底疯狂!彻底疯狂!彻底疯狂!彻底疯狂!彻底疯狂!!!!
  🙋‍♂️🎠‼🌭🆒‼😱‼‼👆✅👊‼👇✅👊‼👈✅👊‼🧹⛪🦵‼🪃🦶‼🕷️🕷 ️🌰👂💩▪🌪 ️👊💀🅿 ️‼🦌🦶▪🐐 🦘
  👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊👊
  🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶🦶
  🕸️👅🌪️🚧🦌🦌🛫🐭🏰🐘🦑🪓🤪🤪🤪🤪🤪🤪🤪🤪🤪
   */
})();