微信懒人翻书

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

目前为 2024-06-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         微信懒人翻书
// @namespace    http://tampermonkey.net/
// @version      0.4
// @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";
  box.style.background = "linear-gradient(45deg, rgb(44, 106, 255),rgb(48, 173, 254))";
  box.style.padding = "10px";
  box.style.borderRadius = "5px";
  box.style.boxShadow = "4px 4px 20px rgba(0, 0, 0, 0.4)";
  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.marginRight = "5px";
  minusBtn.style.cursor = "pointer";
  minusBtn.style.color = "white";
  minusBtn.style.fontSize = "24px";
  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.marginLeft = "5px";
  addBtn.style.cursor = "pointer";
  addBtn.style.color = "white";
  addBtn.style.fontSize = "24px";
  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 = "Min 0.1";
  hint1.style.cursor = "pointer";
  hint1.addEventListener("click", function (event) {
    speedEle.innerText = 0.1;
  });
  const hint2 = document.createElement("div");
  hint2.innerText = "Max 2";
  hint2.style.cursor = "pointer";
  hint2.addEventListener("click", function (event) {
    speedEle.innerText = 2;
  });
  box.appendChild(hint1);
  box.appendChild(hint2);
  document.body.appendChild(box);
  let animationFrameId;
  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;
    function smoothScroll() {
      cancelAnimationFrame(animationFrameId);
      scrollPosition += scrollDistance;
      window.scrollTo(0, scrollPosition);
      // 当你想要停止滚动时,清除这个间隔
      if (isScrolledToBottom()) {
        console.log("滚动到底部了");
        cancelAnimationFrame(animationFrameId);
        return
      }
      animationFrameId = requestAnimationFrame(smoothScroll)
    }
    // 空格键被按下,执行相应的操作
    if (animationFrameId) {
      console.log('清除')
      animationFrameId = cancelAnimationFrame(animationFrameId);
    } else {
      // 在这里你可以添加你想要执行的代码
      animationFrameId = requestAnimationFrame(smoothScroll);
    }
  }
  function isScrolledToBottom() {
    return window.scrollY + window.innerHeight >= document.body.scrollHeight;
  }

  const minus = () => {
    let speed = +speedEle.innerText;
    speed -= 0.1;
    speed = Math.round(speed * 100) / 100;
    speedEle.innerText = speed;
  };

  const add = () => {
    let speed = +speedEle.innerText;
    speed += 0.1;
    speed = Math.round(speed * 100) / 100;
    speedEle.innerText = speed;
  };
  // 键盘按下加减键
  document.addEventListener('keydown', function(event) {
  // 检查按下的键的键码
  if (event.key === '+') {
    console.log('加号键被按下');
    add();
    // 这里可以添加加号键的响应逻辑
  } else if (event.key === '-') {
    console.log('减号键被按下');
    minus();
    // 这里可以添加减号键的响应逻辑
  }
});
  // Your code here...
})();