Advertisement
Guest User

BTN Show Subtitles 1.02

a guest
Mar 28th, 2017
2,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Show Subtitles
  3. // @namespace   BTN
  4. // @description Digs through the torrent info to see if there are text streams, and if so display
  5. // @include     http*://broadcasthe.net/torrents.php?id=*
  6. // @version     1.02
  7. // @grant       none
  8. // ==/UserScript==
  9.  
  10. // changelog
  11. // 1.02 add "?id=" to include string; gracefully handle unexpected data
  12. // 1.01 fixes for Chrome/Tampermonkey compatibility
  13. // 1.00 first release
  14.  
  15. (function() {
  16.     'use strict';
  17.  
  18.     // iterate the first table row corresponding to each torrent
  19.     var iTorrents = document.evaluate('.//tr[@class="group_torrent"]', document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  20.     for (var it = 0; it < iTorrents.snapshotLength; it++) {
  21.         var eTorrent = iTorrents.snapshotItem(it);
  22.  
  23.         // get mkvinfo as an array of lines
  24.         try {
  25.            var info = eTorrent.nextElementSibling.nextElementSibling.lastElementChild.lastElementChild.textContent;
  26.         } catch (e) {
  27.             continue;
  28.         }
  29.         var lines = info.split("\n");
  30.  
  31.         // storage for single torrent subtitle data
  32.         var subs = [];
  33.         var sub;
  34.         var langs = {};
  35.         var substip = "";
  36.  
  37.         // addsub - close off a sub object and record it
  38.         var addsub = function() {
  39.             if (!sub) {
  40.                 return;
  41.             }
  42.             subs.push(sub);
  43.  
  44.             // show each language as a two-character abbreviation
  45.             var lang = sub.Language ? sub.Language.substring(0, 2).toUpperCase() : "??";
  46.             langs[lang] = (langs[lang] || 0) + 1;
  47.  
  48.             // show a tooltip with more detailed information
  49.             substip += (sub.Title || sub.Language || "Unknown") + "\n";
  50.             sub = null;
  51.         };
  52.  
  53.         // iterate the lines of the mkvinfo
  54.         var started = false;
  55.         for (var i = 0; i < lines.length; i++) {
  56.             var line = lines[i].trim();
  57.             if (line == "General") {
  58.                 // starting a new mkv
  59.                 if (started) {
  60.                     // ignore second and subsequent mkvs
  61.                     break;
  62.                 } else {
  63.                     started = true;
  64.                 }
  65.             } else if (line.startsWith("Text")) {
  66.                 // this line introduces a subtitle track
  67.                 sub = {};
  68.             } else if (!line) {
  69.                 // blank line terminates a track
  70.                 addsub(sub);
  71.             } else if (sub) {
  72.                 // record key/value pairs for the current subtitle track
  73.                 var keyval = line.split(/:(.+)/);
  74.                 if (keyval.length == 3) {
  75.                    sub[keyval[0].trim()] = keyval[1].trim();
  76.                 }
  77.             }
  78.         }
  79.         // in case there was no blank line following the last sub
  80.         addsub(sub);
  81.  
  82.         // prepare to output the subtitle info alongside torrent name
  83.         var eOut = eTorrent.nextElementSibling.firstElementChild;
  84.  
  85.         // get rid of empty trailing garbage tags that BTN emits
  86.         eOut.innerHTML = eOut.textContent;
  87.  
  88.         // create a new right-aligned element and insert it
  89.         var div = document.createElement("div");
  90.         div.style.float = "right";
  91.         div.style.textAlign = "right";
  92.         if (subs.length) {
  93.             div.innerText = Object.keys(langs).join(" ");
  94.             div.title = substip;
  95.         } else {
  96.             div.innerText = "[no subs]";
  97.         }
  98.         eOut.appendChild(div);
  99.     }
  100. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement