微信懒人翻书

实现微信读书自动翻书功能

// ==UserScript==
// @name         微信懒人翻书
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  实现微信读书自动翻书功能
// @author       yuankaiyu
// @match        https://weread.qq.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";
  const box = document.createElement("div");
  const divEle = document.createElement("div");
  var textNode = document.createTextNode("懒人翻书");
  divEle.appendChild(textNode);
  box.style.position = "fixed";
  box.style.top = "24px";
  box.style.left = "600px";
  box.style.fontSize = "18px";
  divEle.style.cursor = "pointer";
  divEle.addEventListener("click", function (event) {
    scrollAuto();
  });
  divEle.onclick = "scrollAuto()";
  box.appendChild(divEle);
  const speedController = document.createElement("div");
  speedController.id = "speedController";
  const minusBtn = document.createElement("button");
  minusBtn.innerText = "-";
  minusBtn.id = "minusBtn";
  minusBtn.style.background = "white";
  minusBtn.style.width = "18px";
  minusBtn.style.height = "18px";
  minusBtn.style.lineHeight = "18px";
  minusBtn.style.marginRight = "5px";
  minusBtn.addEventListener("click", function (event) {
    minus();
  });
  const speedEle = document.createElement("span");
  speedEle.id = "speed";
  speedEle.innerText = "0.5";
  const addBtn = document.createElement("button");
  addBtn.innerText = "+";
  addBtn.id = "addBtn";
  addBtn.style.background = "white";
  addBtn.style.width = "18px";
  addBtn.style.height = "18px";
  addBtn.style.lineHeight = "18px";
  addBtn.style.marginLeft = "5px";
  addBtn.addEventListener("click", function (event) {
    add();
  });
  speedController.appendChild(minusBtn);
  speedController.appendChild(speedEle);
  speedController.appendChild(addBtn);
  box.appendChild(speedController);
  const hint1 = document.createElement("div");
  hint1.innerText = "最小速度0.1";
  const hint2 = document.createElement("div");
  hint2.innerText = "最大速度2";
  const hint3 = document.createElement("div");
  hint3.innerText = "双击按钮可至极值";
  box.appendChild(hint1);
  box.appendChild(hint2);
  box.appendChild(hint3);
  document.body.appendChild(box);
  let intervalId;
  let scrollPosition = 0;
  const scrollSpeed = 20; // 你可以调整这个值来改变滚动速度
  // 为元素添加keydown事件监听器
  document.addEventListener("keydown", function (event) {
    // 检查按下的键是否是空格键
    if (event.key === " " || event.keyCode === 32) {
      event.preventDefault();
      scrollAuto();
    }
  });
  function scrollAuto() {
    const scrollDistance = +speedEle.innerText; // 每次滚动的距离
    const renderTargetContainer = document.querySelector(
      ".renderTargetContainer"
    );
    scrollPosition = window.scrollY;
    // 空格键被按下,执行相应的操作
    if (intervalId) {
      intervalId = clearInterval(intervalId);
    } else {
      // 在这里你可以添加你想要执行的代码
      intervalId = setInterval(() => {
        scrollPosition += scrollDistance;
        window.scrollTo(0, scrollPosition);
        // 当你想要停止滚动时,清除这个间隔
        if (isScrolledToBottom()) {
          console.log("滚动到底部了");
          clearInterval(intervalId);
        }
      }, scrollSpeed);
    }
  }
  function isScrolledToBottom() {
    return window.scrollY + window.innerHeight >= document.body.scrollHeight;
  }

  let timer1 = null;
  let clickCount1 = 0;
  let timer2 = null;
  let clickCount2 = 0;
  let speed = +speedEle.innerText;
  const minus = () => {
    // 每次点击时重置计时器
    clearTimeout(timer1);
    clickCount1++;

    // 如果点击次数为 2,则执行函数
    if (clickCount1 === 2) {
      // 这里放你想要执行的函数
      console.log("双击事件触发!");
      // 重置点击次数和计时器
      clickCount1 = 0;
      speed = 0.1;
    } else {
      // 设置计时器,如果用户在一定时间内没有再次点击,则重置点击次数
      timer1 = setTimeout(function () {
        clickCount1 = 0;
      }, 250); // 这里的 1000 是毫秒数,即 1 秒
      if (speed <= 0.1) return;
      speed -= 0.1;
      speed = Math.round(speed * 100) / 100;
    }
    speedEle.innerText = speed;
  };

  const add = () => {
    // 每次点击时重置计时器
    clearTimeout(timer2);
    clickCount2++;

    // 如果点击次数为 2,则执行函数
    if (clickCount2 === 2) {
      // 这里放你想要执行的函数
      console.log("双击事件触发!");
      // 重置点击次数和计时器
      clickCount2 = 0;
      speed = 2;
    } else {
      // 设置计时器,如果用户在一定时间内没有再次点击,则重置点击次数
      timer2 = setTimeout(function () {
        clickCount2 = 0;
      }, 250); // 这里的 100 是毫秒数
      if (speed >= 2) return;
      speed += 0.1;
      speed = Math.round(speed * 100) / 100;
    }
    speedEle.innerText = speed;
  };
  // Your code here...
})();