Sleazy Fork is available in English.

LushStories.com-去除文章选择、复制限制;添加朗读/听书功能

默认去除文章选择、复制限制;添加 Smart Read 按钮,实现语音朗读/听书功能,Esc-结束朗读;空格-暂定/继续(360安全浏览器急速模式(已测试),Chrome浏览器);

2020-03-09 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         LushStories.com-去除文章选择、复制限制;添加朗读/听书功能
// @name:en      LushStories.com-Enable Copy & Smart Read
// @namespace    yoursatan
// @version      0.0.8
// @description  默认去除文章选择、复制限制;添加 Smart Read 按钮,实现语音朗读/听书功能,Esc-结束朗读;空格-暂定/继续(360安全浏览器急速模式(已测试),Chrome浏览器);
// @description:en    Default to remove the copy restriction of the site:www.lushStories.com;Add a Smart Read button to listen stories(Chrome,Esc to cancel & Space to psuse/resume.).
// @author       yorusatan
// @include      http*://www.lushstories.com/*
// @grant        none
// @require      https://code.jquery.com/jquery-2.1.4.min.js
// @license      MIT License
// ==/UserScript==
// v0.0.8 修复一个格式化bug  ——2020-3-9
// v0.0.7 1,移除 Enable Copy 按钮,“选择、复制,可划词以配合有道翻译”改为默认执行  ——2020-3-9
// v0.0.7 2,实现朗读时当前段落处于选中状态(蓝底白字)  ——2020-3-9
// v0.0.6 完善 Smart Read 功能:Esc-结束朗读;空格-暂定/继续  ——2020-3-8
// v0.0.5 添加 Smart Read 按钮,实现语音朗读/听书功能  ——2020-3-8
// v0.0.4 修改介绍文字 -2020-3-7
// v0.0.3 更新按钮样式,统一按钮风格  ——2020-3-7
// v0.0.2 实现基本功能,点击可选,可复制,可划词使用有道翻译 ——2020-3-4
// v0.0.1 添加 Enable Copy 按钮,更改文章样式  ——2020-3-3
(function() {
  "use strict";

  // 更改网站CSS样式
  $(".blockselect").css({
    "-moz-user-select": "-moz-text",
    "-khtml-user-select": "text",
    "-webkit-user-select": "text",
    "user-select": "text"
  });
  $("#printer").css({
    "-moz-user-select": "-moz-text",
    "-khtml-user-select": "text",
    "-webkit-user-select": "text",
    "user-select": "text"
  });
  $(".onoffswitch").css({
    "-moz-user-select": "-moz-text",
    "-khtml-user-select": "text",
    "-webkit-user-select": "text",
    "user-select": "text"
  });

  // 打包story
  $(".story").wrap("<div id='newCont'></div>");

  // 打包storycontent
  $(".storycontent").wrap("<div id='storycontent'></div>");

  /* 功能改为默认执行;
  // 添加 Enable Copy 按钮
  $("#breadcrumb").append(
    '<a id="btnEnableCopy" type = "button" href = "#" title = "Enable Copy"  style="color:black;background:#e60022; width:150px;display: run-in ;padding: 0 5px;text-align:center ;font: 150% Trebuchet MS; border: 1px solid #e60022;-moz-border-radius: 3px;-webkit-border-radius: 3px;border-radius: 3px; height: 20px;">Enable Copy</a>'
  );
  */

  // 添加 Smart Read 按钮
  $("#breadcrumb").append(
    '<a id="btnSmartRead" type = "button" href = "#" title = "Smart Read"  style="color:black;background:#e60022; width:150px;display: run-in ;margin:0 5px; padding: 0 5px;text-align:center ;font: 150% Trebuchet MS; border: 1px solid #e60022;-moz-border-radius: 3px;-webkit-border-radius: 3px;border-radius: 3px; height: 20px;">Smart Read</a>'
  );

  // hover 事件
  $("#btnEnableCopy").hover(
    function() {
      $("#btnEnableCopy").css({ color: "white" });
    },
    function() {
      $("#btnEnableCopy").css({ color: "black" });
    }
  );
  $("#btnSmartRead").hover(
    function() {
      $("#btnSmartRead").css({ color: "white" });
    },
    function() {
      $("#btnSmartRead").css({ color: "black" });
    }
  );

  // 获取story文本并格式化;
  // 原.story文本
  var storyAll = $(".story").html();

  // 原.storycontent文本
  var story = $(".storycontent").html();

  // 用于存储新格式化文本
  var newStory = "";

  // 将原文本进行格式化,分割存储;
  var storyArr = story
    .replace(/<div class="hc">[\s\S].*<\/div>/gi, "")
    .replace(/<\/?p>/gi, "")
    .replace(/<br\s*\/?>/gi, "\n")
    .replace(/\n(\n)*( )*(\n)*\n/g, "\n")
    .replace(/\&nbsp;/g, "")
    .split(/\n/);

  // 用于存储格式化后,并移除空行文本数组
  var newStoryArr = [];

  // 移除数组空项(文本空行)
  const countPara = storyArr.length;
  for (var i = 0; i < countPara; i++) {
    storyArr[i] = storyArr[i].replace(/\s+/g, " ").trim();
    if (storyArr[i] != "") {
      newStory += "<p>" + storyArr[i] + "</p>";
      newStoryArr.push(storyArr[i]);
    }
  }
  const newCountPara = newStoryArr.length;

  // 解除选择、复制限制,更改默认样式
  $("#sticky-col").remove();
  $(".story").remove();
  $("#newCont").append(storyAll);
  $(".storycontent").remove();
  $("#storycontent").append(newStory);
  $("#newCont").css("width", "125%");
  $("#storycontent").css({
    "font-family": "sans-serif",
    "font-size": "1.5em",
    "line-height": "1.8em",
    "font-weight": 300
  });

  // 用于逐段朗读
  var flag = 0;

  // 朗读
  var speaker = new window.SpeechSynthesisUtterance();
  speaker.rate = 1.24;
  speaker.lang = "en-US";
  speaker.voiceURI = "Microsoft Zira Desktop - English (United States)";

  // 多次尝试再for循环中无法循环朗读,故添加flag步进;利用setInterval进行循环。
  $("#btnSmartRead").click(function() {
    // 朗读文字数组
    var storyAllRead = newStoryArr;

    // 用于文字选中效果
    var range = document.createRange();
    var selection = window.getSelection();

    var reading = setInterval(function() {
      if (!window.speechSynthesis.speaking && flag < newCountPara) {
        speaker.text = storyAllRead[flag];
        window.speechSynthesis.speak(speaker);
        flag += 1;

        // 朗读段落文字选中效果
        var referenceNode = document
          .getElementById("storycontent")
          .childNodes.item(flag - 1);
        range.selectNodeContents(referenceNode);
        selection.removeAllRanges();
        selection.addRange(range);
      } else if (flag >= newCountPara) {

        // 朗读结束
        // window.speechSynthesis.cancel();
        clearInterval(reading);
        selection.removeAllRanges();
        flag = 0;
        alert("The story is finished");
      }
    }, 500);

    // 监听键盘:Esc/F5
    $(document).keyup(function(event) {
      if (event.keyCode == 27 || event.keyCode == 116) {
        window.speechSynthesis.cancel();
        clearInterval(reading);
        selection.removeAllRanges();
        flag = 0;
      }
    });
  });

  // 监听键盘:空格键
  $(document).keyup(function(event) {
    event.preventDefault();
    if (event.keyCode == 32) {
      if (window.speechSynthesis.speaking) {
        window.speechSynthesis.pause();
      }
      if (window.speechSynthesis.paused) {
        window.speechSynthesis.resume();
      }
    }
  });
})();