E-Hentai Better Navigation

Удобное средство просмотра галереи.

  1. // ==UserScript==
  2. // @name E-Hentai Better Navigation
  3. // @description Удобное средство просмотра галереи.
  4. // @namespace https://e-hentai.org/s/*
  5. // @include https://exhentai.org/s/*
  6. // @include https://e-hentai.org/s/*
  7. // @version 1
  8. // @grant none
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
  10. // ==/UserScript==
  11.  
  12. document.body.style.background = "black";
  13. document.body.querySelector("html body div#i1.sni").style.display = "none";
  14. document.body.querySelector("html body p.ip").style.display = "none";
  15.  
  16. let mainBody = document.createElement("div");
  17. let sidebar = document.createElement("div");
  18. let header = document.createElement("div");
  19. let headerText = document.createElement("p");
  20. let bottom = document.createElement("div");
  21. let bottomText = document.createElement("p");
  22. let helpLink = document.createElement("a");
  23. let sidebarBody = document.createElement("div");
  24. let imageMain = document.createElement("img");
  25.  
  26.  
  27. let table = document.createElement("table");
  28. let tableBody = document.createElement("tbody");
  29. let tableRow = document.createElement("tr");
  30. let tableColumn1 = document.createElement("td");
  31. let tableColumn2 = document.createElement("td");
  32.  
  33. document.body.insertBefore(mainBody, document.body.firstChild);
  34.  
  35. mainBody.appendChild(table);
  36. table.appendChild(tableBody);
  37. tableBody.appendChild(tableRow);
  38. tableRow.appendChild(tableColumn1);
  39. tableRow.appendChild(tableColumn2);
  40.  
  41. tableColumn1.appendChild(sidebar);
  42. sidebar.appendChild(header);
  43. header.appendChild(headerText);
  44. sidebar.appendChild(sidebarBody);
  45. sidebar.appendChild(bottom);
  46. bottom.appendChild(helpLink);
  47. bottom.appendChild(bottomText);
  48. tableColumn2.appendChild(imageMain);
  49.  
  50. tableColumn1.vAlign = "top";
  51. tableColumn2.vAlign = "top";
  52. tableColumn2.align = "center";
  53. tableColumn2.style.width = "100%";
  54.  
  55. sidebar.style.background = "grey";
  56. sidebar.style.width = "240px";
  57. sidebar.style.height = "100vh";
  58. sidebar.style.position = "fixed";
  59.  
  60. sidebarBody.style.height = "95vh";
  61. sidebarBody.style.overflowY = "auto";
  62.  
  63. header.style.background = "darkgrey";
  64.  
  65. let headerTextMain = document.body.querySelector("html body div#i1.sni h1").innerText;
  66. headerText.innerText = headerTextMain;
  67.  
  68. $(helpLink).click(function () {
  69. alert("Shortkeys:\n" +
  70. "1 - fit image to screen\n" +
  71. "2 - set image size to real\n" +
  72. "m - go to gallery main page");
  73. });
  74. helpLink.innerText = "(help)";
  75. helpLink.href = "#";
  76. bottomText.innerText = "";
  77.  
  78. imageMain.style.textAlign = "center";
  79. imageMain.style.display = "block";
  80. imageMain.style.maxHeight = "99vh";
  81. imageMain.style.userSelect = "none";
  82.  
  83. table.style.borderCollapse = "collapse";
  84.  
  85. $(tableColumn2).click(function (event) {
  86. if ($(event.target).is(imageMain)){
  87. if(openedPageIndex+1<pageUri.length)
  88. openedPageIndex++;
  89. $(sidebarBody).animate({
  90. scrollTop: $(sidebarBody).scrollTop() - $(sidebarBody).offset().top + $("#"+(openedPageIndex+1)).offset().top-50
  91. }, 500);
  92. SrcAssignHandler();
  93. } else {
  94. if(openedPageIndex>0)
  95. openedPageIndex--;
  96. $(sidebarBody).animate({
  97. scrollTop: $(sidebarBody).scrollTop() - $(sidebarBody).offset().top + $("#"+(openedPageIndex+1)).offset().top-50
  98. }, 500);
  99. SrcAssignHandler();
  100. }
  101. window.scrollTo(0, 0);
  102. });
  103.  
  104. let openedPageIndex;
  105. let pageUri = [];
  106. let imageSrc = [];
  107. let imagePreviewBackground = [];
  108. openedPageIndex = parseInt(document.documentURI.substring(document.documentURI.lastIndexOf("-")+1))-1;
  109. imageMain.src = document.body.querySelector("html body div#i1.sni div#i3 a img#img").getAttribute("src");
  110.  
  111. let homeButton = document.body.querySelector("html body div#i1.sni div#i5 div.sb a");
  112. let homeHref = homeButton.getAttribute("href");
  113. if (homeHref.includes("?")) homeHref = homeHref.substr(0, homeHref.indexOf("?")-1)+"/";
  114.  
  115. $.ajax({url: homeHref, dataType: 'html', success: function(responseData){
  116. pageUri = $(responseData).find(".gdtm").children().find("a").toArray();
  117.  
  118. let imagePreview = $(responseData).find(".gdtm").children().toArray();
  119. for (let i=0; i<imagePreview.length; i++){
  120. imagePreviewBackground.push($(imagePreview[i]).css("background"));
  121. }
  122. headerText.innerText += pageUri.length;
  123. GetPagesHrefs();
  124. }});
  125. let galleryPageIndex = 1;
  126.  
  127. let images = [];
  128. function GetPagesHrefs(){
  129. $.ajax({url: homeHref + "?p="+ galleryPageIndex, dataType: 'html', success: function(responseData){
  130. let child = $(responseData).find(".gdtm").children().find("a").toArray();
  131. let imagePreview = $(responseData).find(".gdtm").children().toArray();
  132. for (let i=0; i < imagePreview.length; i++){
  133. imagePreviewBackground.push($(imagePreview[i]).css("background"));
  134. }
  135. let pageIndex;
  136. pageIndex = $(responseData).find(".ptds").first().children().text();
  137. if (pageIndex !== (galleryPageIndex+1).toString()){
  138. headerText.innerText = headerTextMain+ " | ";
  139. AppendNeededImages();
  140. } else {
  141. for (let i = 0; i < child.length; i++){
  142. pageUri.push(child[i]);
  143. }
  144. galleryPageIndex++;
  145. setTimeout(function(){GetPagesHrefs();}, 100);
  146. }
  147. headerText.innerText += pageUri.length;
  148. }});
  149. }
  150.  
  151.  
  152. function AppendNeededImages() {
  153. for (let i=0;i<pageUri.length;i++){
  154. images.push(document.createElement("img"));
  155. imageSrc.push("");
  156.  
  157. let imageNumber = document.createElement("div");
  158. let sideBarImageContainer = document.createElement("div");
  159.  
  160. images[i].id = i+1;
  161. images[i].alt = i+1;
  162. images[i].title = i+1;
  163. imageNumber.innerText = i+1;
  164. imageNumber.style.background = "black";
  165. imageNumber.style.color = "white";
  166. imageNumber.style.position = "absolute";
  167. imageNumber.style.top = "5px";
  168. imageNumber.style.left = "5px";
  169. sideBarImageContainer.title = i+1;
  170. sideBarImageContainer.style.position = "relative";
  171. sideBarImageContainer.style.width = "100px";
  172. sideBarImageContainer.style.minHeight = "138px";
  173. sideBarImageContainer.style.background = imagePreviewBackground[i];
  174.  
  175. $(sideBarImageContainer).click(function () {
  176. openedPageIndex = parseInt(this.title)-1;
  177.  
  178. $(sidebarBody).animate({
  179. scrollTop: $(sidebarBody).scrollTop() - $(sidebarBody).offset().top + $("#"+(openedPageIndex+1)).offset().top-50
  180. }, 500);
  181.  
  182. window.scrollTo(0, 0);
  183. SrcAssignHandler();
  184. });
  185.  
  186. images[i].style.maxHeight = "400px";
  187. images[i].style.maxWidth = "220px";
  188.  
  189.  
  190. sidebarBody.appendChild(sideBarImageContainer);
  191. sideBarImageContainer.appendChild(images[i]);
  192. sideBarImageContainer.appendChild(imageNumber);
  193. }
  194. SrcAssignHandler();
  195. }
  196.  
  197. function SrcAssignHandler() {
  198. if (imageSrc[openedPageIndex] === "")
  199. GetImgSrc(pageUri[openedPageIndex], openedPageIndex);
  200. else imageMain.src = imageSrc[openedPageIndex];
  201. if (openedPageIndex > 1 && imageSrc[openedPageIndex - 2] === "") {
  202. GetImgSrc(pageUri[openedPageIndex - 2], openedPageIndex - 2);
  203. }
  204. if (openedPageIndex > 0 && imageSrc[openedPageIndex - 1] === "") {
  205. GetImgSrc(pageUri[openedPageIndex - 1], openedPageIndex - 1);
  206. }
  207. if (openedPageIndex + 1 < pageUri.length && imageSrc[openedPageIndex + 1] === "") {
  208. GetImgSrc(pageUri[openedPageIndex + 1], openedPageIndex + 1);
  209. }
  210. if (openedPageIndex + 2 < pageUri.length && imageSrc[openedPageIndex + 2] === "") {
  211. GetImgSrc(pageUri[openedPageIndex + 2], openedPageIndex + 2);
  212. }
  213. }
  214.  
  215. function GetImgSrc(pageUri, index) {
  216. $.get(pageUri).then(function(responseData){
  217. let prevImageSrc = responseData.substring(responseData.indexOf("<img id=")+19);
  218. prevImageSrc = prevImageSrc.substring(0,prevImageSrc.indexOf("style")-2);
  219. setTimeout(function(){AssignPageUri(prevImageSrc, index);},100);
  220. });
  221. }
  222.  
  223. function AssignPageUri(src, index) {
  224. if (index === openedPageIndex){
  225. imageMain.src = src;
  226. $(sidebarBody).animate({
  227. scrollTop: $(sidebarBody).scrollTop() - $(sidebarBody).offset().top + $("#"+(openedPageIndex+2)).offset().top
  228. }, 500);
  229. }
  230. imageSrc[index] = src;
  231. images[index].src = src;
  232. }
  233.  
  234.  
  235. document.body.addEventListener("keydown", function (event) {
  236. if (event.keyCode === 49) {imageMain.style.maxHeight = "99vh";} //1
  237. if (event.keyCode === 50) {imageMain.style.maxHeight = "100%";} //2
  238. if (event.keyCode === 77) {window.location.href = homeButton.href;} //m
  239. if (event.keyCode === 81) {imageMain.src = imageSrc[openedPageIndex];} //r
  240. });