IUC / TokkiPlugin_MediaDownloadHelper

// ==UserScript==
// @name         TokkiPlugin_MediaDownloadHelper
// @namespace    http://tokki.be
// @version      0.5
// @description  Media download helper for kch
// @author       IUC
// @license      MIT
// @match        https://kpop.re/*/*
// @excludes     https://kpop.re/all/*
// @grant        GM_download
// ==/UserScript==

// This will make your (You)s on kch/wood/ very visible and give you a one-click download button for files in every post
(function() {
    'use strict';

    // Wait until site is fully loaded
    window.addEventListener('load', function() {

        // Download stuff
        var allPosts = document.getElementsByClassName("post");
        for (let post of allPosts) {
            addDownloadButton(post);
        }
        function addDownloadButton(post) {
            let fileLinks = [];
            fileLinks[0] = post.getAttribute("id");
            var fileIndex = 1;
            var section = post.children[1];
            if (section.children.length > 1) {
                var footerPostControls = post.children[2].children[1];
                var fileDiv = section.children[0];
                var figures = fileDiv.children;
                for (let figure of figures) {
                    var postFile = figure.children[1];
                    fileLinks[fileIndex] = postFile.getAttribute("href");
                    fileIndex++;
                }
                if (fileLinks.length>2){
                    // Adding checkboxes with label
                    for (let c = 1; c<fileLinks.length;c++) {
                        let boxContainer = document.createElement("a");
                        let fileCheckbox = document.createElement("input");
                        fileCheckbox.type = "checkbox";
                        fileCheckbox.id = "file"+ c.toString();
                        let fileCheckBoxLabel = document.createElement("label");
                        //fileCheckBoxLabel.htmlFor = "file"+ c.toString();
                        fileCheckBoxLabel.appendChild(document.createTextNode(c.toString()));
                        fileCheckBoxLabel.style.marginRight = "3px";
                        boxContainer.appendChild(fileCheckBoxLabel);
                        boxContainer.appendChild(fileCheckbox);
                        boxContainer.classList.add("post-control");
                        boxContainer.style.marginRight = "5px";
                        footerPostControls.appendChild(boxContainer);

                    }
                }
                // Adding download button with icon
                var downloadNode = document.createElement("a");
                var downloadIcon = document.createElement("i");
                downloadIcon.classList.add("fa", "fa-download");
                downloadNode.appendChild(downloadIcon);
                downloadNode.setAttribute("id", "dlButton");
                downloadNode.classList.add("post-control", "control");
                downloadNode.style.marginLeft = "8px";
                footerPostControls.appendChild(downloadNode);
                downloadNode.addEventListener('click', function(){
                    download(fileLinks);
                });
            }
        }
        function download(links) {
            var amountOfFiles = links.length -1;
            if (amountOfFiles > 1) {
                let postNode = document.getElementById(links[0]);
                let checkCounter = amountOfFiles;
                for (let g = 1; g<amountOfFiles+1;g++){
                    let checkBoxValue = postNode.querySelector("#file" + g.toString()).checked;
                    if (checkBoxValue) {
                        let args = {
                            url: links[g],
                            name: links[g].slice(links[g].lastIndexOf("/")+1, links[g].length)
                        }
                        GM_download(args);
                    }
                    else {
                        checkCounter--;
                    }
                }
                if (checkCounter < 1) {
                    for (let i = 1; i<links.length;i++) {
                        let args = {
                            url: links[i],
                            name: links[i].slice(links[i].lastIndexOf("/")+1, links[i].length)
                        }
                        GM_download(args);
                    }
                }
            }
            else {
                    let args = {
                        url: links[1],
                        name: links[1].slice(links[1].lastIndexOf("/")+1, links[1].length)
                    }
                    GM_download(args);
            }
        }

        //listener for new posts
        var target = document.getElementsByClassName("thread thread_single")[0];
        var mutationObs = new MutationObserver(function(mutations, mutationObs){
            // Get new post into addedNode
            var mutation = mutations[0];
            var addedNode = mutation.addedNodes[0];
            // Adding download button to new posts
            addDownloadButton(addedNode);

        });
        var config = {
            attributes: false,
            childList: true,
            subtree: false
        }
        mutationObs.observe(target, config);

    }, false);
})();