HJ-OTMOP / Display Active Encodes

// ==UserScript==
// @name         Display Active Encodes
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  When browsing the official HANDJOB thread, the 'Encode History' page is scraped for each user's active encodes.
// @author       HJ-OTMOP
// @license MIT
// @copyright 2018, HJ-OTMOP (https://openuserjs.org/users/HJ-OTMOP)
// @match        https://passthepopcorn.me/*threadid=13617*
// @match        https://passthepopcorn.me/*threadid=28365*
// @match        https://passthepopcorn.me/*threadid=13644*
// @require      http://code.jquery.com/jquery-3.3.1.min.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

// Changelog 0.3 – Added compatibility for when there are more than 50 active encodes on the Encode History page
// Changelog 0.2 – Expanded script to work on HANDJOB Scholarship Fund & Request a HANDJOB threads; Fixed "SD x264" to show as "DVDRip / 480p" instead of only "DVDRip"
// Changelog 0.1 – First version

const encodeHistory = 'https://passthepopcorn.me/encodeoffers.php?search=&format%5B%5D=SdX264&format%5B%5D=Sd576p&format%5B%5D=Hd720p&format%5B%5D=Hd1080p&state%5B%5D=Offered&order_by=time&order_way=desc';
var results = 0;
var pages = 0;
var active = [];

(function() {
    'use strict';
    injectStyle();
    loadHistory(encodeHistory).then(() => loadNext()).catch(e => console.log(e));
})();

function loadNext() {
    if (pages <= 1) processActive();
    else {
        var promises = [];
        for (let i = 2; i < pages; i++) {
            promises.push(loadHistory(encodeHistory + "&page=" + i));
        }
        Promise.all(promises).then(() => processActive()).catch(e => console.log(e));
    }
}

function loadHistory(url) {
    return new Promise((resolve,reject) => { 
    GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: function(response) {
           var raw = $( '<div></div>' );
           raw.html(response.responseText);
            $('.table.table--panel-like tbody tr', raw).each(function() {
                var data = {};
                data.link = $(this).find('td a.l_movie').prop('href');
                data.title = $(this).find('td a.l_movie').text();
                data.res = $(this).find('td:first-child').text().split(" - ").pop();
                var regex = /\d{3,4}p/gi;
                try {data.res = data.res.match(regex)[0];}
                catch (e) {data.res = "DVDRip / 480p";}
                data.userid = $(this).find('td a.username').prop('href').split("id=")[1];
                data.username = $(this).find('td a.username').text();
                data.time = $(this).find('td span.time').attr('title');
                data.timerel = $(this).find('td span.time').text();
                regex = / \[\d{4}\] /gi;
                data.year = $(this).find('td:first-child').text().match(regex);
                data.year = data.year[0].replace(/\W/g, '');
                active.push(data);
            });
            if (!results) {
            results = parseInt($('.search-form__footer__results', raw).text().replace(/\D/gi, ""));
            pages = (50 * Math.ceil(results/50)) / 50;}
            resolve();
        },
        onerror: function (err) {
            console.log(err);
            reject(err);
        }
    });
  });
}

function processActive() {
    active.sort(function(a, b) {
        var aTime = new Date(a.time).getTime();
        var bTime = new Date(b.time).getTime();
        return bTime - aTime;
    });
    $('.forum_post').each(function() {
        addFooter($(this));
        var userid = $(this).find("a.username").attr('href');
        userid = userid.split("id=")[1];
        if (!userid) userid = $(this).find('a.username').attr('data-userid');
        var encodes = [];
        for (let i = 0; i < active.length; i++) {
            if (userid == active[i].userid) {
                encodes.push(`<a href="${active[i].link}">${active[i].title}</a> [${active[i].year}] – ${active[i].res} – <span title="${active[i].time}">started ${active[i].timerel}</span>`);
            }
        }
        var encString = encodes.join("<br>");
        $(this).find('.forum-post__footer-body').html(encString);
        if (encString) {$(this).find('.forum-post__footer').slideDown("fast");}
    });
}

function addFooter(el) {
    $(el).append(`<div class='forum-post__footer'>
<div class="forum-post__footer-container">
<div class='forum-post__footer-header'>
Encoding:</div>
<div class='forum-post__footer-body'></div>
</div>
</div>`);
}

function injectStyle() {
    $("head").append(`
<style>.forum-post__footer {
display: none;
width: 100%;
border-top: thin solid #555;
}

.forum-post__footer-container {
display: flex;
}

.forum-post__footer-header {
text-align: left;
font-size: 0.9em;
font-weight: bold;
padding: 5px;
}

.forum-post__footer-body {
flex: 2 0 auto;
padding: 5px;
font-size: 0.9em;
}
</style>`);   
}