linovelib

优化 linovelib 阅读体验

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

  1. // ==UserScript==
  2. // @name linovelib
  3. // @namespace https://github.com/IronKinoko/userscripts/tree/master/packages/linovelib
  4. // @version 1.4.6
  5. // @license MIT
  6. // @description 优化 linovelib 阅读体验
  7. // @author IronKinoko
  8. // @match https://www.linovelib.com/*
  9. // @match https://w.linovelib.com/*
  10. // @icon https://www.google.com/s2/favicons?domain=w.linovelib.com
  11. // @grant none
  12. // @noframes
  13. // ==/UserScript==
  14. (function () {
  15. 'use strict';
  16.  
  17. function isMobile() {
  18. const re = /iphone|ipad|ipod|android|webos|blackberry|windows phone/i;
  19. const ua = navigator.userAgent;
  20. return re.test(ua);
  21. }
  22.  
  23. function normalizeKeyEvent(e) {
  24. const SPECIAL_KEY_EN = "`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?".split("");
  25. const SPECIAL_KEY_ZH = "\xB7-=\u3010\u3011\u3001\uFF1B\u2018\uFF0C\u3002/\uFF5E\uFF01@#\xA5%\u2026&*\uFF08\uFF09\u2014+\u300C\u300D\uFF5C\uFF1A\u201C\u300A\u300B\uFF1F".split("");
  26. let key = e.key;
  27. if (e.code === "Space") {
  28. key = "Space";
  29. }
  30. if (/^[a-z]$/.test(key)) {
  31. key = key.toUpperCase();
  32. } else if (SPECIAL_KEY_ZH.includes(key)) {
  33. key = SPECIAL_KEY_EN[SPECIAL_KEY_ZH.indexOf(key)];
  34. }
  35. let keyArr = [];
  36. e.ctrlKey && keyArr.push("ctrl");
  37. e.metaKey && keyArr.push("meta");
  38. e.shiftKey && !SPECIAL_KEY_EN.includes(key) && keyArr.push("shift");
  39. e.altKey && keyArr.push("alt");
  40. if (!/Control|Meta|Shift|Alt/i.test(key))
  41. keyArr.push(key);
  42. keyArr = [...new Set(keyArr)];
  43. return keyArr.join("+");
  44. }
  45. function keybind(keys, keydown, keyup) {
  46. const isMac = /macintosh|mac os x/i.test(navigator.userAgent);
  47. keys = keys.filter((key) => !key.includes(isMac ? "ctrl" : "meta"));
  48. function createProcess(callback) {
  49. return function(e) {
  50. var _a;
  51. if (((_a = document.activeElement) == null ? void 0 : _a.tagName) === "INPUT")
  52. return;
  53. const normalizedKey = normalizeKeyEvent(e).toLowerCase();
  54. for (const key of keys) {
  55. if (key.toLowerCase() === normalizedKey)
  56. callback(e, key);
  57. }
  58. };
  59. }
  60. window.addEventListener("keydown", createProcess(keydown));
  61. if (keyup)
  62. window.addEventListener("keyup", createProcess(keyup));
  63. }
  64.  
  65. async function main$1() {
  66. if (!window.ReadTools)
  67. return;
  68. resetPageEvent();
  69. if (isMobile())
  70. injectMovePageEvent();
  71. else
  72. injectShortcuts();
  73. }
  74. function resetPageEvent() {
  75. const $body = document.body;
  76. $body.onclick = (e) => {
  77. const toolsId = ["#toptools", "#bottomtools", "#readset"];
  78. if (toolsId.some(
  79. (id) => {
  80. var _a;
  81. return (_a = document.querySelector(id)) == null ? void 0 : _a.contains(e.target);
  82. }
  83. )) {
  84. return;
  85. }
  86. window.ReadPages.PageClick();
  87. };
  88. }
  89. function injectMovePageEvent() {
  90. let left, startX, startY, diffX, startTime, isMoved, direction;
  91. const $page = document.getElementById("apage");
  92. const isDisabled = (e) => {
  93. return window.ReadTools.pagemid != 1 || e.touches.length > 1 || window.visualViewport && window.visualViewport.scale !== 1 || window.getSelection() && window.getSelection().toString().length > 0;
  94. };
  95. window.addEventListener("touchstart", (e) => {
  96. if (isDisabled(e))
  97. return;
  98. left = parseFloat($page.style.left.replace("px", "")) || 0;
  99. startX = e.touches[0].clientX;
  100. startY = e.touches[0].clientY;
  101. startTime = Date.now();
  102. isMoved = false;
  103. direction = "";
  104. });
  105. window.addEventListener(
  106. "touchmove",
  107. (e) => {
  108. if (isDisabled(e))
  109. return;
  110. isMoved = true;
  111. diffX = e.touches[0].clientX - startX;
  112. let diffY = e.touches[0].clientY - startY;
  113. if (direction === "") {
  114. direction = Math.abs(diffX) > Math.abs(diffY) ? "x" : "y";
  115. }
  116. if (direction === "x") {
  117. e.preventDefault();
  118. $page.style.left = left + diffX + "px";
  119. $page.style.transition = "initail";
  120. }
  121. },
  122. { passive: false }
  123. );
  124. window.addEventListener("touchend", (e) => {
  125. if (isDisabled(e))
  126. return;
  127. if (!isMoved || direction === "y")
  128. return;
  129. const diffTime = Date.now() - startTime;
  130. const threshold = diffTime < 300 ? 10 : document.documentElement.clientWidth * 0.3;
  131. $page.style.transition = "";
  132. if (Math.abs(diffX) > threshold) {
  133. const type = diffX > 0 ? "previous" : "next";
  134. window.ReadPages.ShowPage(type);
  135. if (window.ReadPages.currentPage > window.ReadPages.totalPages || window.ReadPages.currentPage < 1) {
  136. window.ReadPages.ShowPage();
  137. }
  138. } else {
  139. window.ReadPages.ShowPage();
  140. }
  141. });
  142. }
  143. function injectShortcuts() {
  144. keybind(["a", "s", "w", "d", "space", "shift+space"], (e, key) => {
  145. if (window.ReadTools.pagemid != 1)
  146. return;
  147. switch (key) {
  148. case "shift+space":
  149. case "a":
  150. case "w":
  151. window.ReadPages.ShowPage("previous");
  152. break;
  153. case "space":
  154. case "d":
  155. case "s":
  156. window.ReadPages.ShowPage("next");
  157. break;
  158. }
  159. });
  160. }
  161.  
  162. function main() {
  163. removeSelectEvent();
  164. if (/novel\/\d+\.html/.test(window.location.pathname)) {
  165. injectDownload();
  166. }
  167. if (document.body.id === "readbg") {
  168. injectEvent();
  169. }
  170. }
  171. function removeSelectEvent() {
  172. const dom = document.createElement("style");
  173. dom.innerHTML = `* { user-select: initial !important; }`;
  174. document.body.append(dom);
  175. document.body.removeAttribute("onselectstart");
  176. }
  177. function injectEvent() {
  178. const scripts = Array.from(document.scripts);
  179. const script = scripts.find(
  180. (script2) => script2.innerHTML.includes('prevpage="')
  181. );
  182. if (!script)
  183. return;
  184. const res = script.innerHTML.match(
  185. new RegExp('prevpage="(?<pre>.*?)";.*nextpage="(?<next>.*?)";')
  186. );
  187. if (!(res == null ? void 0 : res.groups))
  188. return;
  189. const { pre, next } = res.groups;
  190. keybind(
  191. ["w", "s", "a", "d"],
  192. (e, key) => {
  193. switch (key) {
  194. case "w":
  195. case "s":
  196. const direction = key === "w" ? -1 : 1;
  197. if (e.repeat)
  198. scroll.start(direction * 15);
  199. else
  200. window.scrollBy({ behavior: "smooth", top: direction * 200 });
  201. break;
  202. case "a":
  203. case "d":
  204. window.location.pathname = key === "a" ? pre : next;
  205. break;
  206. }
  207. },
  208. (e, key) => {
  209. switch (key) {
  210. case "w":
  211. case "s":
  212. scroll.stop();
  213. break;
  214. }
  215. }
  216. );
  217. }
  218. function injectDownload() {
  219. const bookId = window.location.pathname.split("/").pop().split(".").shift();
  220. const dom = document.querySelector(".fr.link-group");
  221. const a = document.createElement("a");
  222. dom == null ? void 0 : dom.prepend(a);
  223. a.outerHTML = `<a class="all-catalog" href="http://www.zhidianbao.cn:8088/qs_xq_epub?bookId=${bookId}" target="_blank"><em></em>Epub\u4E0B\u8F7D</a>`;
  224. }
  225. const scroll = (() => {
  226. let handle;
  227. function stop() {
  228. if (!handle)
  229. return;
  230. cancelAnimationFrame(handle);
  231. handle = void 0;
  232. }
  233. function start(step) {
  234. if (handle)
  235. return;
  236. function animate() {
  237. handle = requestAnimationFrame(animate);
  238. window.scrollBy({ top: step });
  239. }
  240. handle = requestAnimationFrame(animate);
  241. }
  242. return { start, stop };
  243. })();
  244.  
  245. var e=[],t=[];function n(n,r){if(n&&"undefined"!=typeof document){var a,s=!0===r.prepend?"prepend":"append",d=!0===r.singleTag,i="string"==typeof r.container?document.querySelector(r.container):document.getElementsByTagName("head")[0];if(d){var u=e.indexOf(i);-1===u&&(u=e.push(i)-1,t[u]={}),a=t[u]&&t[u][s]?t[u][s]:t[u][s]=c();}else a=c();65279===n.charCodeAt(0)&&(n=n.substring(1)),a.styleSheet?a.styleSheet.cssText+=n:a.appendChild(document.createTextNode(n));}function c(){var e=document.createElement("style");if(e.setAttribute("type","text/css"),r.attributes)for(var t=Object.keys(r.attributes),n=0;n<t.length;n++)e.setAttribute(t[n],r.attributes[t[n]]);var a="prepend"===s?"afterbegin":"beforeend";return i.insertAdjacentElement(a,e),e}}
  246.  
  247. var css = "@charset \"UTF-8\";\n.k-wrapper #catelogX .module-header-r {\n min-width: 0;\n}\n.k-wrapper #catelogX .module-header-r .module-header-btn {\n position: static;\n padding: 0;\n}\n.k-wrapper #volumes .chapter-bar {\n display: flex;\n justify-content: space-between;\n align-items: center;\n position: relative;\n}\n.k-wrapper #volumes .chapter-bar::after {\n content: none;\n}\n.k-wrapper #volumes .chapter-bar .download-btn {\n border-radius: 4px;\n background-color: rgba(255, 57, 84, 0.1);\n padding: 4px 12px;\n font-weight: 500;\n color: #ff3955;\n border: 0;\n white-space: nowrap;\n margin-left: 16px;\n}\n.k-wrapper #volumes .chapter-bar .download-btn::after {\n content: \"下载\";\n}\n.k-wrapper #volumes .chapter-bar .download-btn:disabled {\n opacity: 0.5;\n}\n.k-wrapper #volumes .chapter-bar .download-btn:disabled::after {\n content: \"下载中...\";\n}\n.k-wrapper #volumes .chapter-bar .progress,\n.k-wrapper #volumes .chapter-bar .progress > div {\n position: absolute;\n pointer-events: none;\n left: 0;\n right: 0;\n bottom: 0;\n top: 0;\n}\n.k-wrapper #volumes .chapter-bar .progress > div {\n background-color: rgba(255, 57, 84, 0.1);\n transition: all 0.2s linear;\n}";
  248. n(css,{});
  249.  
  250. document.body.classList.add("k-wrapper");
  251. if (window.location.host.includes("www.")) {
  252. main();
  253. } else
  254. main$1();
  255.  
  256. })();