Random Page and Gallery for exhentai

adds random page and random gallery buttons to search pages

  1. // ==UserScript==
  2. // @name Random Page and Gallery for exhentai
  3. // @namespace http://exhentai.org/
  4. // @version 1.1
  5. // @description adds random page and random gallery buttons to search pages
  6. // @author sllypper
  7. // @include *://exhentai.org/*
  8. // @include *://g.e-hentai.org/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // Options:
  13.  
  14. let openGalleryOnNewTab = true;
  15.  
  16. //
  17.  
  18. function onlyUnique(value, index, self) {
  19. return self.indexOf(value) === index;
  20. }
  21.  
  22. function openRandGal() {
  23. let gals = Array.from(document.getElementsByClassName('itg')[0].getElementsByTagName('a')).filter(gal => gal.href.includes('/g/'));
  24. // gals = gals.filter(onlyUnique);
  25. window.open(gals[parseInt(Math.floor(Math.random() * gals.length - 1))].href, openGalleryOnNewTab ? '_blank' : '_parent');
  26. }
  27.  
  28. function getRandPageNum() {
  29. let pageButtons = document.getElementsByClassName('ptt')[0].firstChild.firstChild.children;
  30. let numPages = pageButtons[pageButtons.length - 2].firstChild.text;
  31. return parseInt((numPages * Math.random()) - 1);
  32. }
  33.  
  34. function openRandPage() {
  35. let pageNum = getRandPageNum();
  36. let url = Array.from(document.getElementsByClassName('ptt')[0].getElementsByTagName('a')).find(el => el.href.includes('page'));
  37. if (url) {
  38. url = url.href.replace(/(page=)\d+(&)/, '$1' + pageNum + '$2');
  39. window.open(url, '_parent');
  40. }
  41. }
  42.  
  43. (function(){
  44. let isFavorites = location.href.indexOf('favorites') > 0;
  45. let place;
  46. let style = '';
  47.  
  48. if (!isFavorites) {
  49. // on the main page
  50. place = document.getElementsByClassName('nopm');
  51. place = place[place.length - 1];
  52. style = 'float: right;'
  53. } else {
  54. // on the Favorites page
  55. place = document.getElementsByClassName('ip')[0];
  56. }
  57.  
  58. let template = `<div id="randomStuff" style="${style}">
  59. <a id="randGalLink" href="#">Random Gallery</a> &nbsp; &nbsp;
  60. <a id="randPageLink" href="#">Random Page</a>
  61. </div>`;
  62.  
  63. place.innerHTML = !isFavorites ? place.innerHTML + template : template + place.innerHTML;
  64.  
  65. var myLink = document.getElementById("randGalLink");
  66. myLink.addEventListener("click", openRandGal, true);
  67. var myPageLink = document.getElementById("randPageLink");
  68. myPageLink.addEventListener("click", openRandPage, true);
  69. })();