linovelib

优化 linovelib 阅读体验

当前为 2022-09-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name linovelib
  3. // @namespace https://github.com/IronKinoko/userscripts/tree/master/packages/linovelib
  4. // @version 1.0.3
  5. // @license MIT
  6. // @description 优化 linovelib 阅读体验
  7. // @author IronKinoko
  8. // @match https://w.linovelib.com/*
  9. // @icon https://www.google.com/s2/favicons?domain=w.linovelib.com
  10. // @grant none
  11. // @noframes
  12. // ==/UserScript==
  13. (function () {
  14. 'use strict';
  15.  
  16. function isMobile() {
  17. const re = /iphone|ipad|ipod|android|webos|blackberry|windows phone/i;
  18. const ua = navigator.userAgent;
  19. return re.test(ua);
  20. }
  21.  
  22. main();
  23. async function main() {
  24. if (!isMobile())
  25. return;
  26. if (window.ReadTools) {
  27. resetPageEvent();
  28. injectMovePageEvent();
  29. }
  30. }
  31. function resetPageEvent() {
  32. const $body = document.body;
  33. $body.onclick = (e) => {
  34. const toolsId = ["#toptools", "#bottomtools", "#readset"];
  35. if (toolsId.some((id) => {
  36. var _a;
  37. return (_a = document.querySelector(id)) == null ? void 0 : _a.contains(e.target);
  38. })) {
  39. return;
  40. }
  41. window.ReadPages.PageClick();
  42. };
  43. }
  44. function injectMovePageEvent() {
  45. let left, startX, startY, diffX, startTime, isMoved, direction;
  46. const $page = document.getElementById("apage");
  47. const isDisabled = (e) => {
  48. return window.ReadTools.pagemid != 1 || e.touches.length > 1 || window.visualViewport.scale !== 1;
  49. };
  50. window.addEventListener("touchstart", (e) => {
  51. if (isDisabled(e))
  52. return;
  53. left = parseFloat($page.style.left.replace("px", "")) || 0;
  54. startX = e.touches[0].clientX;
  55. startY = e.touches[0].clientY;
  56. startTime = Date.now();
  57. isMoved = false;
  58. direction = "";
  59. });
  60. window.addEventListener("touchmove", (e) => {
  61. if (isDisabled(e))
  62. return;
  63. isMoved = true;
  64. diffX = e.touches[0].clientX - startX;
  65. let diffY = e.touches[0].clientY - startY;
  66. if (direction === "") {
  67. direction = Math.abs(diffX) > Math.abs(diffY) ? "x" : "y";
  68. }
  69. if (direction === "x") {
  70. e.preventDefault();
  71. $page.style.left = left + diffX + "px";
  72. $page.style.transition = "initail";
  73. }
  74. }, { passive: false });
  75. window.addEventListener("touchend", (e) => {
  76. if (isDisabled(e))
  77. return;
  78. if (!isMoved || direction === "y")
  79. return;
  80. const diffTime = Date.now() - startTime;
  81. const threshold = diffTime < 300 ? 10 : document.documentElement.clientWidth * 0.3;
  82. $page.style.transition = "";
  83. if (Math.abs(diffX) > threshold) {
  84. const type = diffX > 0 ? "previous" : "next";
  85. window.ReadPages.ShowPage(type);
  86. if (window.ReadPages.currentPage > window.ReadPages.totalPages || window.ReadPages.currentPage < 1) {
  87. window.ReadPages.ShowPage();
  88. }
  89. } else {
  90. window.ReadPages.ShowPage();
  91. }
  92. });
  93. }
  94.  
  95. })();