nhentai auto scroller

Automatically changes the image source and page counter every 6 seconds (toggle button now included)

  1. // ==UserScript==
  2. // @name nhentai auto scroller
  3. // @namespace https://nhentai.net/
  4. // @version 1.3
  5. // @description Automatically changes the image source and page counter every 6 seconds (toggle button now included)
  6. // @match https://nhentai.net/g/*/*/
  7. // @author equmaq
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to change the image source
  17. function changeImageSource() {
  18. if (document.hidden) {
  19. return; // Skip changing the source if the page is not in focus
  20. }
  21.  
  22. var toggleButton = document.evaluate("/html/body/div[2]/section[4]/div[2]/span", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  23. if (toggleButton.textContent === "►") {
  24. return; // Skip changing the source if the toggle button is displaying "►"
  25. }
  26.  
  27. var imgElement = document.evaluate("/html/body/div[2]/section[3]/a/img", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  28. var currentSrc = imgElement.src;
  29. var splitSrc = currentSrc.split('/');
  30. var currentNumber = splitSrc[splitSrc.length - 1].split('.')[0];
  31. var newNumber = parseInt(currentNumber) + 1;
  32. var newSrc = currentSrc.replace(currentNumber + '.jpg', newNumber + '.jpg');
  33. imgElement.src = newSrc;
  34. }
  35.  
  36. // Function to change the page counter text
  37. function changePageCounter() {
  38. if (document.hidden) {
  39. return; // Skip changing the page counter if the page is not in focus
  40. }
  41.  
  42. var toggleButton = document.evaluate("/html/body/div[2]/section[4]/div[2]/span", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  43. if (toggleButton.textContent === "►") {
  44. return; // Skip changing the page counter if the toggle button is displaying "►"
  45. }
  46.  
  47. var counterElement = document.evaluate("/html/body/div[2]/section[4]/div[2]/button/span[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  48. var currentPage = parseInt(counterElement.textContent);
  49. var newPage = currentPage + 1;
  50. counterElement.textContent = newPage.toString();
  51. }
  52.  
  53. // Function to toggle the button text and action
  54. function toggleButton() {
  55. if (document.hidden) {
  56. return; // Skip toggling the button if the page is not in focus
  57. }
  58.  
  59. var buttonElement = document.createElement("span");
  60. buttonElement.style.width = "40px";
  61. buttonElement.style.height = "40px";
  62. buttonElement.style.display = "inline-flex";
  63. buttonElement.style.alignItems = "center";
  64. buttonElement.style.justifyContent = "center";
  65. buttonElement.style.cursor = "pointer";
  66. buttonElement.style.fontSize = "24px";
  67. buttonElement.style.userSelect = "none";
  68. buttonElement.textContent = "►"; // Set initial button text
  69.  
  70. var toggleButton = true;
  71.  
  72. function toggle() {
  73. toggleButton = !toggleButton;
  74. buttonElement.textContent = toggleButton ? "►" : "II";
  75. }
  76.  
  77. buttonElement.addEventListener("click", toggle);
  78.  
  79. var containerElement = document.evaluate("/html/body/div[2]/section[4]/div[2]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  80. containerElement.appendChild(buttonElement);
  81. }
  82.  
  83. // Function to recreate the toggle button element
  84. function recreateToggleButton() {
  85. var toggleButton = document.evaluate(
  86. "/html/body/div[2]/section[4]/div[2]/span",
  87. document,
  88. null,
  89. XPathResult.FIRST_ORDERED_NODE_TYPE,
  90. null
  91. ).singleNodeValue;
  92.  
  93. if (!toggleButton) {
  94. toggleButton = document.createElement("span");
  95. toggleButton.style.width = "40px";
  96. toggleButton.style.height = "40px";
  97. toggleButton.style.display = "inline-flex";
  98. toggleButton.style.alignItems = "center";
  99. toggleButton.style.justifyContent = "center";
  100. toggleButton.style.cursor = "pointer";
  101. toggleButton.style.fontSize = "24px";
  102. toggleButton.style.userSelect = "none";
  103. toggleButton.textContent = "►";
  104.  
  105. var containerElement = document.evaluate(
  106. "/html/body/div[2]/section[4]/div[2]",
  107. document,
  108. null,
  109. XPathResult.FIRST_ORDERED_NODE_TYPE,
  110. null
  111. ).singleNodeValue;
  112. containerElement.appendChild(toggleButton);
  113. }
  114. }
  115.  
  116. // Mutation observer callback function
  117. function mutationCallback(mutationsList) {
  118. for (var mutation of mutationsList) {
  119. if (mutation.type === 'childList') {
  120. recreateToggleButton();
  121. }
  122. }
  123. }
  124.  
  125. // Create a new MutationObserver instance
  126. var observer = new MutationObserver(mutationCallback);
  127.  
  128. // Start observing changes in the target node
  129. observer.observe(document.body, { childList: true, subtree: true });
  130.  
  131. // Call the functions every 6 seconds
  132. setInterval(changeImageSource, 6000);
  133. setInterval(changePageCounter, 6000);
  134. toggleButton();
  135. recreateToggleButton();
  136.  
  137. })();