F95 Random Bookmark Picker

Randomly picks a bookmark from your f95-like bookmark page. Just press the 'Random' button.

  1. // ==UserScript==
  2. // @name F95 Random Bookmark Picker
  3. // @namespace 1330126-edexal
  4. // @license Unlicense
  5. // @version 1.1.2.1
  6. // @description Randomly picks a bookmark from your f95-like bookmark page. Just press the 'Random' button.
  7. // @author Edexal
  8. // @match *://f95zone.to/account/bookmarks*
  9. // @icon https://external-content.duckduckgo.com/ip3/f95zone.to.ico
  10. // @grant none
  11. // ==/UserScript==
  12. (() => {
  13. 'use strict';
  14. const BOOKMARK_QUERY = 'bmpos';
  15. //Apply custom styles in a style tag
  16. function applyCSS(css) {
  17. let styleEl = document.querySelector("style");
  18. if (styleEl === null) {
  19. styleEl = document.createElement('style');
  20. }
  21. styleEl.appendChild(document.createTextNode(css));
  22. document.head.appendChild(styleEl);
  23. }
  24.  
  25. //CSS styles
  26. let stylesCSS = (`
  27. #randbtn{
  28. color:yellow;
  29. border: 1px solid #343638;
  30.  
  31. }
  32. #randbtn:hover{
  33. cursor: pointer;
  34. opacity: 0.7;
  35. }
  36. #randbtn:active{
  37. background-color: #ec5555;
  38. }
  39. #chosen{
  40. color: yellow;
  41. text-shadow: #cc0000 1px 0 8px;
  42. display:inherit !important;
  43. }
  44. #randContainer{
  45. margin-top:20px;
  46. }
  47. `);
  48.  
  49. function getRandIntInc(min, max) {
  50. //Inclusive random number generator
  51. return Math.floor(Math.random() * (max - min + 1) ) + min;
  52. }
  53.  
  54. function highlightBookmarkChoice(){
  55. if(!location.search.includes(BOOKMARK_QUERY)) {
  56. return;
  57. }
  58. let newURL = location.href.substring(0,
  59. location.search.includes('&'+BOOKMARK_QUERY) !== -1 ? location.href.indexOf('&'+BOOKMARK_QUERY) : location.href.indexOf(BOOKMARK_QUERY));// removes '&bmpos' or 'bmpos' from URL
  60. history.replaceState({},"",newURL); //Change the URL to one w/o 'bmpos' query param
  61.  
  62. let bookmarkListEl = document.querySelector("ol.listPlain");
  63.  
  64. let randBookmarkPos = getRandIntInc(0,bookmarkListEl.children.length - 1);// Pick a random bookmark position (usually 0-19, total 20)
  65. let chosenBookmarkEl = bookmarkListEl.children[randBookmarkPos];
  66. chosenBookmarkEl.id = "chosen";
  67. chosenBookmarkEl.scrollIntoView(false); //scroll selected bookmark into view from the bottom of the page
  68. }
  69. highlightBookmarkChoice();
  70.  
  71. let randBtn = document.createElement('button');
  72. //Prepare button attributes
  73. randBtn.setAttribute("name","random");
  74. randBtn.setAttribute("type","button");
  75. randBtn.id = "randbtn";
  76. //F95zone style classes
  77. randBtn.classList.add("pageNav-jump","pageNav-jump--next");
  78.  
  79. //Prepare button text
  80. let randTxt = document.createTextNode('Random');
  81. randBtn.appendChild(randTxt);
  82.  
  83. //Pick bookmark from a selection of all pages
  84. function pickRandomBookmark(){
  85. let pagination = document.querySelector("ul.pageNav-main");//Select pagination
  86. let lastPageNum = pagination.children[pagination.children.length-1].children[0].textContent;//Get the last page number of the pagination
  87. let randPageChoiceNum = getRandIntInc(1,Number(lastPageNum));//Pick a random page number
  88. let curURL = new URL(location.href);//Get the current URL
  89. curURL.searchParams.set("page",randPageChoiceNum);//Customize URL to point to the randomly chosen page number
  90.  
  91. //Customize URL to add bookmark query parameter
  92. curURL.searchParams.set("bmpos",1);
  93.  
  94. //Go To randomly chosen bookmark page
  95. location.replace(curURL.toString());
  96. }
  97.  
  98. //Pick bookmark on the first page (if 1 page is only available)
  99. function pickRandBookmarkOne() {
  100. let curURL = new URL(location.href);//Get the current URL
  101. curURL.searchParams.set("page",1);//Customize URL to point to the randomly chosen page number
  102.  
  103. //Customize URL to add bookmark query parameter
  104. curURL.searchParams.set("bmpos",1);
  105.  
  106. //Go To randomly chosen bookmark page
  107. location.replace(curURL.toString());
  108. }
  109.  
  110. //Add button to F95
  111. let entirePaginationEl = document.querySelector("nav > div.pageNav");
  112. //Checks if there is only a single page of bookmarks or more
  113. if(!!entirePaginationEl){
  114. // > 1
  115. let isPageNavVisible = window.getComputedStyle(document.querySelector(".pageNavWrapper--mixed .pageNav")).getPropertyValue("display") === "none";
  116. if(isPageNavVisible){
  117. //Mobile Pagination (w/ arrows)
  118. let randContainerEl = document.createElement("div");
  119. randContainerEl.id = "randContainer";
  120. randContainerEl.appendChild(randBtn);
  121. let pageArrowsEl = document.querySelector(".block-outer--after");
  122. pageArrowsEl.appendChild(randContainerEl);
  123. }else{
  124. //Desktop Pagination
  125. entirePaginationEl.appendChild(randBtn);
  126. }
  127. randBtn.addEventListener("click", pickRandomBookmark);
  128.  
  129. }else{
  130. // == 1
  131. document.querySelector("div > div.breadcrumb.p-breadcrumb--bottom").prepend(randBtn);
  132. randBtn.addEventListener("click", pickRandBookmarkOne);
  133. stylesCSS = stylesCSS + `
  134. #randbtn {
  135. position:relative;
  136. left: 50%;
  137. width:150px;
  138. height:40px;
  139. font-size: 16px;
  140. border: 1pt inset navajowhite;
  141. box-shadow: 0 1px 5px #988787;
  142. border-radius: 50px;
  143. }
  144. `;
  145. }
  146. applyCSS(stylesCSS);
  147.  
  148. })();