漫画猫阅读器

漫画猫阅读器,重排页面,按键翻页,奇偶切换,单双页切换

  1. // ==UserScript==
  2. // @name 漫画猫阅读器
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.maofly.com/manga/*/*
  5. // @grant none
  6. // @version 0.5
  7. // @author chemPolonium
  8. // @description 漫画猫阅读器,重排页面,按键翻页,奇偶切换,单双页切换
  9. // @license GPLv3
  10. // ==/UserScript==
  11.  
  12. /* jshint esversion: 6 */
  13. /* jshint multistr: true */
  14.  
  15. (function () {
  16. "use strict";
  17.  
  18. let comicContainer = document.getElementsByClassName("container-fluid comic-detail p-0")[0];
  19. comicContainer.style.maxWidth = '100%';
  20.  
  21. let comicList = document.getElementsByClassName("img-content")[0];
  22. comicList.style.display = 'grid';
  23. comicList.style.direction = 'rtl';
  24.  
  25. let comicListChildren = comicList.children;
  26.  
  27. let currentImageIndex = 0;
  28.  
  29. let pageNumPerScreen = 2;
  30.  
  31. let loadedPageNum = 1;
  32.  
  33. function setstyle(target) {
  34. target.style = {};
  35. target.style.height = '100vh';
  36. target.style.width = '100%';
  37. target.style.objectFit = 'contain';
  38. }
  39.  
  40.  
  41. function moveImageIndex(x) {
  42. let newImageIndex = currentImageIndex + x;
  43. if (newImageIndex < comicList.children.length && newImageIndex >= 0) {
  44. currentImageIndex = newImageIndex;
  45. }
  46. }
  47.  
  48. function getImage(imageIndex) {
  49. return comicListChildren[imageIndex];
  50. }
  51.  
  52. function getCurrentImage() {
  53. return getImage(currentImageIndex);
  54. }
  55.  
  56. function moveToCurrentImage() {
  57. getCurrentImage().scrollIntoView();
  58. }
  59.  
  60. function onePageDown() {
  61. moveImageIndex(pageNumPerScreen);
  62. moveToCurrentImage();
  63. }
  64.  
  65. function onePageUp() {
  66. moveImageIndex(-pageNumPerScreen);
  67. moveToCurrentImage();
  68. }
  69.  
  70. function setSingleAlign(imageIndex) {
  71. if (pageNumPerScreen == 1) {
  72. comicListChildren[imageIndex].style.objectPosition = 'center';
  73. }
  74. if (pageNumPerScreen == 2) {
  75. comicListChildren[imageIndex].style.objectPosition = (imageIndex % 2 == 0) ? 'left' : 'right';
  76. }
  77. }
  78.  
  79. function setAlign() {
  80. for (let imageIndex = 0; imageIndex < comicListChildren.length; imageIndex++) {
  81. setSingleAlign(imageIndex);
  82. }
  83. }
  84.  
  85. function setPageNumPerScreen(pageNum) {
  86. comicList.style.gridTemplateColumns = 'repeat(' + String(pageNum) + ', 1fr)';
  87. moveToCurrentImage();
  88. pageNumPerScreen = pageNum;
  89. currentImageIndex -= (currentImageIndex % pageNum);
  90. setAlign();
  91. }
  92.  
  93. setstyle(comicList.firstElementChild);
  94.  
  95. moveToCurrentImage();
  96.  
  97. setPageNumPerScreen(2);
  98.  
  99. function createTitlePage() {
  100. let titlePage = document.createElement('p');
  101. titlePage.textContent = document.title;
  102. titlePage.style.fontSize = 'xx-large';
  103. titlePage.style.maxWidth = '30vw';
  104. titlePage.style.marginTop = '30%';
  105. titlePage.style.marginRight = '20%';
  106. titlePage.style.whiteSpace = 'normal';
  107. return titlePage;
  108. }
  109.  
  110. let titlePage = createTitlePage();
  111.  
  112. let parityChanged = false;
  113.  
  114. function switchParity() {
  115. if (parityChanged) {
  116. comicListChildren[0].remove();
  117. } else {
  118. comicList.insertAdjacentElement('afterbegin', titlePage);
  119. }
  120. parityChanged = !parityChanged;
  121. setAlign();
  122. moveToCurrentImage();
  123. }
  124.  
  125. document.addEventListener('keydown', (event) => {
  126. switch (event.code) {
  127. case 'ArrowRight':
  128. goNumPage('next');
  129. break;
  130. case 'ArrowLeft':
  131. goNumPage('pre');
  132. break;
  133. case 'KeyK':
  134. onePageUp();
  135. break;
  136. case 'KeyJ':
  137. onePageDown();
  138. break;
  139. case 'Semicolon':
  140. switchParity();
  141. break;
  142. case 'Digit1':
  143. setPageNumPerScreen(1);
  144. break;
  145. case 'Digit2':
  146. setPageNumPerScreen(2);
  147. break;
  148. default:
  149. console.log('key: ' + event.key + ' code: ' + event.code);
  150. }
  151. });
  152.  
  153. let firstLoad = true;
  154. comicList.addEventListener('DOMNodeInserted', (event) => {
  155. setstyle(event.target);
  156. if (event.target.className != '') {
  157. loadedPageNum += 1;
  158. }
  159. if (parityChanged) {
  160. setSingleAlign(loadedPageNum);
  161. } else {
  162. setSingleAlign(loadedPageNum - 1);
  163. }
  164. });
  165. })();