Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Show Subtitles
- // @namespace BTN
- // @description Digs through the torrent info to see if there are text streams, and if so display
- // @include http*://broadcasthe.net/torrents.php?id=*
- // @version 1.02
- // @grant none
- // ==/UserScript==
- // changelog
- // 1.02 add "?id=" to include string; gracefully handle unexpected data
- // 1.01 fixes for Chrome/Tampermonkey compatibility
- // 1.00 first release
- (function() {
- 'use strict';
- // iterate the first table row corresponding to each torrent
- var iTorrents = document.evaluate('.//tr[@class="group_torrent"]', document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
- for (var it = 0; it < iTorrents.snapshotLength; it++) {
- var eTorrent = iTorrents.snapshotItem(it);
- // get mkvinfo as an array of lines
- try {
- var info = eTorrent.nextElementSibling.nextElementSibling.lastElementChild.lastElementChild.textContent;
- } catch (e) {
- continue;
- }
- var lines = info.split("\n");
- // storage for single torrent subtitle data
- var subs = [];
- var sub;
- var langs = {};
- var substip = "";
- // addsub - close off a sub object and record it
- var addsub = function() {
- if (!sub) {
- return;
- }
- subs.push(sub);
- // show each language as a two-character abbreviation
- var lang = sub.Language ? sub.Language.substring(0, 2).toUpperCase() : "??";
- langs[lang] = (langs[lang] || 0) + 1;
- // show a tooltip with more detailed information
- substip += (sub.Title || sub.Language || "Unknown") + "\n";
- sub = null;
- };
- // iterate the lines of the mkvinfo
- var started = false;
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i].trim();
- if (line == "General") {
- // starting a new mkv
- if (started) {
- // ignore second and subsequent mkvs
- break;
- } else {
- started = true;
- }
- } else if (line.startsWith("Text")) {
- // this line introduces a subtitle track
- sub = {};
- } else if (!line) {
- // blank line terminates a track
- addsub(sub);
- } else if (sub) {
- // record key/value pairs for the current subtitle track
- var keyval = line.split(/:(.+)/);
- if (keyval.length == 3) {
- sub[keyval[0].trim()] = keyval[1].trim();
- }
- }
- }
- // in case there was no blank line following the last sub
- addsub(sub);
- // prepare to output the subtitle info alongside torrent name
- var eOut = eTorrent.nextElementSibling.firstElementChild;
- // get rid of empty trailing garbage tags that BTN emits
- eOut.innerHTML = eOut.textContent;
- // create a new right-aligned element and insert it
- var div = document.createElement("div");
- div.style.float = "right";
- div.style.textAlign = "right";
- if (subs.length) {
- div.innerText = Object.keys(langs).join(" ");
- div.title = substip;
- } else {
- div.innerText = "[no subs]";
- }
- eOut.appendChild(div);
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement