Auto-load NHentai images

Auto-load NHentai images1

  1. // ==UserScript==
  2. // @name Auto-load NHentai images
  3. // @namespace Violentmonkey Scripts
  4. // @version 0.3
  5. // @description Auto-load NHentai images1
  6. // @author Kenseori
  7. // @license MIT
  8. // @match https://nhentai.xxx/g/*/*
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let totalPages = 0;
  16.  
  17. // Функция для получения общего числа страниц
  18. const getTotalPages = () => {
  19. const pagesButton = document.querySelector('.reader_nav .pages_btn');
  20. if (pagesButton) {
  21. const pageCountText = pagesButton.querySelector('.tp')?.textContent;
  22. if (pageCountText) {
  23. totalPages = parseInt(pageCountText, 10);
  24. console.log('Total Pages:', totalPages);
  25. }
  26. }
  27. if (!totalPages) {
  28. console.error("Не удалось найти количество страниц.");
  29. }
  30. };
  31.  
  32. // Функция для загрузки следующей страницы
  33. const loadNextPage = (currentUrl) => {
  34. const match = currentUrl.match(/\/g\/(\d+)\/(\d+)\/$/);
  35. if (!match) return console.error("Не удалось извлечь данные из URL:", currentUrl);
  36.  
  37. const [_, galleryId, currentPageNumber] = match;
  38. const nextPageNumber = parseInt(currentPageNumber, 10) + 1;
  39.  
  40. if (nextPageNumber <= totalPages) {
  41. const nextPageUrl = `https://nhentai.xxx/g/${galleryId}/${nextPageNumber}/`;
  42. GM_xmlhttpRequest({
  43. method: 'GET',
  44. url: nextPageUrl,
  45. onload: function(response) {
  46. const doc = new DOMParser().parseFromString(response.responseText, 'text/html');
  47. const newImage = doc.querySelector('img[id="fimg"]');
  48. if (newImage) {
  49. const imageSource = newImage.hasAttribute('data-src') ? newImage.getAttribute('data-src') : newImage.src;
  50. const imgContainer = document.querySelector('.fw_img');
  51. if (imgContainer) {
  52. const newImgElement = document.createElement('img');
  53. newImgElement.src = imageSource;
  54. newImgElement.className = 'lazy entered loaded';
  55. imgContainer.appendChild(newImgElement);
  56. console.log(`Загружена страница ${nextPageNumber}`);
  57. loadNextPage(nextPageUrl); // рекурсивный вызов для следующей страницы
  58. } else {
  59. console.error("Не удалось найти контейнер для изображения.");
  60. }
  61. } else {
  62. console.error("Изображение не найдено на следующей странице:", nextPageUrl);
  63. }
  64. },
  65. onerror: () => {
  66. console.error("Ошибка при загрузке страницы:", nextPageUrl);
  67. }
  68. });
  69. } else {
  70. console.log('Загрузили все страницы.');
  71. }
  72. };
  73.  
  74. // Запуск автоматической загрузки
  75. const startAutoLoad = () => {
  76. getTotalPages();
  77. if (totalPages > 0) {
  78. loadNextPage(window.location.href);
  79. } else {
  80. console.error('Не удалось получить количество страниц.');
  81. }
  82. };
  83.  
  84. startAutoLoad();
  85. })();