Advertisement
junh1024

RCH CBH

Feb 19th, 2017
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 30.81 KB | None | 0 0
  1. // ==UserScript==
  2. // @name        PTH :: Collage Building Helper
  3. // @author      Z4ppy - Updated for PTH by jungletek
  4. // @namespace   jungletek.redacted.ch
  5. // @description Make it easier to build your collage.
  6. // @include     https://*redacted.ch/torrents.php*
  7. // @include     https://*redacted.ch/collages.php*
  8. // @include     https://*redacted.ch/collage.php*
  9. // @include     https://*redacted.ch/artist.php*
  10. // @include     https://*redacted.ch/bookmarks.php*
  11.  
  12.  
  13. // @grant       GM_setValue
  14. // @grant       GM_getValue
  15. // @grant       GM_deleteValue
  16. // @grant       GM_registerMenuCommand
  17. // @grant       GM_xmlhttpRequest
  18. // @version     1.0.1
  19. // @date        2016-12-01
  20. // ==/UserScript==
  21.  
  22. // Changelog can be found at https://userscripts.org/topics/118127
  23.  
  24. var collageID, collName;
  25. // Momenticons: http://findicons.com/icon/263495/folder_open_add
  26. var ICON_ADD = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACmUlEQVR4nIWRS0hVURSGv33Ovp57j0oqV7Qkil5mEwNDKoILikQjg6Jykg0qIrIkwkySbOANahBYCj0oigoLCyxqpFRIUHTLHlb0tCJTTK2b9+HjnLMbHDOjsjXca+1vfaxfKKVoOTUz/01X9N7IcBQAw5uMIWMFGyoHb/KfEsdqfGtT/HMb16xaCdHngILEbC5dvsrAlzdFW2riLZMCjtdmqk2l61H9t0BZgAPCQKQu5UxjE+Fw3/iwLr1k+FN9q7e9Hfr5JuOxMERegh0HoSN0AWoYwm2UFueCNNxJI5OO1zYfX7eKiQYSAMdGKRvLcjh7rZPYsBprP/ubdaxurwmApusfxgCjaFjcbu+ndONJpPMJhQ1MXOagCQVCgPJA2iKOHij56AJsBysWQU8rwjPchoo8RCg14bMABEoIUAphzuTtg26SszJ3ugAsbj+OEChejDNwEWVFQGhuSynQvOjmbPDNB6FD0ixaL1Tc3VzdFXIBQ1FsfwEy1o4T63PFhZsoykIz02joinDgfT0CRWnKEraXXAyOH/FWRz+BwCrUl3MwEgc0lyAT0BIyICmPnr4eqgM7kB548qjje/qcZRaQKQFsXzZG9D5WtMdVdlNHmnNp+Kpz5Ml5wqMRtqabSAmXum+YN05nXS5buKtJHN7jVeVlJ6D3Cox8++UuJGSsYHpLkOpAOb3RTtq7WvEmQv6MIqYmz2B382GklB7q6jf/LW9SprxD+B10zUbXwPCBYehIXSAlCARC/RbXHzXv0P3g/hMd9cWDo4MJlcvX6bqE4PVGO8mTPFK2cFfTpIBXn1t986YVpgB5++5UHVywwMyRmuDps+iLmqXBCmAsxn+tn1YYB+JAyG+mhyqba3NAsDO3KgSEgJ4f88//Bf3Zb/0AAAAASUVORK5CYII=';
  27. // Yusuke Kamiyamane: http://findicons.com/icon/118036/cross
  28. var ICON_ERR = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAX5JREFUeNrEU71OwlAUPrVqrDFBHsARX6AJri4kJSEyEBMHggkLK8EEFsauvoPo4iN04gkgLiYOOBHUxDSUCBO0XL9z7a1t1YnBJl967/l+enIOaEII2uTZog2fbXW41zTSiSwcbaAbEDl+TJTmLsLO4x1YIOzzTscMvoRWmjtrNn9yPAPGHdFg3W6Lx1JJLFstwXfACjF4r9clN6lWJad8UUAPwhsQXqMh+vm8eK3VBN8ZT+WyrA0LBVljrfJpags9zMBnAi2eVirm82hEmUyGFosFvUwmZBgGzcfjoYYZYCbOZehLBKzwDsKQo1zOnLqu5HRdpw/XlWYM09lBTQVEW3gDdsMChLSazWhfkUEgw9nCm1n+9TuAwILAPs5mTcP36QA1Ba4xJ5LbSa4RX7FPIFSmB88bMtSduVVqjVEAE8WYuQ8jvthl9GMhxe+QZMAcwlsI93DmN9/RrsP4jVO+aAtX2IKH1tZIR2o3C+NhKJoBae46vcZ/+zd+CjAAN5vaL1x3kMMAAAAASUVORK5CYII=';
  29. // Yusuke Kamiyamane: http://findicons.com/icon/119015/tick
  30. var ICON_OK = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAehJREFUeNpi/P//PwMlgImBQsACIhhnCAFZjEAGkANy0B8gwQ00m5kRogpEMYHl5YF4LZBXw/D3/47/Ea/xuOD7PwaGX1AMMvDvfwGGf/8nprkkGQPZLUAVHqR4gY3hH0Ofoby+6ZcvXxjinWL0Gf4wtMC9QAAwAjXXK0jIO8gLyUl9/Pzx98Fjh24AXdOA7gIPoP/PwJwGdvoPoNP/MGQIcwkGq4mrKHz5+uXf0UtH7wBdMRGodgvCgP9ATX/+tyT5JxqCaDAfIu7DwcSWJckjqfzgwQOGU5dP3f3w4cNSoJq5DL//I0Xjn38tMX4xes+ePWOK8IowAPGBmnNZ/jPVS4vLqH7985Xl5YcXDz99+rAJGIDtQAxxIcKA/zVLViy8xM7J9uvU7VPMDnaOOkAb4sVkxTV+sPxgf/fhzdOP797vZ/gLjD4Ghn8oAQRKiYx9/AxADaAwaDF2NtN6+vMZpwCnAMP7b+8Zfrz49vrj3fdHGJgZkhhYmT4wsELTC8iHWe+RAvE/ww6g02vO7jhxjfkv49fXjK8Zvn/5+uHTzXcXgSGeC1TxAWdKRDHk81+Gp5vutnCZ8Mt8v/DpDtCBeUBrnuJNyigAmEQZ3v9h+LbzTQsw+ZYwCLJcBydjXIlkwHMjQIABAIHQ3hY9qLekAAAAAElFTkSuQmCC';
  31. // ajaxload.info
  32. var ICON_LOADING = 'data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==';
  33.  
  34. var torrentCache = {
  35.     Cache: [],
  36.     GM_read: function () {
  37.         this.clear();
  38.         this.Cache = JSON.parse(GM_getValue('torrentCache', '[]'));
  39.         return this.Cache.length;
  40.     },
  41.     GM_save: function () {
  42.         return GM_setValue('torrentCache', JSON.stringify(this.Cache));
  43.     },
  44.     GM_delete: function () {
  45.         GM_deleteValue('torrentCache');
  46.     },
  47.     add: function (torrentgroupid) {
  48.         if (this.Cache.indexOf(torrentgroupid) != -1) {
  49.             return false;
  50.         }
  51.         this.Cache.push(torrentgroupid);
  52.         return true;
  53.     },
  54.     contains: function (torrentgroupid) {
  55.         return (this.Cache.indexOf(torrentgroupid) != -1);
  56.     },
  57.     remove: function (torrentgroupid) {
  58.         if (this.Cache.indexOf(torrentgroupid) == -1) {
  59.             return false;
  60.         }
  61.         this.Cache.splice(this.Cache.indexOf(torrentgroupid), 1);
  62.         return true;
  63.     },
  64.     clear: function () {
  65.         this.Cache.length = 0;
  66.     }
  67. }
  68.  
  69. var URL = new Object();
  70. function parseURL() {
  71.     var URLArr = document.URL.match(/^(https:\/\/(?:ssl\.)?redacted\.ch)\/([\w\/\.]+?)(\?(.*?))?(#.*)?$/);
  72.     URL.Host = URLArr[1]; //protocol + hostname, eg. https://redacted.ch
  73.     URL.Path = URLArr[2];
  74.     URL.Hash = URLArr[5];
  75.     URL.Parameters = new Object();
  76.     if (typeof URLArr[4] != 'undefined') {
  77.         var URLPArr = URLArr[4].split('&'); //URL parameter array
  78.         var tmpP; //temporary Parameter
  79.         for (var i = 0; i < URLPArr.length; ++i) {
  80.             tmpP = URLPArr[i].split('=');
  81.             URL.Parameters[tmpP[0]] = tmpP[1];
  82.         }
  83.     }
  84. }
  85.  
  86. function init() {
  87.     parseURL();
  88.  
  89.     getCollageID();
  90.  
  91.     torrentCache.GM_read();
  92.  
  93.     // putting the add image on various pages (with an active collage)
  94.     // $.inArray(URL.Path, ['torrents.php', 'artist.php'])
  95.     if ((URL.Path == 'torrents.php' || URL.Path == 'artist.php' || URL.Path == 'bookmarks.php' || URL.Path == 'collages.php') && (collageID != 0)) {
  96.         if (URL.Path == 'torrents.php' && URL.Parameters['id']) {
  97.             //torrent group page
  98.             var title = document.getElementById('content').getElementsByTagName('h2')[0];
  99.             title.insertBefore(document.createTextNode(' '), null);
  100.             title.insertBefore(createAddIcon(URL.Host + '/torrents.php?id=' + URL.Parameters['id'], URL.Parameters['id']), null);
  101.         } else {
  102.             var tableId = '';
  103.             if (URL.Path == 'torrents.php' && (URL.Parameters['type'] || URL.Parameters['action'] == 'notify')) {
  104.                 // user uploaded/snatched/etc and notifications
  105.                 tableId = 'content';
  106.             } else if (URL.Path == 'artist.php' || (URL.Path == 'collages.php' && URL.Parameters['id'] && URL.Parameters['id'] != collageID)) {
  107.                 // artist page; other collages
  108.                 tableId = 'discog_table';
  109.             } else if (URL.Path == 'torrents.php' || URL.Path == 'bookmarks.php') {
  110.                 // search page; bookmarks
  111.                 tableId = 'torrent_table';
  112.             }
  113.  
  114.             if (tableId != '') {
  115.                 var linkArr = extractTorrentgroupLinks(tableId);
  116.                 for (var i = 0; i < linkArr.length; ++i) {
  117.                     linkArr[i].parentNode.insertBefore(document.createTextNode(' '), linkArr[i].parentNode.firstChild);
  118.                     linkArr[i].parentNode.insertBefore(createAddIcon(linkArr[i].href, linkArr[i].getAttribute('torrentgroupid')), linkArr[i].parentNode.firstChild);
  119.                 }
  120.             }
  121.         }
  122.     }
  123.    
  124.     if (URL.Path == 'collages.php' && URL.Parameters['id'] && !URL.Parameters['action']) {
  125.         var currentCollURL = URL.Parameters['id'];
  126.  
  127.         var cx = document.getElementsByTagName('h2')[0];
  128.  
  129.         if (collageID != 0 && collageID != currentCollURL) {
  130.             // if there is an active collage & the active collage is different than current page, use name + link
  131.             cx.parentNode.innerHTML = "\<h3 id=\"actcollage\"\>The active collage is: \<a href\=\"" + URL.Host + "\/collages.php\?id\=" + collageID + "\"\>" + collName + "\<\/a\>" + cx.parentNode.innerHTML;
  132.         } else {
  133.             // else just display name
  134.             cx.parentNode.innerHTML = "\<h3 id=\"actcollage\"\>The active collage is: " + collName + cx.parentNode.innerHTML;
  135.         }
  136.  
  137.         var jx = document.getElementsByTagName('h2')[0];
  138.  
  139.         // display "Make This Active Collage!"
  140.         if (currentCollURL != collageID) {
  141.             //standard collage pages
  142.             var collageLink = document.createElement('a');
  143.             collageLink.href = '#';
  144.             collageLink.textContent = 'Make this the active collage';
  145.             collageLink.setAttribute('collage-id', currentCollURL);
  146.             collageLink.setAttribute('collage-name', jx.innerHTML);
  147.             collageLink.className = 'brackets';
  148.             collageLink.id = 'thelink';
  149.             collageLink.addEventListener('click', function(e) { setCollageID(e.target); return false; }, false);
  150.             jx.parentNode.insertBefore(collageLink, jx);
  151.         }
  152.  
  153.         // display "Remove Active Collage"
  154.         if (collageID != 0) {
  155.             var removeActColl = document.createElement('a');
  156.             removeActColl.href = '#';
  157.             removeActColl.textContent = 'Remove the active collage';
  158.             removeActColl.setAttribute('collage-id', '0');
  159.             removeActColl.className = 'brackets';
  160.             removeActColl.id = 'thelink';
  161.             removeActColl.addEventListener('click', function(e) { setCollageID(e.target); return false; }, false);
  162.             jx.parentNode.insertBefore(removeActColl, jx);
  163.         }
  164.  
  165.        
  166.         makeitp = document.getElementById("thelink");
  167.         makeitp.insertAdjacentHTML("beforeBegin",'\<p\>');
  168.         makeitp.insertAdjacentHTML("afterEnd",'&nbsp;\</p\>\<hr\>');
  169.        
  170.         // cache the current contents of the active collage
  171.         if (currentCollURL == collageID) {
  172.             var linkArr = extractTorrentgroupLinks('discog_table');
  173.             torrentCache.clear();
  174.             for (var i = 0; i < linkArr.length; ++i) {
  175.                 torrentCache.add(linkArr[i].getAttribute('torrentgroupid'));
  176.             }
  177.             torrentCache.GM_save();
  178.         }
  179.     } else if (URL.Path == 'collages.php' && URL.Parameters['collageid'] && URL.Parameters['action'] == 'manage') {
  180.         manageStuff();
  181.     }
  182. }
  183.  
  184. function extractTorrentgroupLinks(tableId) {
  185.     var linkArr = new Array();
  186.  
  187.     if (document.getElementById(tableId) != null) {
  188.         var lx = document.getElementById(tableId).getElementsByTagName('a');
  189.         if (typeof lx != 'undefined') {
  190.             var curTorrID = '0';
  191.             for (var i = 0; i < lx.length; ++i) {
  192.                 match = /torrents\.php\?id=(\d+)/.exec(lx[i].href);
  193.                 if (match != null && match[1] != curTorrID) {
  194.                     lx[i].setAttribute('torrentgroupid', match[1]);
  195.                     linkArr.push(lx[i]);
  196.                     curTorrID = match[1];
  197.                 }
  198.             }
  199.         }
  200.     }
  201.  
  202.     return linkArr;
  203. }
  204.  
  205. function createAddIcon(torrent_url, torrentgroupid) {
  206.     var res = document.createElement('img');
  207.     res.setAttribute('torrent-url', torrent_url);
  208.     res.setAttribute('torrentgroupid', torrentgroupid);
  209.     updateAddIcon(res);
  210.     res.addEventListener('click', image_click, false);
  211.     return res;
  212. }
  213.  
  214. function updateAddIcon(img) {
  215.     if (torrentCache.contains(img.getAttribute('torrentgroupid'))) {
  216.         img.src = ICON_OK;
  217.         img.title = 'Remove from collage "' + collName + '"...';
  218.     } else {
  219.         img.src = ICON_ADD;
  220.         img.title = 'Add to collage "' + collName + '"...';
  221.     }
  222. }
  223.  
  224. function manageStuff() {
  225.     var authKey = findAuthKey();
  226.    
  227.     var activeCollageID = URL.Parameters['collageid'];
  228.     var allGroupNumbers=new Array();
  229.     var allNewSortNumbers=new Array();
  230.     var allOrigSortNumbers=new Array();
  231.     var isUpdated = 0;
  232.  
  233.     //get initial group numbers
  234.     var jx = document.getElementsByName('groupid');
  235.     for (var i = 0;i < jx.length; ++i) {
  236.         allGroupNumbers[i]=parseInt(jx[i].value);
  237.     }
  238.    
  239.     //get initial group sort numbers
  240.     var kx = document.getElementsByName('sort');
  241.     for (var i = 0; i < kx.length; ++i) {
  242.         allOrigSortNumbers[i]=parseInt(kx[i].value);
  243.     }
  244.    
  245.     var editAllButton = document.createElement('input');
  246.     editAllButton.type = 'button';
  247.     editAllButton.value = 'Edit All';
  248.    
  249.     var zx = document.getElementById('content').getElementsByTagName('h2')[0];
  250.     zx.parentNode.insertBefore(editAllButton, zx);
  251.    
  252.     //edit all button
  253.     editAllButton.addEventListener("click", function(){
  254.         var mx = document.getElementsByName('sort');
  255.         var i = 0;
  256.         isUpdated = 1;
  257.         editAllButton.value = 'Updating...';
  258.        
  259.         for (i = 0; i < mx.length; ++i) {
  260.             allNewSortNumbers[i]=parseInt(mx[i].value);
  261.            
  262.             //only update new numbers
  263.             if (allNewSortNumbers[i] != allOrigSortNumbers[i]) {
  264.                 GM_xmlhttpRequest({
  265.                     method: "POST",
  266.                     url: URL.Host + "/collages.php",
  267.                     data: "action=manage_handle&auth=" + authKey + "&collageid=" + activeCollageID + "&groupid=" + allGroupNumbers[i] + "&sort=" + allNewSortNumbers[i],
  268.                     headers: { "Content-Type" : "application/x-www-form-urlencoded" },
  269.                     //I can't figure out a way to error check this page...
  270.                     onload: function(e) {
  271.                         if (e.responseText.match(/Invalid authorization key/)) {
  272.                             var temp = document.getElementById('content');
  273.                             temp.innerHTML = "Invalid authorization key (the system must have updated your authkey since you loaded the page).  Refresh the page and try again!<br>I strongly recommend rechecking your values, since it's possible (though unlikely) that some values updated while others didn't.  Sorry :(";
  274.                         }
  275.                     },
  276.                     onerror: function(e) {
  277.                         alert("some sort of error occured with the server.  Error occured with torrentID: \"" + allGroupNumbers[i] + "\" which had a new sort number of: \"" + allNewSortNumbers[i] + "\".  Recommend rechecking your values.");
  278.                         return;
  279.                     }
  280.                 });
  281.             }
  282.         }
  283.         // one more worthless request to reload the page after all previous requests finish
  284.         GM_xmlhttpRequest({
  285.             method: "POST",
  286.             url: URL.Host + "/collages.php",
  287.             data: "action=manage_handle&auth=" + authKey + "&collageid=" + activeCollageID + "&groupid=" + allGroupNumbers[i] + "&sort=" + allNewSortNumbers[i],
  288.             headers: { "Content-Type" : "application/x-www-form-urlencoded" },
  289.             onload: function(e) {
  290.                 if (e.responseText.match(/Invalid authorization key/)) {
  291.                     var temp = document.getElementById('content');
  292.                     temp.innerHTML = "Invalid authorization key (the system must have updated your authkey since you loaded the page).  Refresh the page and try again!<br>I strongly recommend rechecking your values, since it's possible (though unlikely) that some values updated while others didn't.  Sorry :(";
  293.                 } else {
  294.                     document.location.href = document.location.href;
  295.                 }
  296.             }
  297.         });
  298.     }, false);
  299.    
  300.     //insert blank space between edit all image and math range image
  301.     var oNewP = document.createElement("b");
  302.     var oText = document.createTextNode(" ");
  303.     oNewP.appendChild(oText);
  304.     var beforeMe = document.getElementsByTagName('h2')[0];
  305.     beforeMe.parentNode.insertBefore(oNewP, beforeMe);
  306.    
  307.     if (GM_getValue("checkBoxAlwaysOn", "0") == 0) {
  308.         var mathRangeButton = document.createElement('input');
  309.         mathRangeButton.type = 'button';
  310.         mathRangeButton.value = 'Math Range';
  311.        
  312.         mathRangeButton.addEventListener("click", function() {
  313.             mathArray();
  314.             mathRangeButton.style.display = 'none';
  315.         }, false);
  316.        
  317.         var bx = document.getElementById('content').getElementsByTagName('h2')[0];
  318.         bx.parentNode.insertBefore(mathRangeButton, bx);
  319.     } else {
  320.         mathArray();
  321.     }
  322.    
  323.     //catch 'Remove' clicks for torrentcache update
  324.     var inputs = document.querySelectorAll('input[name=submit][value=Remove]');
  325.     var groupidnode;
  326.     for(var ii = 0; ii < inputs.length; ++ii) {
  327.         groupidnode = inputs[ii].parentNode.querySelector('input[type=hidden][name=groupid]');
  328.         inputs[ii].setAttribute('torrentgroupid', groupidnode.value);
  329.         inputs[ii].addEventListener('click', function(e) { manage_remove_click(e); });
  330.     }
  331. }
  332.  
  333. // used for operations on manage page
  334. function mathArray() {
  335.     // math Array checkboxes
  336.     var box=new Array();
  337.     var lx = document.getElementsByName('sort');
  338.    
  339.     for (var i =0; i< lx.length; ++i) {
  340.         box[i] = document.createElement("input");
  341.         box[i].type="checkbox";
  342.         box[i].setAttribute('realChecked', "0");
  343.         box[i].setAttribute('arrayValue', i);
  344.         lx[i].parentNode.insertBefore(box[i], lx[i]);
  345.        
  346.         //check for max of two boxes
  347.         box[i].addEventListener("click", function() {
  348.             var unclickCheck = 0;
  349.             if (this.checked == true) {
  350.                 this.setAttribute('realChecked', "1");
  351.             } else  {
  352.                 // if you unselect the box do the following:
  353.                 this.setAttribute('realChecked', "0");
  354.                 for (var m =0; m< lx.length; ++m) {
  355.                     if (box[m].getAttribute('realChecked') != "1") {
  356.                         box[m].checked = false;
  357.                         box[m].disabled = false;
  358.                     }
  359.                 }
  360.             }
  361.             var counter = 0;
  362.                        
  363.             var firstValue, secondValue, thirdValue;
  364.             for (var z=0; z< lx.length; ++z) {
  365.                 if (box[z].getAttribute('realChecked') == "1") {
  366.                     counter++;
  367.                     if (counter == 1) {
  368.                         firstValue = parseInt(z);
  369.                     } else if (counter == 2) {
  370.                         secondValue = parseInt(z);
  371.                     } else if (counter == 3) {
  372.                         thirdValue = parseInt(z);
  373.                     }
  374.                 }
  375.             }
  376.             if (counter == 2) {
  377.                 //visibly show which boxes you checked
  378.                 for (var x = firstValue+1; x <= secondValue-1; ++x) {
  379.                     box[x].checked = true;
  380.                     box[x].disabled = true;
  381.                 }
  382.             } else if (counter == 3) {
  383.                 alert("You can only select TWO check boxes.  One for the top of your range, one for the bottom of your range.  Unselecting the checkbox.");
  384.                 // if you select below the current range
  385.                 if  (this.getAttribute('arrayValue') > secondValue) {
  386.                     for (var x = firstValue+1; x <= secondValue+1; ++x) {
  387.                         box[x].checked = false;
  388.                         box[x].disabled = false;
  389.                         box[x].setAttribute('realChecked', "0");
  390.                     }
  391.                     //uncheck current selection
  392.                     box[thirdValue].checked = false;
  393.                     box[thirdValue].setAttribute('realChecked', "0");
  394.  
  395.                 }
  396.                 // if you select above the current range
  397.                 else if (this.getAttribute('arrayValue') < secondValue) {
  398.                     for (var y = secondValue; y <= thirdValue-1; ++y) {
  399.                        
  400.                         box[y].checked = false;
  401.                         box[y].disabled = false;
  402.                         box[y].setAttribute('realChecked', "0");
  403.                     }
  404.                     //uncheck current selection
  405.                     box[firstValue].checked = false;
  406.                     box[firstValue].setAttribute('realChecked', "0");
  407.                 }
  408.             }
  409.         }, false);
  410.     }
  411.    
  412.     //execute the math range button
  413.     var operationButton = document.createElement('input');
  414.     operationButton.type = 'button';
  415.     operationButton.value = 'Operation';
  416.    
  417.     var yx = document.getElementById('content').getElementsByTagName('h2')[0];
  418.     yx.parentNode.insertBefore(operationButton, yx);
  419.    
  420.     operationButton.addEventListener("click", function(e){
  421.         var upperRangeValue, lowerRangeValue;
  422.        
  423.         var counter = 0;
  424.         // check if they have two check boxes
  425.         for (var q =0; q< lx.length; ++q) {
  426.             if (box[q].getAttribute('realChecked') == "1"){
  427.                 counter++;
  428.                 if (counter == 1) { upperRangeValue = parseInt(q); }
  429.                 else if (counter == 2) { lowerRangeValue = parseInt(q); }  
  430.             }
  431.         }
  432.         if (counter == 1) {
  433.             alert("You must select two checkboxes to include your entire range.\nExample- select the 3rd checkbox and 6th checkbox to perform a math operation on torrents 3, 4, 5, 6\nAnother option is to select NO checkboxes.  When you perform an operation, it will perform on ALL torrents.\nRemember: click edit all to update the changes.");
  434.         } else {
  435.             // We are ready to do the math operations
  436.             var userOperationMessage = "What operation do you want to perform:\nadd: +\nsubtract: -\nmultiply: *\ndivide: \/\nfind missing torrents: m\nexport to tab delimitted file: e\n\nRemember: click edit all to update the changes.";
  437.             if (counter == 0) {            
  438.                 // check all the boxes so you can see you're doing stuff on everything
  439.                 for (var c = 0; c < lx.length; ++c) {
  440.                     box[c].checked = true;
  441.                     box[c].disabled = true;
  442.                 }
  443.                
  444.                 upperRangeValue = 0;
  445.                 lowerRangeValue = parseInt(c);
  446.  
  447.                 var userOperation = prompt("Now performing an operation on ALL elements.\n\n" + userOperationMessage);
  448.             } else {
  449.                 var userOperation = prompt(userOperationMessage);
  450.             }
  451.            
  452.             if (userOperation != null && userOperation != '') {
  453.                 userOperation = userOperation.toUpperCase();
  454.                 if (userOperation == "+" || userOperation == "-" || userOperation == "*" || userOperation == "\/") {
  455.                     var adjustValue = prompt("Current operation: \"" + userOperation + "\".  Input an integer to adjust the selected range by the operation.");
  456.                     if (adjustValue != null && adjustValue != '') {
  457.                         adjustValue = parseInt(adjustValue);
  458.                        
  459.                         if (isNaN(adjustValue)) {
  460.                             alert("Please pick a valid integer!");
  461.                             return;
  462.                         }
  463.                     } else {
  464.                         return;
  465.                     }
  466.                 } else if (userOperation == "M") {
  467.                     var diffValue = prompt("Current operation: \"Find Missing Torrents\".  Input the difference of sort numbers.\nExample: Input 10 if the collage sort numbers are 10, 20, 30, ...");
  468.                     if (diffValue != null && diffValue != '') {
  469.                         diffValue = parseInt(diffValue);
  470.                         if (isNaN(diffValue)) {
  471.                             alert("Please pick a valid integer!");
  472.                             return;
  473.                         }
  474.                     } else {
  475.                         return;
  476.                     }
  477.                 } else if (userOperation == "E") {
  478.                 } else {
  479.                     // uncheck all the boxes assuming the user never selecting any
  480.                     if (counter == 0) {                
  481.                         for (var c =0; c< lx.length; ++c) {
  482.                             if (c >= upperRangeValue && c <= lowerRangeValue) {
  483.                                 box[c].checked = false;
  484.                                 box[c].disabled = false;
  485.                             }  
  486.                         }
  487.                     }
  488.                     alert("Please select only the following characters \"+\", \"-\", \"*\", \"\/\", \"m\" (no quotes)");
  489.                     return;
  490.                 }
  491.             } else {
  492.                 // user hits cancel
  493.  
  494.                 // uncheck all the boxes assuming the user never selecting any
  495.                 if (counter == 0) {
  496.                     for (var c = 0; c < lx.length; ++c) {
  497.                         if (c >= upperRangeValue && c <= lowerRangeValue) {
  498.                             box[c].checked = false;
  499.                             box[c].disabled = false;
  500.                         }  
  501.                     }
  502.                 }
  503.                 return;
  504.             }
  505.                        
  506.             var allNewSortNumbers = new Array();
  507.  
  508.             if (userOperation == "+") {
  509.                 for (var b = 0; b < lx.length; ++b) {      
  510.                     if (b >= upperRangeValue && b <= lowerRangeValue) {
  511.                         allNewSortNumbers[b] = parseInt(lx[b].value) + adjustValue;
  512.                         lx[b].value=allNewSortNumbers[b];
  513.                     }              
  514.                 }
  515.             } else if (userOperation == "-") {
  516.                 for (var b = 0; b < lx.length; ++b) {      
  517.                     if (b >= upperRangeValue && b <= lowerRangeValue) {
  518.                         allNewSortNumbers[b] = parseInt(lx[b].value) - adjustValue;
  519.                         lx[b].value=allNewSortNumbers[b];
  520.                     }              
  521.                 }
  522.             } else if (userOperation == "*") {
  523.                 for (var b = 0; b < lx.length; ++b) {      
  524.                     if (b >= upperRangeValue && b <= lowerRangeValue) {
  525.                         allNewSortNumbers[b] = parseInt(lx[b].value) * adjustValue;
  526.                         lx[b].value=allNewSortNumbers[b];
  527.                     }              
  528.                 }          
  529.             } else if (userOperation == "\/") {
  530.                 for (var b = 0; b < lx.length; ++b) {      
  531.                     if (b >= upperRangeValue && b <= lowerRangeValue) {
  532.                         allNewSortNumbers[b] = parseInt(lx[b].value) / adjustValue;
  533.                         lx[b].value=allNewSortNumbers[b];
  534.                     }              
  535.                 }
  536.             } else if (userOperation == "M") {
  537.                 //find missing numbers
  538.                 var missingValues=new Array();
  539.                 var extraValues=new Array();
  540.                 var missingCount = 0;
  541.                 var extraCount = 0;
  542.                 var zzb = document.getElementsByName('sort');
  543.                 var checkNumber = parseInt(zzb[upperRangeValue].value);
  544.                 var nextNumber =0;
  545.                 for (var i = upperRangeValue; i < lowerRangeValue-1; ++i) {
  546.                     checkNumber += diffValue;
  547.                    
  548.                     nextNumber = parseInt(zzb[i + 1].value);
  549.  
  550.                     if (checkNumber != nextNumber) {
  551.                         if (checkNumber < nextNumber) {
  552.                             i--;
  553.                             if (checkNumber != missingValues[missingCount-1]) {
  554.                                 missingValues[missingCount] = checkNumber;
  555.                                 missingCount++;
  556.                             }
  557.                         } else {
  558.                             extraValues[extraCount] = nextNumber;
  559.                             extraCount++;
  560.                             if (checkNumber != missingValues[missingCount-1]) {
  561.                                 missingValues[missingCount] = checkNumber;
  562.                                 missingCount++;
  563.                                 checkNumber -= diffValue;
  564.                             }
  565.                         }
  566.                     } else {
  567.                         if (checkNumber == missingValues[missingCount-1]) {
  568.                                 missingCount--;
  569.                                 missingValues.pop();
  570.                         }
  571.                     }
  572.                 }
  573.                 if (missingCount == 0) {
  574.                     if (extraCount == 0) {
  575.                         alert("No missing sort numbers.\nAlso, no extra sort numbers tucked inbetween.");
  576.                     } else {
  577.                         alert("No missing sort numbers.\nBut the extra sort numbers are: " + extraValues);
  578.                     }  
  579.                 } else {
  580.                     if (extraCount == 0) {
  581.                         alert("The missing sort numbers are: " + missingValues + "\nAlso, no extra sort numbers tucked inbetween.");
  582.                     } else {
  583.                         alert("The missing sort numbers are: " + missingValues + "\nAnd the extra sort numbers are: " + extraValues);
  584.                     }
  585.                 }
  586.             } else if (userOperation == "E") {
  587.                 var items = document.getElementById("content").getElementsByTagName("a");
  588.                 var names = new Array();
  589.                 var artist, album;
  590.                 var artistMatch, artistMatchSecond, albumMatch;
  591.                 var combined;
  592.  
  593.                 for (var i = 0; i < items.length; ++i) {
  594.                     albumMatch = items[i].href.match(/torrents\.php\?id=([0-9]+)$/);
  595.                     if (albumMatch) {
  596.                         album = items[i].innerHTML;
  597.                         artistMatch = items[i-1].href.match(/artist\.php\?id=([0-9]+)$/);
  598.                        
  599.                         if (artistMatch) {
  600.                             artist = items[i-1].innerHTML;
  601.                             //check for 2 artists (eg. "Billy Bragg" and "Wilco" - "Mermaid Avenue")
  602.                             artistMatchSecond = items[i-2].href.match(/artist\.php\?id=([0-9]+)$/);
  603.                             if (artistMatchSecond) { artist = items[i-2].innerHTML + " and " + artist; }
  604.                             // sort by artist - album
  605.                             if (userOperation == "E" ) { combined = "\"" + artist + "\",\"" + album+  "\""; }
  606.                             // sort by album - artist
  607.                             else {combined = "\"" + album + "\",\"" + artist + "\""; }
  608.                         } else {
  609.                             // case for no artist found (ie pdf, audiobook, Various Artists etc)
  610.                             combined = ",\"" + album + "\"";
  611.                            
  612.                             // super ugly way to handle Various Artists for sort by artist (if anyone has a better idea, I'm all ears :)
  613.                             var itemsx = document.getElementById("content").getElementsByTagName("tr");
  614.                             outerLoop: for (var z = 0; z < itemsx.length; ++z) {
  615.                                 var itemsInnerHTML = itemsx[z].innerHTML
  616.                                 var match = itemsInnerHTML.match(/torrents\.php/);
  617.                                 if (match) {
  618.                                     if (itemsx[z].innerHTML.match(/Various Artists/)) {
  619.                                         var tempAlbumCheck = itemsInnerHTML.substring(itemsInnerHTML.indexOf("Torrent\">")+9, itemsInnerHTML.indexOf("</a>"));
  620.                                         if (album == tempAlbumCheck) {
  621.                                             //put at end of sort list
  622.                                             combined = "\"Various Artits\",\"" + album + "\"";
  623.                                             break outerLoop;
  624.                                         }
  625.                                     }
  626.                                 }
  627.                             }
  628.                         }
  629.                         names.push(combined);
  630.                     }
  631.                 }
  632.                 var message = '';
  633.                 for (var x = 0; x < names.length; ++x) {
  634.                     message = message + names[x] + "<br>";
  635.                 }
  636.                 document.body.innerHTML = message;
  637.                
  638.             }
  639.  
  640.             // uncheck the boxes in the range (note: the above/below loops should be separate for speed issues
  641.             for (var c =0; c< lx.length; ++c) {
  642.                 if (c >= upperRangeValue && c <= lowerRangeValue) {
  643.                     box[c].checked = false;
  644.                     box[c].disabled = false;
  645.                 }  
  646.             }
  647.             box[upperRangeValue].setAttribute('realChecked', "0");
  648.             box[lowerRangeValue].setAttribute('realChecked', "0");
  649.         }
  650.     }, false);
  651. }
  652.  
  653.  
  654. // get the collage ID from stored file
  655. function getCollageID() {
  656.     collageID = GM_getValue('activeCollageID', '0');
  657.     collName = GM_getValue('activeCollageName', 'None');
  658. }
  659.  
  660. // write the collage ID to the stored file
  661. function setCollageID(linkref) {
  662.     if (linkref.getAttribute('collage-id') == '0') {
  663.         GM_deleteValue('activeCollageID');
  664.         GM_deleteValue('activeCollageName');
  665.         torrentCache.GM_delete();
  666.     } else {
  667.         GM_setValue('activeCollageID', linkref.getAttribute('collage-id'));
  668.         GM_setValue('activeCollageName', linkref.getAttribute('collage-name'));
  669.     }
  670.     window.location.reload();
  671. }
  672.  
  673. function image_click(e) {
  674.     var authKey = findAuthKey();
  675.  
  676.     //error checking on collageID in case user updates collage and doesn't refresh torrents.php page
  677.     if (GM_getValue('activeCollageID') != collageID) {
  678.         alert('Active collage has changed. Refresh the page and try again!');
  679.         return;
  680.     }
  681.  
  682.     var img = this; //necessary to be able to change the image's src attribute from within the onload/onerror closures
  683.  
  684.     var inCollage = torrentCache.contains(img.getAttribute('torrentgroupid'));
  685.  
  686.     img.src = ICON_LOADING;
  687.  
  688.     GM_xmlhttpRequest({
  689.         method: "POST",
  690.         url: URL.Host + "/collages.php",
  691.         data: (inCollage ?
  692.             'action=manage_handle&auth=' + authKey + '&collageid=' + collageID + '&groupid=' + img.getAttribute('torrentgroupid') + '&submit=Remove' :
  693.             'action=add_torrent&auth=' + authKey + '&collageid=' + collageID + '&url=' + img.getAttribute('torrent-url')),
  694.         headers: { "Content-Type" : "application/x-www-form-urlencoded" },
  695.         onload: function(response) {
  696.             // we get redirected if the action was successful, so if we are still on collages.php$, that means that something went wrong.
  697.             if (response.finalUrl.match(/\/collages\.php$/)) {
  698.                 // generically read out the error message. yes, this code isn't pretty :-/
  699.                 var title = response.responseText.substring(response.responseText.search('<h2>')+4);
  700.                 title = title.substring(0, title.search('</h2>'));
  701.                 var description = response.responseText.substring(response.responseText.search('<h2>Error</h2>'));
  702.                 description = description.substring(description.search('<p>')+3);
  703.                 description = description.substring(0, description.search('</p>'));
  704.                
  705.                 img.src = ICON_ERR;
  706.                 alert(title+"\n\n"+description);
  707.             } else {
  708.                 // Remember: inCollage is determined *before* sending the request. When inCollage is true, the torrent now isn't in the collage anymore!
  709.                 if (inCollage) {
  710.                     torrentCache.remove(img.getAttribute('torrentgroupid'));
  711.                 } else {
  712.                     torrentCache.add(img.getAttribute('torrentgroupid'));
  713.                 }
  714.                 torrentCache.GM_save();
  715.                 updateAddIcon(img);
  716.             }
  717.         },
  718.         onerror: function(f) {
  719.             img.src = ICON_ERR;
  720.             alert('Some unknown error occured. Refresh the page and try again!');
  721.         }
  722.     });
  723. }
  724.  
  725. function manage_remove_click(e) {
  726.     torrentCache.remove(e.target.getAttribute('torrentgroupid'));
  727.     torrentCache.GM_save();
  728.     return true;
  729. }
  730.  
  731. //find authkey
  732. function findAuthKey() {
  733.     return unsafeWindow.authkey;
  734. }
  735.  
  736. // preference for always showing checkboxes on manage page
  737. GM_registerMenuCommand("PTH.ME :: Collage Building Helper - MathRange checkboxes", function() {
  738.     var checkBoxQuestion;
  739.     for (var zz = 1; zz <= 3 && isNaN(checkBoxQuestion); ++zz) {
  740.         checkBoxQuestion = prompt("Do you want the checkboxes to always appear on the manage collage page?\nType \"1\" for yes and \"0\" for no.\nCurrent selection is: " + GM_getValue("checkBoxAlwaysOn", "0") + " (0 is default)");
  741.         if (checkBoxQuestion != null && checkBoxQuestion != '') {
  742.             checkBoxQuestion = parseInt(checkBoxQuestion);
  743.             if (isNaN(checkBoxQuestion)) {
  744.                 if (zz==3) {
  745.                     alert("Try harder!  Better luck next time!");
  746.                 } else {
  747.                     alert("Please pick \"1\" or \"0\" (no quotes)");
  748.                 }
  749.             } else if (checkBoxQuestion == 0 || checkBoxQuestion == 1) {
  750.                 GM_setValue("checkBoxAlwaysOn", checkBoxQuestion);
  751.             } else {
  752.                 alert("Please pick \"1\" or \"0\" (no quotes)");
  753.             }
  754.         }
  755.     }
  756. });
  757.  
  758. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement