Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. // ==UserScript==
  2. // @name nh_load_all
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://nhentai.net/g/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. let buttonElement, imageQueue, continueOnError;
  15. let imageSources = [];
  16. let imageHeight = '100vh';
  17. let imageWidth = '50%';
  18. let title = document.title;
  19. let container = document.getElementById('thumbnail-container');
  20. let gallerythumbs = document.getElementsByClassName('gallerythumb');
  21. let pattern = /.*galleries\/([\d\/]+).*\.(.*)$/;
  22. let replacement = "https://i.nhentai.net/galleries/$1.$2";
  23. let numProcesses = 1;
  24.  
  25. window.reloadImage = function(imageElement) {
  26. let reloadCount = imageElement.getAttribute('data-reload-count');
  27. let dataSource = imageElement.getAttribute('data-source');
  28. let currentTime = (new Date()).getTime();
  29.  
  30. if (reloadCount < 3) {
  31. imageElement.src = `${dataSource}?t=${currentTime}`;
  32. imageElement.setAttribute('data-reload-count', reloadCount + 1);
  33. console.log(`${reloadCount}: reloading img ${dataSource}`);
  34. } else {
  35. console.log(`max retry of ${reloadCount} reached - ${dataSource}`);
  36. if (typeof continueOnError === 'undefined') {
  37. continueOnError = confirm('Unable to load image. Proceed with next images?');
  38. }
  39. if (continueOnError) {
  40. processQueue();
  41. }
  42. }
  43. };
  44.  
  45. for (let gallerythumb of gallerythumbs) {
  46. let imageElement = gallerythumb.getElementsByTagName('img')[0];
  47. let dataSource = imageElement.getAttribute('data-src');
  48. let imageSource = dataSource.replace(pattern, replacement);
  49. imageSources.push(imageSource);
  50. }
  51. document.title = `[INC - ${imageSources.length}] ${title}`;
  52. container.innerHTML = '';
  53.  
  54. // insert one image a time
  55. imageQueue = [...imageSources];
  56. window.processQueue = function() {
  57. let numRemainingImages = imageQueue.length;
  58. if (numRemainingImages === 0) {
  59. console.log('no more images left');
  60. document.title = `[COMPLETE] ${title}`;
  61. return;
  62. }
  63. let imageSource = imageQueue.shift();
  64. let imageElement = document.createElement("img");
  65. imageElement.src = imageSource;
  66. imageElement.setAttribute('data-reload-count', 0);
  67. imageElement.setAttribute('data-source', imageElement.src);
  68. imageElement.setAttribute('onerror', 'reloadImage(this)');
  69. imageElement.setAttribute('onload', 'processQueue()');
  70. imageElement.style.background = `transparent url('https://thinkfuture.com/wp-content/uploads/2013/10/loadingSpinner.gif') center no-repeat;`;
  71. imageElement.style['max-width'] = imageWidth;
  72. imageElement.style['max-height'] = imageHeight;
  73. container.appendChild(imageElement);
  74. // notify progress
  75. console.log(`image added: ${imageElement.src}`);
  76. document.title = `[INC - ${numRemainingImages}] ${title}`;
  77. };
  78. // start queue
  79. for (var i = 0; i < numProcesses; i++) {
  80. processQueue();
  81. }
  82.  
  83. // add button to update height
  84. buttonElement = document.createElement('button');
  85. buttonElement.className = 'btn btn-primary btn-custom';
  86. buttonElement.innerText = '200vh';
  87. buttonElement.style = `position: fixed; top: 2em; right: 2em;`;
  88.  
  89. buttonElement.addEventListener('click', function() {
  90. if (imageHeight === '200vh') {
  91. imageHeight = '100vh';
  92. imageWidth = '50%';
  93. this.innerText = '200vh';
  94. } else {
  95. imageHeight = '200vh';
  96. imageWidth = '100%';
  97. this.innerText = '100vh';
  98. }
  99.  
  100. let imgs = document.querySelectorAll('#thumbnail-container img');
  101. for (let img of imgs) {
  102. if (img) {
  103. img.style['max-width'] = imageWidth;
  104. img.style['max-height'] = imageHeight;
  105. }
  106. }
  107. console.log(`settings cssText to ${imgs[0].style.cssText}`);
  108. });
  109. document.body.append(buttonElement);
  110.  
  111. // pagedown on right
  112. document.addEventListener('keydown', function(e) {
  113. let height = window.innerHeight;
  114.  
  115. switch(e.keyCode) {
  116. case 37: // left
  117. window.scrollBy(0, -height);
  118. break;
  119. case 39: // right
  120. window.scrollBy(0, height);
  121. break;
  122. default: return; // exit this handler for other keys
  123. }
  124. e.preventDefault(); // prevent the default action (scroll / move caret)
  125. });
  126.  
  127. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement