· 5 years ago · Jul 10, 2020, 11:32 PM
1// ==UserScript==
2// @name PTP Add missing RT links
3// @namespace http://tampermonkey.net/
4// @version 0.1
5// @match http*://*passthepopcorn.me/torrents.php?id=*
6// @grant GM_xmlhttpRequest
7// @connect www.omdbapi.com
8// ==/UserScript==
9
10(function() {
11 'use strict';
12
13 // OMDB API KEY (http://www.omdbapi.com/apikey.aspx)
14 let apiKey = '';
15
16 // look for imdbId, or stop here
17 let imdbId = (function() {
18 let imdbLink = document.getElementById('imdb-title-link');
19 if ( imdbLink ) {
20 let imdbMatch = imdbLink.href.match(/tt\d+/);
21 if ( imdbMatch ) return imdbMatch[0];
22 }
23 return false;
24 })();
25
26 // stop here if we can't get an imdbId
27 if ( ! imdbId ) return false;
28
29 // stop here if we already have a Rotten Tomatoes link on this page
30 if ( document.querySelector('#movie-ratings-table img[title="Rotten Tomatoes"]') ) return false;
31
32 GM_xmlhttpRequest({
33 method: 'GET',
34 url: 'https://www.omdbapi.com/?apikey='+ apiKey +'&tomatoes=true&i='+ imdbId, // tt#######
35 onload: function(xhr) {
36 let movie = jsonParse(xhr.responseText) || {};
37 if ( typeof movie === 'object' && Array.isArray(movie.Ratings) ) {
38 let rtRating = movie.Ratings.reduce(function(val, rtObj) {return ( ! val && typeof rtObj === 'object' && rtObj.Source === 'Rotten Tomatoes' ) ? rtObj.Value.replace('%','') : val; }, false);
39 if ( ! rtRating) return false;
40
41 // build the content for placement
42 let ratingImg = rtRating <= 59 ? 'splat.png' : 'tomatoes.png';
43 let col1 = document.createElement('td');
44 col1.setAttribute('colspane', '1');
45 col1.innerHTML = '<center><a target="_blank" href="'+ movie.tomatoURL +'"><img src="/static/common/ratings/x3_'+ ratingImg +'" style="height:64px;width:64px;" title="Rotten Tomatoes\nvia OMDB" /></a></center>';
46 let col2 = document.createElement('td');
47 col2.innerHTML = '<span class="rating">'+ rtRating +'</span> <span class="mid">/</span> <span class="outof"> 100</span>';
48
49 // find PTP rating element & place RT before it
50 console.log('Adding RT Link/Rating for '+ movie.Title, movie);
51 let ptpRatingEl = document.getElementById('ptp_rating_td');
52 ptpRatingEl.parentNode.insertBefore(col1, ptpRatingEl.previousElementSibling);
53 ptpRatingEl.parentNode.insertBefore(col2, ptpRatingEl.previousElementSibling);
54
55 // there is width being applied (in the ratings box) through JS, so we need to clear & reassign to appear correctly
56 $jq( "#movie-ratings-table tr:first td" ).each( function() { $jq(this).width(''); });
57 $jq( "#movie-ratings-table tr:first td" ).each( function() { $jq(this).width( $jq(this).width() ); });
58 }
59 }
60 });
61
62 // protect against invalid JSON strings
63 function jsonParse(json) {
64 try { return JSON.parse(json); }
65 catch(e) {}
66
67 return false;
68 };
69
70})();