Copymanga Simple Read

manga reader for copymanga, J/K for DOWN/UP, LEFT/RIGHT for previous/next chapter.

当前为 2021-09-11 提交的版本,查看 最新版本

// ==UserScript==
// @name        Copymanga Simple Read
// @namespace   http://tampermonkey.net/
// @match       https://www.copymanga.com/comic/*/chapter/*
// @grant       none
// @version     1.7
// @author      chemPolonium
// @description manga reader for copymanga, J/K for DOWN/UP, LEFT/RIGHT for previous/next chapter.
// @run-at      document-end
// ==/UserScript==

/* jshint esversion: 6 */
(function() {
  'use strict';

  document.getElementsByClassName('header')[0].remove();

  let comicContainerFluid = document.getElementsByClassName('container-fluid comicContent')[0];
  comicContainerFluid.setAttribute('style', 'padding-right: 0px !important; padding-left: 0px !important;');

  let comicContainer = comicContainerFluid.children[0];
  comicContainer.setAttribute('style', 'max-width: 100vw !important; margin-right: 0px !important; margin-left: 0px !important');

  let comicList = comicContainer.children[0];
  // let comicList = document.getElementsByClassName('comicContent-list')[0];
  // comicList.setAttribute('style', 'padding-top: 0px !important; margin-bottom: 0px !important; width: 100vw !important');
  // let headerWhiteSpace = comicList.children[0].children[0].offsetTop;
  comicList.setAttribute('style', 'padding-top: 0px !important; margin-bottom: 0px !important; max-width: 100vw !important; width: 100vw !important; display:grid;grid-template-columns: repeat(2, 1fr);direction: rtl;');


  let currentPageInd = -1;
  let currentImage;

  function movePage(x) {
    currentPageInd += x;
    currentPageInd = currentPageInd < comicList.children.length ? currentPageInd : comicList.children.length - 1;
    currentPageInd = currentPageInd >= 0 ? currentPageInd : 1;
    currentImage = comicList.children[currentPageInd].children[0];
  }

  // window.scrollTo(0, headerWhiteSpace);

  let evt = new UIEvent('scroll');

  function setAlign() {
    let isLeft = false;
    let comicListChildren = comicList.children;
    for (let i = 0; i < comicListChildren.length; i++) {
      if (isLeft) {
        comicListChildren[i].setAttribute('style', 'text-align: right;');
      } else {
        comicListChildren[i].setAttribute('style', 'text-align: left;');
      }
      isLeft = !isLeft;
    }
  }
  
  function onePageDown() {
    // simulate the scroll for preload
    // the script is like this: total client height / 3 < window scrollY then not load
    // so first scroll Y to 0
    window.scrollTo(0, 0);
    for (let i = 0; i < 2; i++) {
      window.dispatchEvent(evt);
      // dispatch the scroll event for preload
      movePage(1);
      // the set will not work if important is not added
      currentImage.setAttribute('style', 'width:auto !important; height:auto !important;max-height: 100vh !important; max-width: 50vw !important;');
    }
    window.scrollTo(0, currentImage.offsetTop);
    setAlign();
  }

  function onePageUp() {
    movePage(-2);
    window.scrollTo(0, currentImage.offsetTop);
  }

  function isParityChanged() {
    return (comicList.children[0].innerText == 'foo');
  }

  function switchParity() {
    if (currentPageInd < 0) {
      onePageDown();
    }
    if (isParityChanged()) {
      comicList.children[0].remove();
    } else {
      comicList.insertAdjacentHTML('afterbegin','<li>foo</li>');
    }
    setAlign();
  }

  let footer = document.getElementsByClassName('footer')[0];
  let footerChildren = footer.children;
  let prevChapterHref = footerChildren[1].children[0].href;
  let nextChapterHref = footerChildren[3].children[0].href;
  let chapterListHref = footerChildren[4].children[0].href;
  
  document.addEventListener('keydown', (event) => {
    switch (event.code) {
      case 'ArrowRight':
        window.location = nextChapterHref;
        break;
      case 'ArrowLeft':
        window.location = prevChapterHref;
        break;
      case 'KeyK':
        onePageUp();
        break;
      case 'KeyJ':
        onePageDown();
        break;
      case 'KeyL':
        window.location = chapterListHref;
        break;
      case 'Semicolon':
        switchParity();
        break;
      default:
        console.log('key: ' + event.key + ' code: ' + event.code);
    }
  });
  
  footer.remove();

  let firstLoad = true;
  comicList.addEventListener('DOMNodeInserted', (event) => {
    if (firstLoad && comicList.children.length > 2) {
      firstLoad = false;
      onePageDown();
      switchParity();
    }
  });

})();