F95 Random Latest Update Choice

Randomly selects a resource(e.g. game,asset,animation) from the 'Latest Update' page. Just press the '?' button in the filter drawer.

  1. // ==UserScript==
  2. // @name F95 Random Latest Update Choice
  3. // @namespace 1330126-edexal
  4. // @match *://f95zone.to/sam/latest_alpha/*
  5. // @icon https://external-content.duckduckgo.com/ip3/f95zone.to.ico
  6. // @grant none
  7. // @version 1.0.4
  8. // @author Edexal
  9. // @license Unlicense
  10. // @description Randomly selects a resource(e.g. game,asset,animation) from the 'Latest Update' page. Just press the '?' button in the filter drawer.
  11. // ==/UserScript==
  12. (() => {
  13. const LATEST_PAGE_QUERY = 'lppos';
  14. let stylesCSS = `
  15. .fa-question {
  16. color: yellow;
  17. background:#641c1c;
  18. font-size: 15px;
  19. }
  20.  
  21. .fa-question:hover{
  22. cursor: pointer;
  23. opacity: 0.7;
  24. }
  25.  
  26. .fa-question::before {
  27. content: "\\3f";
  28. }
  29.  
  30. /*Tooltip for button*/
  31. #filter-random::before {
  32. content: "lucky";
  33. }
  34.  
  35. /*Chosen item*/
  36. #chosen {
  37. box-shadow: #c00 1px 0 10px 8px !important;
  38. }
  39. #chosen > a.resource-tile_link {
  40. color: yellow !important;
  41. text-shadow: #cc0000 1px 0 8px;
  42. }
  43.  
  44. /*Button Labels*/
  45. aside#latest-page_filter-wrap
  46. div#latest-page_filter-wrap_inner
  47. div.content-block_filter
  48. h3.content-block_filter-title
  49. div#filter-controls a::before{
  50. right:105px;
  51. }
  52.  
  53. aside#latest-page_filter-wrap
  54. div#latest-page_filter-wrap_inner
  55. div.content-block_filter
  56. h3.content-block_filter-title
  57. div#filter-controls a:hover::before{
  58. width: 135px;
  59. color: #ecac31;
  60. }
  61.  
  62. `;
  63.  
  64. //Random number
  65. function getRandIntInc(min, max) {
  66. //Inclusive random number generator
  67. return Math.floor(Math.random() * (max - min + 1) ) + min;
  68. }
  69. //Apply custom styles in a style tag
  70. function applyCSS(css) {
  71. let styleEl = document.querySelector("style");
  72. if (styleEl === null) {
  73. styleEl = document.createElement('style');
  74. }
  75. styleEl.appendChild(document.createTextNode(css));
  76. document.head.appendChild(styleEl);
  77. }
  78.  
  79. //Chooses a random item on the page
  80. function selectAnItem(){
  81. if(!location.search.includes(LATEST_PAGE_QUERY)) {
  82. return;
  83. }
  84. //Change the URL to one w/o 'lppos' query param
  85. let curURL = new URL(location.href);
  86. curURL.searchParams.delete(LATEST_PAGE_QUERY);
  87. history.replaceState({},"",curURL.toString());
  88.  
  89. //Selects a random item on the page
  90. let allItemsEls = document.querySelectorAll(".resource-tile"); // Gets all resource tiles
  91. let randNumChoice = getRandIntInc(1, allItemsEls.length);
  92. let chosenEl = allItemsEls[randNumChoice - 1];
  93. chosenEl.id = "chosen";
  94. chosenEl.scrollIntoView(false);//scroll selected bookmark into view from the bottom of the page
  95. }
  96. //Chooses a random page and goes to it
  97. function goToAPage() {
  98. let maxPageNum = document.querySelector(".sub-nav_info-paging_nums .nav_num:last-child").dataset.page;
  99. let chosenNum = getRandIntInc(1,+maxPageNum);
  100. let curURL = new URL(location.href);//Get the current URL
  101. //Customize URL to add latest page query parameter
  102. curURL.searchParams.set(LATEST_PAGE_QUERY,1);
  103. //Change page to random one
  104. let newURL = !curURL.toString().includes("page=") ? `${curURL.toString()}#/cat=games/page=${chosenNum}` :
  105. curURL.toString().replace(/page=\d+/i,`page=${chosenNum}`);
  106.  
  107. location.replace(newURL);//Go To randomly chosen bookmark page
  108. }
  109.  
  110. //Creates the random button
  111. function makeRandbtn(){
  112. let randLinkEl = document.createElement("a");
  113. randLinkEl.classList.add("button-icon");
  114. randLinkEl.id = "filter-random";
  115.  
  116. let randIconEl = document.createElement("i");
  117. randIconEl.classList.add("fas", "fa-question");
  118.  
  119. let parentEl = document.querySelector("#filter-controls");
  120. randLinkEl.addEventListener("click",goToAPage);
  121. randLinkEl.append(randIconEl);
  122. parentEl.insertAdjacentElement('afterbegin',randLinkEl);
  123. }
  124.  
  125. function run(){
  126. applyCSS(stylesCSS);
  127. makeRandbtn();
  128. setTimeout(selectAnItem, 2500); // [VALUE] Wait for elements to load before executing
  129. }
  130.  
  131. run();
  132.  
  133. })();