JAVLibrary - Highlight Owned Movies

Highlight owned movies on JAVLibrary

  1. // ==UserScript==
  2. // @author jvlflame
  3. // @name JAVLibrary - Highlight Owned Movies
  4. // @version 0.0.4
  5.  
  6. // @include https://*javlibrary.com/*/
  7. // @include *://*/movie/*
  8. // @include *://*/cn*
  9. // @include *://*/tw*
  10. // @include *://*/ja*
  11. // @include *://*/en*
  12.  
  13. // @description Highlight owned movies on JAVLibrary
  14.  
  15. // @namespace https://greasyfork.org/users/63118
  16. // ==/UserScript==
  17.  
  18. let styles = `
  19. .owned {
  20. background: cornflowerblue;
  21. color: black;
  22. }
  23. `;
  24. let styleSheet = document.createElement("style");
  25. styleSheet.type = "text/css";
  26. styleSheet.innerText = styles;
  27. document.head.appendChild(styleSheet);
  28.  
  29. var buttonDiv = document.createElement("div");
  30. buttonDiv.innerHTML =
  31. '<button id="refresh-owned-btn" type="button">' +
  32. "Refresh Owned Movies</button>";
  33. buttonDiv.setAttribute("class", "refreshowned category");
  34. document.querySelector(".menul1").appendChild(buttonDiv);
  35.  
  36. //--- Activate the newly added button.
  37. document
  38. .getElementById("refresh-owned-btn")
  39. .addEventListener("click", ButtonClickAction, false);
  40.  
  41. function ButtonClickAction(zEvent) {
  42. // Write owned movie list to localstorage so we don't have to
  43. // fetch the list on every page
  44. aggregateMovieList();
  45. }
  46.  
  47. function sleep(ms) {
  48. return new Promise((resolve) => setTimeout(resolve, ms));
  49. }
  50.  
  51. function updateDOM() {
  52. //const moviesOnCurrentPage = [];
  53. const moviesOnCurrentPage = document.querySelectorAll(".video");
  54.  
  55. // We can parse the list more easily as an array
  56. if (localStorage.getItem("ownedMovies")) {
  57. const ownedMovies = localStorage.getItem("ownedMovies").split(",");
  58. try {
  59. for (let i = 0; i < moviesOnCurrentPage.length; i++) {
  60. if (
  61. ownedMovies.includes(
  62. moviesOnCurrentPage[i].children[0].attributes[0].value.split(
  63. "?v="
  64. )[1]
  65. )
  66. ) {
  67. moviesOnCurrentPage[i].children[0].children[0].classList.add("owned");
  68. }
  69. }
  70. } catch (err) {
  71. console.log(err);
  72. }
  73. }
  74. }
  75.  
  76. const movieList = [];
  77.  
  78. function setMoviesToLocalStorage(ownedMovies) {
  79. const ownedMovieList = ownedMovies.join(",");
  80. ownedMovies.forEach((movie) => {
  81. movieList.push(movie);
  82. });
  83. localStorage.setItem("ownedMovies", ownedMovieList);
  84. }
  85.  
  86. function getOwnedMovieCount() {
  87. const movieCount = fetch(
  88. `https://www.javlibrary.com/en/userowned.php?list&mode=&u=${getCookie(
  89. "userid"
  90. )}&page=0`,
  91. {
  92. headers: {
  93. accept:
  94. "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
  95. "accept-language": "en-US,en;q=0.9",
  96. "cache-control": "no-cache",
  97. pragma: "no-cache",
  98. "sec-fetch-dest": "empty",
  99. "sec-fetch-mode": "cors",
  100. "sec-fetch-site": "same-origin",
  101. "sec-gpc": "1",
  102. "upgrade-insecure-requests": "1",
  103. },
  104. referrer: `https://www.javlibrary.com/en/userowned.php?list&mode=&u=${getCookie(
  105. "userid"
  106. )}`,
  107. referrerPolicy: "strict-origin-when-cross-origin",
  108. body: null,
  109. method: "GET",
  110. mode: "cors",
  111. credentials: "include",
  112. }
  113. )
  114. .then((res) => res.text())
  115. .then((html) => {
  116. // Initialize the DOM parser
  117. var parser = new DOMParser();
  118.  
  119. // Parse the text
  120. var doc = parser.parseFromString(html, "text/html");
  121.  
  122. return doc.querySelector(".boxtitle").innerHTML.split(": ")[1];
  123. });
  124.  
  125. return movieCount;
  126. }
  127.  
  128. function getOwnedMoviesByPage(pageNum) {
  129. const data = fetch(
  130. `https://www.javlibrary.com/en/userowned.php?list&mode=&u=${getCookie(
  131. "userid"
  132. )}&page=${pageNum}`,
  133. {
  134. headers: {
  135. accept:
  136. "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
  137. "accept-language": "en-US,en;q=0.9",
  138. "cache-control": "no-cache",
  139. pragma: "no-cache",
  140. "sec-fetch-dest": "document",
  141. "sec-fetch-mode": "navigate",
  142. "sec-fetch-site": "same-origin",
  143. "sec-fetch-user": "?1",
  144. "sec-gpc": "1",
  145. "upgrade-insecure-requests": "1",
  146. cookie: `${document.cookie}`,
  147. },
  148. referrer: `https://www.javlibrary.com/en/userowned.php?list&mode=&u=${getCookie(
  149. "userid"
  150. )}`,
  151. referrerPolicy: "strict-origin-when-cross-origin",
  152. body: null,
  153. method: "GET",
  154. mode: "cors",
  155. }
  156. )
  157. .then((res) => res.text())
  158. .then((html) => {
  159. // Initialize the DOM parser
  160. var parser = new DOMParser();
  161.  
  162. // Parse the text
  163. var doc = parser.parseFromString(html, "text/html");
  164.  
  165. const vidsOnPage = doc.querySelectorAll(".video");
  166. const ownedMovies = [];
  167. for (let i = 0; i < vidsOnPage.length; i++) {
  168. const movieId = ownedMovies.push(
  169. vidsOnPage[i].children[1].attributes[0].value.split("?v=")[1]
  170. );
  171. }
  172. return ownedMovies;
  173. });
  174.  
  175. return data;
  176. }
  177.  
  178. function getOwnedMovies() {
  179. // fetch owned movies from the javlibrary owned movies list
  180. fetch("https://www.javlibrary.com/en/mv_owned_print.php", {
  181. headers: {
  182. accept: "*/*",
  183. "accept-language": "en-US,en;q=0.9",
  184. "cache-control": "no-cache",
  185. pragma: "no-cache",
  186. "sec-fetch-dest": "empty",
  187. "sec-fetch-mode": "cors",
  188. "sec-fetch-site": "same-origin",
  189. "sec-gpc": "1",
  190. },
  191. referrer: "https://www.javlibrary.com/en/myaccount.php",
  192. referrerPolicy: "strict-origin-when-cross-origin",
  193. body: null,
  194. method: "GET",
  195. mode: "cors",
  196. credentials: "include",
  197. })
  198. .then((res) => {
  199. // return the raw text
  200. return res.text();
  201. })
  202. .then((html) => {
  203. // Initialize the DOM parser
  204. var parser = new DOMParser();
  205.  
  206. // Parse the text
  207. var doc = parser.parseFromString(html, "text/html");
  208.  
  209. const ownedMovies = [];
  210. const titles = doc.querySelectorAll(".title");
  211. titles.forEach((title) => {
  212. if (title.innerHTML !== "Title") {
  213. ownedMovies.push(title.innerHTML.split(" ")[0].replace("\n", ""));
  214. }
  215. });
  216. return ownedMovies;
  217. })
  218. .then((movies) => {
  219. setMoviesToLocalStorage(movies);
  220. })
  221. .catch((err) =>
  222. window.alert(`Failed to fetch and/or set owned movie list ${err}`)
  223. );
  224. }
  225.  
  226. async function aggregateMovieList() {
  227. console.time("Get owned movies");
  228. const movieList = [];
  229. const movieCount = await getOwnedMovieCount();
  230. const pageCount = await Math.ceil(movieCount / 20);
  231.  
  232. for (let i = 0; i <= pageCount; i++) {
  233. document.getElementById(
  234. "refresh-owned-btn"
  235. ).innerHTML = `Please wait ${i} / ${pageCount} (Do not close this page)`;
  236. const movies = await getOwnedMoviesByPage(i);
  237. for (let j = 0; j < movies.length; j++) {
  238. if (movies[j] !== "") {
  239. movieList.push(movies[j]);
  240. }
  241. }
  242. await sleep(100);
  243. }
  244.  
  245. setMoviesToLocalStorage(movieList);
  246. document.getElementById("refresh-owned-btn").innerHTML = "Completed!";
  247. console.timeEnd("Get owned movies");
  248. }
  249.  
  250. updateDOM();