| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | // -*- tab-width: 4; js-indent-level: 4; -*-
 | 
					
						
							|  |  |  | // ==UserScript==
 | 
					
						
							|  |  |  | // @name         Trakt Show Picker
 | 
					
						
							|  |  |  | // @namespace    danielrayjones
 | 
					
						
							| 
									
										
										
										
											2020-08-28 16:58:29 -05:00
										 |  |  | // @version      0.0.10
 | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | // @description  Pick a show from progress page
 | 
					
						
							|  |  |  | // @author       Dan Jones
 | 
					
						
							|  |  |  | // @match        https://trakt.tv/users/*/progress*
 | 
					
						
							|  |  |  | // @grant        none
 | 
					
						
							| 
									
										
										
										
											2020-06-05 14:09:47 -05:00
										 |  |  | // @require      https://bowercdn.net/c/konami-code-1.3.2/src/jquery.konami.min.js
 | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | // ==/UserScript==
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:03:09 -05:00
										 |  |  | /* global $, compressedCache, localStorage, MutationObserver */ | 
					
						
							| 
									
										
										
										
											2020-06-05 14:48:01 -05:00
										 |  |  | /* jshint esversion: 6 */ | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function() { | 
					
						
							|  |  |  |     'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 11:35:45 -05:00
										 |  |  |     let watched_shows; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  |     $(window).konami({ | 
					
						
							|  |  |  |         code: [80, 73, 67, 75], | 
					
						
							| 
									
										
										
										
											2020-06-05 19:46:36 -05:00
										 |  |  |         cheat: pickShow | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 16:27:17 -05:00
										 |  |  |     function unghost() { | 
					
						
							|  |  |  |         $(this).removeClass('sortable-ghost'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:03:09 -05:00
										 |  |  |     function addPickButton() { | 
					
						
							|  |  |  |         const $leftNav = $('.subnav-wrapper .container .left'); | 
					
						
							|  |  |  |         const $found = $leftNav.find('.pick-episode'); | 
					
						
							|  |  |  |         if (!$found.length) { | 
					
						
							| 
									
										
										
										
											2020-08-03 19:48:33 -05:00
										 |  |  |             $leftNav.append('<span class="filter-dropdown toggle-simple-progress pick-episode" title="Pick Episode"><span class="icon trakt-icon-wand"></span></span>') | 
					
						
							| 
									
										
										
										
											2020-08-03 19:03:09 -05:00
										 |  |  |                 .find('.pick-episode').on('click', pickShow); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-01 11:35:45 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-28 16:27:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 11:35:45 -05:00
										 |  |  |     function refreshPage() { | 
					
						
							|  |  |  |         addPickButton(); | 
					
						
							|  |  |  |         $('div[data-type="show"]') | 
					
						
							|  |  |  |             .on('click', unghost); | 
					
						
							| 
									
										
										
										
											2020-08-03 19:03:09 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-03 14:01:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 11:35:45 -05:00
										 |  |  |     refreshPage(); | 
					
						
							|  |  |  |     const observer = new MutationObserver(refreshPage); | 
					
						
							|  |  |  |     observer.observe(document.head.parentElement, {childList: true}); | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 13:49:36 -05:00
										 |  |  |     function getWeightedIndex(total) { | 
					
						
							|  |  |  |         const opts = []; | 
					
						
							|  |  |  |         for ( let idx = 0; idx < total; idx++) { | 
					
						
							|  |  |  |             for (let idxInst = 0; idxInst < total - idx; idxInst++) { | 
					
						
							|  |  |  |                 opts.push(idx); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const totalWeights = opts.length; | 
					
						
							|  |  |  |         const which = Math.floor(Math.random() * totalWeights); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return opts[which]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  |     function pickShow() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ('compressedCache' in window) { | 
					
						
							|  |  |  |             watched_shows = compressedCache.get('watched_shows'); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             watched_shows = JSON.parse(localStorage.watched_shows); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let $shows = $('div[data-type="show"]'); | 
					
						
							|  |  |  |         $shows.removeClass('sortable-ghost'); | 
					
						
							|  |  |  |         $shows = filterShows($shows); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 13:49:36 -05:00
										 |  |  |         let picked = getWeightedIndex($shows.length); | 
					
						
							| 
									
										
										
										
											2020-06-05 11:59:25 -05:00
										 |  |  |         let $picked = $shows.eq(picked); | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $shows.addClass('sortable-ghost'); | 
					
						
							| 
									
										
										
										
											2020-06-05 11:59:25 -05:00
										 |  |  |         $picked.removeClass('sortable-ghost').insertAfter($picked.parent().find('.pagination-top')); | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function filterShows($shows) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Milliseconds in two days
 | 
					
						
							| 
									
										
										
										
											2020-08-28 16:28:07 -05:00
										 |  |  |         const twoDays = 2 * 24 * 60 * 60 * 1000; | 
					
						
							|  |  |  |         const twoDaysAgo = (new Date()) - twoDays; | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $shows.each(function (i, show) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 16:28:07 -05:00
										 |  |  |             const showId = Number.parseInt(show.dataset.showId); | 
					
						
							|  |  |  |             const showInfo = watched_shows[showId]; | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  |             if (!showInfo) return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-28 16:28:07 -05:00
										 |  |  |             const lastWatch = showInfo.ts*1000; | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  |             // Skip ones we've watched in the last two days
 | 
					
						
							|  |  |  |             if (lastWatch > twoDaysAgo) { | 
					
						
							|  |  |  |                 show.classList.add('sortable-ghost'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-08-28 16:28:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const percentage = show.dataset.percentage; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (percentage == 100) { | 
					
						
							|  |  |  |                 show.classList.add('sortable-ghost'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-05 11:49:56 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $shows.filter(':not(.sortable-ghost)'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | })(); |