nhentai Auto Scroller

Auto scroll doujinshi and manga on nhentai so you can jerk off all hands-free.

2022-02-27 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         nhentai Auto Scroller
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  Auto scroll doujinshi and manga on nhentai so you can jerk off all hands-free.
// @author       LoliEnjoyer
// @include      /^https:\/\/nhentai\.net\/g\/\d+\/\d+\/$/
// ==/UserScript==
const PageHandler = {

  dispatch: function (options) {
    document.body.dispatchEvent(new KeyboardEvent("keydown", options));
    this.downTimeoutID = undefined;
    this.rightTimeoutID = undefined;
  },

  down: function (timeout) {
    if (typeof this.downTimeoutID === 'number') {
      this.cancel(this.downTimeoutID);
    }

    this.downTimeoutID = setTimeout(function (opts) {
      this.dispatch(opts);
    }.bind(this), timeout, { key: "ArrowDown", keyCode: 40 });
  },

  right: function (timeout) {
    if (typeof this.rightTimeoutID === 'number') {
      this.cancel(this.rightTimeoutID);
    }

    this.rightTimeoutID = setTimeout(function (opts) {
      this.dispatch(opts);
    }.bind(this), timeout, { key: "ArrowRight", keyCode: 39 });
  },

  cancel: function (timeoutID) {
    clearTimeout(timeoutID);
  },

  stop: function () {
    this.cancel(this.downTimeoutID);
    this.cancel(this.rightTimeoutID);
    document.body.dispatchEvent(new KeyboardEvent('keyup', { key: "ArrowDown", keyCode: 40 }));
  }
};

const div = document.createElement('div');
const button = document.createElement('button');
const icon = document.createElement('i');

const keyboardDown = new KeyboardEvent("keydown", { key: "ArrowDown", keyCode: 40 });
const keyboardRight = new KeyboardEvent("keydown", { key: "ArrowRight", keyCode: 39 });

let isAutoScroll = false;

function buttonClickAction() {
  isAutoScroll = !isAutoScroll
  icon.setAttribute('class', `fa fa-lg ${isAutoScroll ? 'fa-pause' : 'fa-play'}`);
  isAutoScroll ? PageHandler.down(250) : PageHandler.stop()
}

function buttonPosition() {
  window.pageYOffset >= 90
    ? Object.assign(div.style, { position: 'fixed', top: 0 })
    : Object.assign(div.style, { position: 'absolute', top: "90px" });
}

button.setAttribute('class', 'btn btn-primary');
icon.setAttribute('class', 'fa fa-lg fa-play');

Object.assign(div.style, { 'z-indez': 100, right: 0, margin: '10px' })
Object.assign(button.style, { width: '46px', height: '46px' })
buttonPosition()
button.appendChild(icon)
div.appendChild(button)
document.body.appendChild(div);

button.addEventListener("click", buttonClickAction);
window.addEventListener("scroll", buttonPosition);
window.addEventListener("scroll", function () {
  if ((window.innerHeight + window.pageYOffset >= document.body.scrollHeight) && isAutoScroll) {
    PageHandler.right(4500);
  }
});

const onMutate = function(mutationsList) {
  if (isAutoScroll) PageHandler.down(3500);
}
const observer = new MutationObserver(onMutate);
observer.observe(document.body, {childList: true, subtree: true});