Empornium Deluxe Mode

Enhances the empornium.me porn torrent website

Fra 09.07.2019. Se den seneste versjonen.

// ==UserScript==
// @name         Empornium Deluxe Mode
// @namespace    http://tampermonkey.net/
// @version      1.13
// @description  Enhances the empornium.me porn torrent website
// @author       codingjoe
// @match        https://*.empornium.me/*
// @include      /empornium\.me/
// @require      https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @grant        GM_registerMenuCommand
// @run-at       document-idle
// ==/UserScript==

// returns a list of elements contained in both lists (i.e. set intersection)
Array.prototype.intersect = function (lst) {
    return this.filter(r => lst.indexOf(r) >= 0);
};

// determines whether or not there exists an intersection between the two lists
// tests for one or more elements in common
Array.prototype.intersects = function (lst) {
    return this.intersect(lst).length > 0;
};

// returns a list of elements unique to both lists
Array.prototype.union = function(lst) {
    let u = [];
    this.concat(lst).forEach(r => { if (!u.includes(r)) { u.push(r); } });
    return u;
};

Element.prototype.props = function(json) {
    return Object.assign(this, json);
};

HTMLDocument.prototype.new = function(tagName) {
    return document.createElement(tagName);
};

Element.prototype.appendTo = function(element) {
    element.appendChild(this);
    return this;
};

function RenderAccordion(layout, wrapper) {
    let div = document.createElement("div");

    // display the fields under the designated accordion label
    Object.keys(layout).forEach(header => {
        // create the category header on the accordion
        let category = document.new("button").props({ className: "accordion", innerText: header }).appendTo(div);

        // wire-up accordion node to reveal its contents
        category.addEventListener("click", function() {
            this.classList.toggle("active");
            let panel = this.nextElementSibling;
            if (panel.style.display === "block") {
                panel.style.display = "none";
            } else {
                panel.style.display = "block";
            }
        });

        // create the content panel of this accordion
        let settingsList = document.new("div").props({ className: "panel" }).appendTo(div);

        // render the settings within the content panel
        layout[header].forEach(setting => {
            settingsList.appendChild(wrapper.querySelector("[id$=" + setting + "_var]"));
        });
    });

    return div;
}

function OpenModal(GM_config) {
    let modal = document.body.appendChild(document.createElement("div"));
    modal.id = "modal";
    Object.assign(modal.style, {
        display: "block",
        position: "fixed",
        left: 0,
        top: 0,
        width: "100%",
        height: "100%",
        overflow: "auto",
        backgroundColor: "rgba(0,0,0,0.7)"
    });

    modal.addEventListener("click", function (e) {
        // when modal clicked
        if (e.target === this) {
            // close the config dialog
            GM_config.close();
        }
    });
    document.body.style.overflow = "hidden";
}

GM_config.init({
  'id': 'EmporniumEnhancementsConfig',
  'title': 'Empornium Deluxe Mode Configuration',
  'fields': {
    'AlwaysHeader' : {
        'label': 'Always keep header in view',
        'type': 'checkbox',
        'default': false
    },
    'AutoDismiss': {
        'label': 'Auto-dismiss login timeout notification',
        'type': 'checkbox',
        'default': false
    },
    'AutoOpenFileList': {
        'label': 'Auto-open filelist',
        'type': 'checkbox',
        'default': false
    },
    'AutoOpenSpoilers': {
        'label': 'Auto-open hidden text / spoilers',
        'type': 'checkbox',
        'default': false
    },
    'AutoThankUploader': {
        'label': 'Auto-thank the uploader upon clicking download / freeleech / doubleseed',
        'type': 'checkbox',
        'default': false
    },
    'ShowImages': {
        'label': 'Display hover images inline',
        'type': 'checkbox',
        'default': false
    },
    'ShowRatioGoals': {
        'label': 'Display ratio goals table on the Bonus Shop page (experimental)',
        'type': 'checkbox',
        'default': false
    },
    'JumpToTop': {
        'label': "Insert 'Jump to Top' link onto bottom-right corner of all pages",
        'type': 'checkbox',
        'default': false
    },
    'StripAnonym': {
        'label': 'Strip url anonymizers from links',
        'type': 'checkbox',
        'default': false
    },
    'CopyClearBottomNotifs': {
        'label': 'Move "clear" and "clear selected" links onto bottom of matched groups',
        'type': 'checkbox',
        'default': false
    },
    'HideClearAll': {
        'label': 'Hide "Clear" and "clear all" links to prevent accidental clearing',
        'type': 'checkbox',
        'default': false
    },
    'AutoCheckNotif': {
        'label': 'When the link is clicked, check the torrent\'s checkbox for manual clearing',
        'type': 'checkbox',
        'default': false
    },
    'PreventNewWindow': {
        'label': 'Prevent single-click links from opening a new window',
        'type': 'checkbox',
        'default': false
    },
    'HideSeeded': {
        'label': 'Hide currently seeding torrents',
        'type': 'checkbox',
        'default': false
    },
    'HideLeeching': {
        'label': 'Hide currently leeching torrents',
        'type': 'checkbox',
        'default': false
    },
    'HideGrabbed': {
        'label': 'Hide previously grabbed torrents [incomplete download]',
        'type': 'checkbox',
        'default': false
    },
    'HideSnatched': {
        'label': 'Hide previously snatched torrents [finished]',
        'type': 'checkbox',
        'default': false
    },
    'FilterFilesize': {
        'label': 'Only display torrents in the filesize range:',
        'type': 'checkbox',
        'default': false
    },
    'FilterTags': {
        'label': 'Hide torrents that contain any of the following tags (black list):',
        'type': 'checkbox',
        'default': false
    },
    'TagListing': {
        'label': '(comma-separated list)',
        'type': 'textarea',
        'title': 'black list',
        'default': 'bbc, big.black.cock, bbw, fat, obese, hairy, gay, trans, tranny, transsexual, scat, feces, poop, puke, vomit, censored'
    },
    'OnlyShowTheseTags': {
        'label': 'Hide torrents that do <u>not</u> contain any of the following tags (must-have list):',
        'type': 'checkbox',
        'default': false
    },
    'OnlyShowTagListing': {
        'label': '(comma-separated list)',
        'type': 'textarea',
        'title': 'must-have list',
        'default': 'swallowed.com, evilangel.com, amourangels.com, met-art.com, legalporno.com, mplstudios.com, femjoy.com, sexart.com, ftvgirls.com, teenpornstorage.com, domai.com, analteenangels.com, showybeauty.com, justteensite.com, facefucking.com, teenfidelity.com, chaturbate.com, myfreecams.com, twistys.com, atkgalleria.com, iameighteen.com'
    }
  },
  'css': '#EmporniumEnhancementsConfig { background-color:#000525 !important; width: auto!important; height: auto!important; }\
         #EmporniumEnhancementsConfig_wrapper { background-color:#000525 !important; color:#2C8466 !important; width: 900px!important; margin-left: auto!important; margin-right: auto!important; }\
         #EmporniumEnhancementsConfig_field_TagListing, #EmporniumEnhancementsConfig_field_OnlyShowTagListing { width: 870px; height: 65px; }\
         #EmporniumEnhancementsConfig_FilterFilesize_var, #EmporniumEnhancementsConfig_TagListing_var { border-bottom: 3px solid #2C8466 !important; }\
         #EmporniumEnhancementsConfig_resetLink { color: #2C8466 !important; text-decoration: none !important; }\
         select, button, textarea, input:not([type=checkbox]) { background-color: #0E2D4A; color: silver; }\
         button {border-radius:5px}\
         #lowerNumber, #higherNumber { width: 50px }\
         .accordion {\
              /*background-color: #eee;\
              color: #444;*/\
              cursor: pointer;\
              padding: 18px;\
              width: 100%;\
              border: none;\
              text-align: center;\
              outline: none;\
              font-size: 15px;\
              transition: 0.4s;\
          }\
          \
          .active, .accordion:hover {\
              background-color: #133C5F; \
          }\
          \
          .panel {\
              padding: 0 18px;\
              display: none;\
              overflow: hidden;\
          }',
  'events': {
    'init': function() {
      // Set the value of the dummy field to the saved value
      //GM_config.set('customCSS', GM_config.get('validCSS'));
    },
    'open': function(doc) {
        // Use a listener to update the hidden field when the dummy field passes validation
        /*GM_config.fields['customCSS'].node.addEventListener('change', function () {
          // get the current value of the visible field
          var customCSS = GM_config.get('customCSS', true);

          // Only save valid CSS
          if (/\w+\s*\{\s*\w+\s*:\s*\w+[\s|\S]*\}/.test(customCSS)) {
            GM_config.set('validCSS', customCSS);
          }
        }, false);*/

        /* logic to create the filesize range fields */
        let ref = GM_config.fields['FilterFilesize'].node.parentNode;
        
        let div = document.new("div").props({ id: "grpFilesize" }).appendTo(ref);
        let lowerNumber = document.new("input").props({ id: "lowerNumber", type: "text", title: "Lower range. Digits only." }).appendTo(div);
        let lowerUnits = document.new("select").props({ id: "lowerUnits" }).appendTo(div);
        let txt = document.new("label").props({ innerText: " to " }).appendTo(div);
        let higherNumber = document.new("input").props({ id: "higherNumber", type: "text", title: "Higher range. Digits only." }).appendTo(div);
        let higherUnits = document.new("select").props({ id: "higherUnits" }).appendTo(div);

        // add select options
        let units = [ "KiB", "MiB", "GiB", "TiB" ];
        units.forEach(u => {
            document.new("option").props({ text: u, value: u }).appendTo(lowerUnits);
            document.new("option").props({ text: u, value: u }).appendTo(higherUnits);
        });

        // set defaults
        lowerNumber.setAttribute("maxlength", 3);
        lowerNumber.value = "0";

        higherNumber.setAttribute("maxlength", 3);
        higherNumber.value = "999";

        higherUnits.value = "TiB";
        /* end of filesize range creation logic */

        // attempt to parse stored range values
        try {
            let filesizeRange = JSON.parse(GM_getValue('fileSizeRange'));
            lowerNumber.value = filesizeRange.lowerNumber;
            lowerUnits.value = filesizeRange.lowerUnits;
            higherNumber.value = filesizeRange.higherNumber;
            higherUnits.value = filesizeRange.higherUnits;
        } catch {
            console.log("Check out the filesize range feature.");
        }

        OpenModal(this);
        
        /* create the accordion UI */
        let wrapper = ref.parentNode;
        
        // define accordion layout
        div = RenderAccordion({
            "General": [
                "AlwaysHeader",
                "AutoDismiss",
                "JumpToTop",
                "PreventNewWindow",
                "StripAnonym"
            ],
            "Notifications": [
                "CopyClearBottomNotifs",
                "HideClearAll",
                "AutoCheckNotif"
            ],
            "Torrent Page": [
                "AutoOpenFileList",
                "AutoOpenSpoilers",
                "AutoThankUploader"
            ],
            "Torrent Listings": [
                "ShowImages",
                "HideSeeded",
                "HideLeeching",
                "HideGrabbed",
                "HideSnatched"
            ],
            "Filters": [
                "FilterFilesize",
                "FilterTags",
                "TagListing",
                "OnlyShowTheseTags",
                "OnlyShowTagListing"
            ],
            "Bonus": [
                "ShowRatioGoals"
            ]
        }, wrapper);
        
        wrapper.insertBefore(div, wrapper.firstChild.nextSibling);
        
        
        // wire-up the filter filesize checkbox to trigger the disabled state of its corresponding fields
        let chkFilterFilesize = wrapper.querySelector("#EmporniumEnhancementsConfig_field_FilterFilesize");
        if (chkFilterFilesize.checked == false) {
            chkFilterFilesize.nextSibling.querySelectorAll("input,select").forEach(field => {
                field.disabled = true;
                field.style.color = "gray";
            });
        }

        chkFilterFilesize.addEventListener("click", function (e) {
            this.nextSibling.querySelectorAll("input,select").forEach(field => {
                if (this.checked == true) {
                    field.removeAttribute("disabled");
                    field.style.color = "silver";
                } else {
                    field.disabled = true;
                    field.style.color = "gray";
                }
            });
        });

        // wire-up the filter tags checkbox to trigger the disabled state of its textbox
        let chkFilterTags = wrapper.querySelector("#EmporniumEnhancementsConfig_field_FilterTags");
        if (chkFilterTags.checked == false) {
            let txtTagListing = wrapper.querySelector("#EmporniumEnhancementsConfig_field_TagListing");
            txtTagListing.disabled = true;
            txtTagListing.style.color = "gray";
        }

        chkFilterTags.addEventListener("click", function (e) {
            let txtTagListing = wrapper.querySelector("#EmporniumEnhancementsConfig_field_TagListing");
            if (this.checked == true) {
                txtTagListing.removeAttribute("disabled");
                txtTagListing.style.color = "silver";
            } else {
                txtTagListing.disabled = true;
                txtTagListing.style.color = "gray";
            }
        });

        //  wire-up the must-have tags checkbox to trigger the disabled state of its textbox
        let chkOnlyShowTheseTags = wrapper.querySelector("#EmporniumEnhancementsConfig_field_OnlyShowTheseTags");
        if (chkOnlyShowTheseTags.checked == false) {
            let txtOnlyShowTagListing = wrapper.querySelector("#EmporniumEnhancementsConfig_field_OnlyShowTagListing");
            txtOnlyShowTagListing.disabled = true;
            txtOnlyShowTagListing.style.color = "gray";
        }

        chkOnlyShowTheseTags.addEventListener("click", function (e) {
            let txtOnlyShowTagListing = wrapper.querySelector("#EmporniumEnhancementsConfig_field_OnlyShowTagListing");
            if (this.checked == true) {
                txtOnlyShowTagListing.removeAttribute("disabled");
                txtOnlyShowTagListing.style.color = "silver";
            } else {
                txtOnlyShowTagListing.disabled = true;
                txtOnlyShowTagListing.style.color = "gray";
            }
        });
    },
    'save': function(forgotten) {
        let ref = GM_config.fields['FilterFilesize'].node.parentNode;
        let lowerNumber = ref.querySelector("#lowerNumber");
        let lowerUnits = ref.querySelector("#lowerUnits");
        let higherNumber = ref.querySelector("#higherNumber");
        let higherUnits = ref.querySelector("#higherUnits");

        // validate filesize number contraints
        if (/\D/.test(lowerNumber.value) || lowerNumber.value.length === 0) {
            console.log("Lower number contains non-digit or empty.");
            lowerNumber.style = "border: 1px solid red";
            lowerNumber.focus();
            lowerNumber.addEventListener("keydown", function () { lowerNumber.style = ""; });
        } else if (/\D/.test(higherNumber.value) || higherNumber.value.length === 0) {
            console.log("Higher number contains non-digit or empty.");
            higherNumber.style = "border: 1px solid red";
            higherNumber.focus();
            higherNumber.addEventListener("keydown", function () { higherNumber.style = ""; });
        } else {
            let filesizeRange = JSON.stringify({ "lowerNumber": lowerNumber.value, "lowerUnits": lowerUnits.value, "higherNumber": higherNumber.value, "higherUnits": higherUnits.value });
            GM_setValue('fileSizeRange', filesizeRange);

            // reload on successful save
            location.reload();
        }
    },
    'close': function() {
        // remove modal element on close
        document.body.removeChild(document.querySelector("#modal"));
        document.body.style.overflow = "";
    }
  }
});

GM_registerMenuCommand('Empornium Deluxe Mode Configuration', function () {
    GM_config.open();
});

function myRound(x, places) {
    let trunc = Math.pow(10, places);
	return Math.round(x * trunc) / trunc;
}

function Insert_JumpToTop() {
    if (GM_config.get('JumpToTop')) {
        // create a div fixed in the bottom-right corner
        let jumpDiv = document.createElement("div");
        Object.assign(jumpDiv.style, {
            position: "fixed",
            bottom: "5px",
            right: "5px",
            textAlign: "right"
        });

        // create the 'Jump to Top' link
        let anchor = document.createElement("a");
        anchor.innerText = "Jump to Top";
        anchor.href = "javascript:window.scrollTo(0, 0)";

        // append the link to the corner div
        jumpDiv.appendChild(anchor);

        // append the entire element to the body of the page
        document.body.appendChild(jumpDiv);
    }
}

function Affix_Header() {
    if (GM_config.get('AlwaysHeader')) {
        if (document.querySelectorAll("#header").length > 0) {
            // affix the header into position
            document.querySelector("#header").style.cssText = "position: fixed !important; top: 0px; left: 0px;";
            // shift the contents down to account for missing space
            document.querySelector("#content").style.cssText = "margin-top:120px;";
        }
    }
}

function Strip_Anon(links) {
    if (GM_config.get('StripAnonym')) {
        // list of url anonymizers to remove
        let anonymizers = [ "http://anonym.to/?", "http://anon.now.im/?" ];

        // loop over each link
        links.forEach(link => {
            // loop thru the list of anonymizers
            anonymizers.forEach(anonymizer => {
                // if link contains current anonymizer
                if (link.href.indexOf(anonymizer) >= 0) {
                    // replace it with empty string
                    link.href = link.href.replace(anonymizer, "");
                }
            });
        });
    }
}

function Prevent_NewWindow(links) {
    if (GM_config.get('PreventNewWindow')) {
        // loop over each link
        links.forEach(link => {
            // if a new window target has been set for current link
            if (link.target.length > 0) {
                // remove its target
                link.target = '';
            }
        });
    }
}

function AutoOpen_Spoilers(links) {
    if (GM_config.get('AutoOpenSpoilers')) {
        // find all spoiler links
        links.filter(r => r.innerHTML === "Show").forEach(link => {
            // click to unhide
            link.click();
        });
    }
}

function AutoDismiss_LoginTimeout() {
    if (GM_config.get('AutoDismiss')) {
        if (document.querySelectorAll("#flashClose").length > 0) {
            document.querySelector("#flashClose").click()
        }
    }
}

function RedirectTo_LoginScreen(links) {
    if (links.filter(r => r.innerHTML === "Login").length > 0) {
        window.location.href += "login";
    }
}

function SetFocus_LoginForm() {
    document.querySelector("input[name=username]").focus();
}

function Show_RatioGoals() {
    if (GM_config.get('ShowRatioGoals')) {
        var stats = document.querySelectorAll(".stat");
        var units = [ "KiB", "MiB", "GiB", "TiB" ];

        var ratio = parseFloat(stats[5].innerText);
        var down = stats[3].innerText;
        var up = stats[1].innerText;

        var upUnitsIdx = units.indexOf(up.substring(1 + up.indexOf(" ")));
        var downUnitsIdx = units.indexOf(down.substring(1 + down.indexOf(" ")));

        var unitsDiff = parseInt(Math.round(upUnitsIdx - downUnitsIdx));
        var displayUnits = units[downUnitsIdx];

        up = up.replace(/,/g, "");
        up = parseFloat(up.substring(0, up.indexOf(" ")));
        down = down.replace(/,/g, "");
        down = parseFloat(down.substring(0, down.indexOf(" ")));

        // convert totals to same units
        if (unitsDiff < 0) {
            down *= Math.pow(1024, -unitsDiff);
            displayUnits = units[upUnitsIdx];
        } else if (unitsDiff > 0) {
            up *= Math.pow(1024, unitsDiff);
        }

        var diff = down / 100.0;
        var currAmount = Math.abs((ratio + 0.01) * down - up);

        let strHtml = "<table style=\"text-align:center !important;margin: 0px auto; width:50%;\">\
                           <tbody>\
                               <tr>\
                                   <td style=\"width:90px;text-align:center;\">U/L differential</td>\
                                   <td style=\"width:90px;text-align:center;\"> for ratio </td>\
                                   <td style=\"width:90px;text-align:center;\"> Amount to U/L </td>\
                               </tr>";

        for (let i = 1; i <= 10; i++) {
            var currRatio = i/100.0 + ratio;

            // alternate background color
            var style = "";
            if (i % 2 != 0) {
                style = " style=\"background-color:#222222 !important;\"";
            }

            strHtml += "<tr" + style + "><td style=\"text-align:center;\"> +" + (i == 1 ? myRound(currAmount, 3) : myRound(diff, 3)) + " " + displayUnits + " </td><td style=\"text-align:center;\"> " + myRound(currRatio, 3) + " </td><td style=\"text-align:center;\"> " + myRound(currAmount, 3) + " " + displayUnits + " </td></tr>";

            currAmount += diff;
        }

        strHtml += "</tbody></table>";

        var content = document.querySelector("#content");
        content.innerHTML = strHtml + content.innerHTML;
    }
}

function Hide_Seeded(torrents) {
    if (GM_config.get('HideSeeded')) {
        torrents.filter(r => r.querySelectorAll(".icon_disk_seed").length > 0).forEach(itemRow => { itemRow.style = "display:none"; });
    }
}

function Hide_Grabbed(torrents) {
    if (GM_config.get('HideGrabbed')) {
        torrents.filter(r => r.querySelectorAll(".icon_disk_grabbed").length > 0).forEach(itemRow => { itemRow.style = "display:none"; });
    }
}

function Hide_Snatched(torrents) {
    if (GM_config.get('HideSnatched')) {
        torrents.filter(r => r.querySelectorAll(".icon_disk_snatched").length > 0).forEach(itemRow => { itemRow.style = "display:none"; });
    }
}

function Hide_Leeching(torrents) {
    if (GM_config.get('HideLeeching')) {
        torrents.filter(r => r.querySelectorAll(".icon_disk_leech").length > 0).forEach(itemRow => { itemRow.style = "display:none"; });
    }
}

function Filter_Tags(torrents) {
    if (GM_config.get('FilterTags')) {
        // grab the filters from the settings
        let filters = GM_config.get('TagListing').replace(/[\n\r\s]/g, "").split(',');

        // loop over each torrent
        torrents.forEach(itemRow => {
            // grab the current torrent's list of tags
            let tags = itemRow.querySelector(".tags").innerText.split(" ");

            // if the filter keywords and tags have items in common
            if (filters.intersects(tags)) {
                // hide this torrent row
                itemRow.style = "display:none";
                console.log("Filtered these tags: " + JSON.stringify(filters.intersect(tags)));
            }
        });
    }
}

function Display_ImagesInline(torrents) {
    if (GM_config.get('ShowImages')) {
        // loop over each torrent
        torrents.forEach(itemRow => {
            // if current torrent row contains a hover script and a cats_col class of element exists within it
            if (itemRow.querySelectorAll("script").length > 0 && itemRow.querySelectorAll("[class*=cats_col]").length > 0) {
                // extract the image-generating html from the hover script
                let strHtml = itemRow.querySelector("script").innerHTML.replace(/[\[\]\{\}\(\)\\\|]/g, "");
                let start = strHtml.indexOf("\"")+1;
                let end = strHtml.lastIndexOf("\"");
                strHtml = strHtml.substring(start, end);

                // create a div within which to place the image
                let div = document.createElement("div");

                // hide the cats_col's child elements which contain a definite title to make room for the image
                Array.from(itemRow.querySelector("[class*=cats_col]").childNodes).filter(r => r.title !== undefined).forEach(cat => { cat.style.display = "none"; });

                // add the html to generate the image to the div
                div.innerHTML = strHtml;

                // display the image in the torrent row
                itemRow.querySelector("[class*=cats_col]").appendChild(div);
            }
        });
    }
}

function Hide_ClearAll(links) {
    if (GM_config.get("HideClearAll")) {
        // hide "clear all" and "Clear"
        links.filter(r => r.innerHTML === "(clear all)" || r.innerHTML === "Clear").forEach(link => { link.style.display = "none"; });
    }
}

function AutoCheck_ClickedTorrent(torrents) {
    if (GM_config.get("AutoCheckNotif")) {
        // loop over entire torrent list
        torrents.forEach(itemRow => {
            // filter-out links on current torrent that do not contain "/torrents.php?id"
            Array.from(itemRow.querySelectorAll("a")).filter(r => r.href.indexOf("/torrents.php?id") > -1).forEach(link => {
                link.addEventListener("mousedown", function (e) {
                    // activate checkbox of current torrent if notification link was clicked
                    itemRow.querySelector("input[type=checkbox]").checked = true;
                });
            });
        });
    }
}

function Move_ClearToGroupBottom() {
    if (GM_config.get("CopyClearBottomNotifs")) {
        // look for the torrent_table class of elements
        document.querySelectorAll(".torrent_table").forEach(t => {
            // find the current table's previous sibling's previous sibling
            let ps = t.previousSibling.previousSibling;
            // clone the element
            let c = ps.cloneNode(true);
            // insert at the bottom
            t.parentNode.insertBefore(c, t.nextSibling);
            ps.innerHTML = ps.innerHTML.toString().match(/(\w+\s){4}/gm);
        });
    }
}

function Filter_Filesizes(torrents) {
    if (GM_config.get("FilterFilesize")) {
        let units = [ "KiB", "MiB", "GiB", "TiB" ];
        let filesizeRange = JSON.parse(GM_getValue('fileSizeRange'));

        // loop over each torrent
        torrents.forEach(itemRow => {
            // retrieve filesize of current torrent
            let currSize = Array.from(itemRow.querySelectorAll(".nobr")).filter(r => /^[\d\.,]+\s.iB$/.test(r.innerText))[0].innerText.split(" ");

            // hide row if units of current row are less than units of min range
            if (units.indexOf(currSize[1]) < units.indexOf(filesizeRange.lowerUnits)) {
                itemRow.style.display = "none";
            } else if (currSize[1] === filesizeRange.lowerUnits) {
                // hide row if units match and filesize is less than min range
                if (parseFloat(currSize[0]) < parseInt(filesizeRange.lowerNumber)) {
                    itemRow.style.display = "none";
                }
            }

            // hide row if units of current row are greater than units of max range
            if (units.indexOf(currSize[1]) > units.indexOf(filesizeRange.higherUnits)) {
                itemRow.style.display = "none";
            } else if (currSize[1] === filesizeRange.higherUnits) {
                // hide row if units match and filesize is greater than max range
                if (parseFloat(currSize[0]) > parseInt(filesizeRange.higherNumber)) {
                    itemRow.style.display = "none";
                }
            }
        });
    }
}

function Draw_MenuItem() {
    if (document.querySelector(".username") != null) {
        // config icon - wrench & screwdriver depicted
        let icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA\
                    u0lEQVQ4jZ2SMQ4CMQwERyiICqEr+EGgRBelpkvLI3kDD+IBdDQUFKbJIcdyTqezlGazu7\
                    ZXBlM55+1ajFLyYS2mQanv4hFDCNP/rKNUoid+NAZql5MxFODriMtkEGPcNV2BsxIL8O6J\
                    vfVE7fd/w7C/LhFjOosxTEvETTnTjK7YO4hKDM40o+bZEO0qnwrZbNKSHF4OZoNtK6V4VC\
                    QtnkoAUYZuZwE2vRHVdbqXeO90ttgTuEEnRA+cw36EkV9UhABsAgAAAABJRU5ErkJggg==";
        // find the user nav element
        let navbar = document.querySelector(".username").parentNode.querySelector("ul");
        // clone a list item layout
        let configNode = navbar.querySelector("li").cloneNode(true);

        // wire-up the config element
        configNode.id = "deluxe_config";
        configNode.innerHTML = "";
        let anchor = document.new("a").appendTo(configNode);
        anchor.title = "Empornium Deluxe Mode Configuration";
        anchor.innerHTML = '<img src="' + icon + '" style="filter:invert(100%);"/> Deluxe Mode Config';
        anchor.href = "javascript:void(0)";
        anchor.addEventListener("click", function (e) {
            GM_config.open();
        });

        // display config link as first item in the user nav
        navbar.insertBefore(configNode, navbar.firstChild);
    }
}

function AutoThank_Uploader() {
    if (GM_config.get("AutoThankUploader")) {
        // .blueButton => Download
        // .greenButton => Freeleech
        // .orangeButton => Doubleseed
        // look for the download / freeleech / doubleseed buttons
        Array.from(document.querySelectorAll(".blueButton,.greenButton,.orangeButton")).forEach(btn => {
            // wire-up event to auto-click thanks upon downloading
            btn.addEventListener("click", function (e) {
                window.setTimeout(function () {
                    // if thanks button not disabled
                    if (!document.querySelector("#thanksbutton").disabled) {
                        // invoke it
                        document.querySelector("#thanksbutton").click();
                    }
                    // wait 500ms so as not to interrupt download request
                }, 500);
            });
        });
    }
}

function AutoOpen_FileList(links) {
    if (GM_config.get("AutoOpenFileList")) {
        links.filter(r => r.innerHTML === "(View Filelist)")[0].click();
    }
}

function OnlyShow_TheseTags(torrents) {
    if (GM_config.get('OnlyShowTheseTags')) {
        // grab the filters from the settings
        let filters = GM_config.get('OnlyShowTagListing').replace(/[\n\r\s]/g, "").split(',');

        // loop over each torrent
        torrents.filter(r => r.style.display !== "none").forEach(itemRow => {
            // grab the current torrent's list of tags
            let tags = itemRow.querySelector(".tags").innerText.split(" ");

            // if the filter keywords and tags do not have items in common
            if (!filters.intersects(tags)) {
                // hide this torrent row
                itemRow.style = "display:none";
                console.log("These tags aren't whitelisted and therefore the torrent was hidden: " + JSON.stringify(tags.filter(r => r !== "")));
            }
        });
    }
}



// main
(function() {
    'use strict';

    AutoDismiss_LoginTimeout();
    Draw_MenuItem();

    let links = Array.from(document.querySelectorAll("a"));
    let torrents = Array.from(document.querySelectorAll(".torrent"));

    // page match rules
    if (/empornium\.me\/?$/.test(window.location.href)) {
        RedirectTo_LoginScreen(links);
    } else if (/empornium\.me\/login$/.test(window.location.href)) {
        SetFocus_LoginForm();
    } else {
        // occurs on all authenticated pages
        Affix_Header();
        Strip_Anon(links);
        Prevent_NewWindow(links);
        Insert_JumpToTop();


        if (/torrents\.php.+action=notify/.test(window.location.href)) {
            // notifications page
            Hide_ClearAll(links);
            Move_ClearToGroupBottom();
            AutoCheck_ClickedTorrent(torrents);
        }

        if (/(torrents|top10|user)\.php/.test(window.location.href) && !/(\?|&)id=/.test(window.location.href)) {
            // torrents / top10 / user - lists of torrents
            Filter_Tags(torrents);
            Filter_Filesizes(torrents);
            OnlyShow_TheseTags(torrents);

            Hide_Seeded(torrents);
            Hide_Grabbed(torrents);
            Hide_Snatched(torrents);
            Hide_Leeching(torrents);

            Display_ImagesInline(torrents);
        } else if (/torrents\.php\?id=\d+/.test(window.location.href)) {
            // single torrent landing page
            AutoOpen_Spoilers(links);
            AutoOpen_FileList(links);
            AutoThank_Uploader();
        } else if (/bonus\.php/.test(window.location.href)) {
            // bonus page
            Show_RatioGoals();
        }
    }
})();