Advertisement
jcunews

trakt-movieshow-info-list.user.js

Feb 27th, 2023 (edited)
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Trakt movie/show info list
  3. // @namespace   https://greasyfork.org/en/users/85671-jcunews
  4. // @version     1.0.1
  5. // @license     AGPL v3
  6. // @author      jcunews
  7. // @description Context: https://www.reddit.com/r/GreaseMonkey/comments/11332a7/creating_an_unlimited_watchlist_from_traktv/
  8. // @match       https://trakt.tv/*
  9. // @grant       none
  10. // ==/UserScript==
  11.  
  12. (lst => {
  13.   function bmiaddelClick(a, b, c, d) {
  14.     a = this.closest('[data-show-id],[data-episode-id],[data-movie-id]');
  15.     if (this.classList.contains("del")) { //delete
  16.       delete lst[a.dataset.type + (a.dataset.episodeId || a.dataset.showId || a.dataset.movieId)];
  17.       this.title = "Add to watchlist"
  18.     } else { //add
  19.       b = a.querySelector('.titles-link>*'); //title with optional year
  20.       c = a.querySelector('.titles-link+.titles-link'); //optional episode
  21.       d = a.querySelector('.titles meta[itemprop="datePublished"]'); //optional episode time
  22.       lst[a.dataset.type + (a.dataset.episodeId || a.dataset.showId || a.dataset.movieId)] =
  23.         "[" + (a.dataset.type === "show" ? "TV" : a.dataset.type === "episode" ? "Episode" : "Movie") + "] " + //type
  24.         b.firstChild.data.trim() + (b.firstElementChild ? " (" + b.firstElementChild.textContent.trim() + ")": "") + //title with optional year
  25.         (c ? " (" + c.textContent.trim() + ")": "") + //optional episode & local time
  26.         (d ? " {" + (new Date(d.content.replace(" UTC", "Z"))).toDateString() + "}": "");
  27.       this.title = "Remove from watchlist"
  28.     }
  29.     localStorage.movieInfoList = JSON.stringify(lst);
  30.     this.classList.toggle("del")
  31.   }
  32.   function getData() {
  33.     lst = JSON.parse(localStorage.movieInfoList || "{}");
  34.     document.querySelectorAll(':is([data-show-id],[data-episode-id],[data-movie-id]) .actions').forEach((e, a) => {
  35.       a = e.closest('[data-show-id],[data-episode-id],[data-movie-id]');
  36.       if (!(b = e.querySelector(".bmiaddel"))) {
  37.         e.insertAdjacentHTML("beforeend", '<a class="bmiaddel" title="Add to watchlist">&#x1f441;</a>');
  38.         (b = e.lastElementChild).onclick = bmiaddelClick
  39.       }
  40.       if (lst[a.dataset.type + (a.dataset.episodeId || a.dataset.showId || a.dataset.movieId)]) {
  41.         b.classList.add("del")
  42.       } else b.classList.remove("del")
  43.     });
  44.   }
  45.   document.querySelector('#top-nav .navbar-nav.brand-right').insertAdjacentHTML("beforeend", `
  46. <li>
  47.   <style>
  48.     #bmishow { margin-left: 2em; border-radius: .3em }
  49.     .bmiaddel { color: #77d }
  50.     .bmiaddel:hover { background: #335; color: #99f }
  51.     .bmiaddel.del { background: #33b }
  52.   </style>
  53.   <a id="bmishow" href="javascript:void(0)">Watchlist</a>
  54. </li>`);
  55.   document.querySelector('#bmishow').onclick = (l, a) => {
  56.     if (localStorage.movieInfoList) {
  57.       if (confirm(`Save below movie/show info list?\n\n${l = Object.values(lst).join("\n")}`)) {
  58.         (a = document.createElement("A")).href = URL.createObjectURL(new Blob([l], {type: "text/plain"}));
  59.         a.download = "movieshow-info-list.txt";
  60.         a.click();
  61.         setTimeout(() => URL.revokeObjectURL(a.href), 10000);
  62.       }
  63.     } else alert("Movie/show info list is empty.");
  64.   };
  65.   addEventListener("focus", getData);
  66.   getData()
  67. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement