Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         PTP Box Office Detector V2
  3. // @namespace    PTPBOD_abc123
  4. // @version      0.2b
  5. // @description  Finds a film's box office information when possible
  6. // @author       abc123
  7. // @include      https://passthepopcorn.me/torrents.php?id=*
  8. // @grant        GM_xmlhttpRequest
  9. // @connect      www.imdb.com
  10. // ==/UserScript==
  11.  
  12. /*****************
  13.  * AUTHOR'S NOTE *
  14.  *****************
  15.  * This script is designed for fetching box office information for any
  16.  * given movie being viewed on PassThePopcorn. It does this by querying
  17.  * IMDB and pulling their Box Office data.
  18.  *
  19.  * Big thanks to HostileThingy to made a version of this that works with
  20.  * wikipedia.
  21.  */
  22.  
  23. /***********
  24.  * OPTIONS *
  25.  ***********/
  26.  
  27. // uncomment (or add) values you wish to hide
  28. var boHideArr = [];
  29. // boHideArr.push('Budget');
  30. // boHideArr.push('Opening Weekend USA');
  31. // boHideArr.push('Gross USA');
  32. // boHideArr.push('Worldwide Gross');
  33.  
  34. /*****************
  35.  * FUNCTIONALITY *
  36.  *****************/
  37.  
  38. function queryIMDB(imdbId) {
  39.     GM_xmlhttpRequest({
  40.         method: 'GET',
  41.         url: 'https://www.imdb.com/title/tt'+ imdbId,
  42.         onload: parseIMDB
  43.     });
  44. }
  45.  
  46. function parseIMDB(response) {
  47.     var domparser = new DOMParser();
  48.     var doc = domparser.parseFromString(response.responseText, 'text/html');
  49.     if ( typeof doc !== 'object' ) return false;
  50.  
  51.     // get movie info panel of PTP
  52.     var parentEl = document.querySelector('#movieinfo > .panel__body');
  53.     if ( ! parentEl ) return false;
  54.  
  55.     // find detail area of IMDB
  56.     var details = doc.querySelector('#titleDetails');
  57.     if ( ! details ) return false;
  58.  
  59.     var inBO = false;
  60.     var detail = details.querySelectorAll('.subheading, div.txt-block');
  61.     // NOTE: forEach on nodeList won't work on early browsers... use Array.prototype
  62.     detail.forEach(function(node) {
  63.         if ( node.classList.contains('subheading') ) {
  64.             inBO = !! (node.innerText === 'Box Office');
  65.             return false;
  66.         }
  67.  
  68.         if ( inBO ) {
  69.             appendIMDBInfo(parentEl, node);
  70.         }
  71.     });
  72. }
  73.  
  74. function appendIMDBInfo(parentEl, node) {
  75.  
  76.     // find box office label
  77.     var labelSrc = node.querySelector('h4');
  78.     if ( ! labelSrc ) return false;
  79.     var label = labelSrc.innerText;
  80.     node.removeChild(labelSrc);
  81.  
  82.     // find box office detail (extra info)
  83.     var detailSrc = node.querySelector('span');
  84.     if ( detailSrc ) {
  85.         node.removeChild(detailSrc);
  86.     }
  87.  
  88.     var value = node.innerText;
  89.  
  90.     // do any extra display modifications here
  91.     label = label.replace(/Cumulative /, '');
  92.     value = value.replace(/,\s*$/, '');
  93.  
  94.     // only show budget and worldwide gross
  95.     if ( ~boHideArr.indexOf(label.replace(':','')) ) return false;
  96.  
  97.     // output to screen
  98.     var labelDiv = document.createElement('div');
  99.     var labelStrong = document.createElement('strong');
  100.     labelStrong.innerHTML = label;
  101.     labelDiv.appendChild(labelStrong);
  102.     labelDiv.innerHTML += value;
  103.     parentEl.appendChild(labelDiv);
  104. }
  105.  
  106. (function() {
  107.     'use strict';
  108.  
  109.     var imdbLink = document.getElementById('imdb-title-link');
  110.     if ( ! imdbLink ) return false;
  111.  
  112.     var imdbMatch = imdbLink.href.match(/tt(\d+)/);
  113.     if ( ! imdbMatch ) return false;
  114.  
  115.     queryIMDB(imdbMatch[1]);
  116. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement