Danbooru-Image-Adder

Add images to posts

Verze ze dne 07. 07. 2017. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Danbooru-Image-Adder
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add images to posts
// @author       ECHibiki /qa/
// @match http://boards.4chan.org/*
// @match https://boards.4chan.org/*
// @grant         GM_xmlhttpRequest
// ==/UserScript==

var numberOfPosts; 
var pageNo;
var JSONPage;

var setPostAndPage = function(){
    numberOfPosts = Math.floor(Math.random() * 100 % 20);
    console.log("Post No: " + numberOfPosts);

    pageNo = Math.floor(Math.random() * 100 % 10 + 1);
    console.log("Page No: " + pageNo);
};

var enhance4ChanX = function(){


    if(document.getElementById("qrImages") !== null) qrWindow.removeChild(document.getElementById("qrImages"));

    var qrWindow = document.getElementById("qr");

    var qrTable = document.createElement("TABLE");
    qrTable.setAttribute("id", "qrImages");
    qrWindow.appendChild(qrTable);
    qrWindow.appendChild(document.createElement("BR"));

    var instructionRow = document.createElement("TR");        
    var topRowNodes = [document.createElement("BR"),
                       document.createTextNode("Insert Tags to search from danbooru bellow."),
                       document.createElement("BR"),
                       document.createTextNode("No error messages will apear at this point in time."),
                      ];
    topRowNodes.forEach(
        function(node){
            instructionRow.appendChild(node);
        });
    qrTable.appendChild(instructionRow);

    var tagRow = document.createElement("TR");
    var secondRowNodes = [
        document.createTextNode("Tags: "),
        document.createElement("INPUT"),
        document.createElement("INPUT"),
    ];
    secondRowNodes.forEach(
        function(node){
            tagRow.appendChild(node);
        });
    qrTable.appendChild(tagRow);

    secondRowNodes[1].setAttribute("ID", "tags");

    secondRowNodes[2].setAttribute("ID", "imageButton");
    secondRowNodes[2].setAttribute("type", "button");
    secondRowNodes[2].setAttribute("value", "Set Image");
    secondRowNodes[2].addEventListener("click", function(){
        console.log("exce");
        var tags = document.getElementById("tags").value;
        console.log("tags: " + tags);
        tags = tags.split(" ");
        if(tags[1] === undefined) tags[1] = "";

        setPostAndPage();
        console.log("PageNo: " + pageNo + " PostNo: " + numberOfPosts);

        //GET AND SAVE JSON OF GIVEN PAGE USING TAG NUMBER 1 AND 2 TO 
        getJSON("https://danbooru.donmai.us/posts.json?page=" + pageNo + "&utf8=%E2%9C%93&tags=" + tags[0] + "+" + tags[1], setImageFromDanbooru, tags);
        //SCAN PAGE FOR TAGS GOING UPWARDS 
        //IF PAST POST 20 SAVE THE NEXT PAGE
        //IF A MATCH CHECK FILESIZE
        //IF LESS THAN 4MB USE FULL ELSE USE SAMPLE SIZE

    });

};

var setImageFromDanbooru = function(err, data, tags){
    if (err != null) {
        console.log('Something went wrong: ' + err);
        pageNo = 1;
        numberOfPosts = 0;
    }
    else {
        var JSONPage = data;
        while(JSONPage["" + numberOfPosts] !== undefined){
            var endURL = JSONPage["" + numberOfPosts].file_url;       
            var URL = "https://danbooru.donmai.us" + endURL;
            console.log("URL: " + URL);
            console.log("PageNo: " +pageNo);
            console.log("PostNo: " +numberOfPosts);

            var fail = false;

            if(endURL === undefined || 
               endURL.indexOf(".mp4") > -1 || endURL.indexOf(".webm") > -1){
                numberOfPosts++;
                if(numberOfPosts == 20){
                    numberOfPosts = 0;
                    pageNo++;
                    getJSON("https://danbooru.donmai.us/posts.json?page=" + pageNo + "&utf8=%E2%9C%93&tags=" + tags[0] + "+" + tags[1], setImageFromDanbooru, tags);
                    return;
                }
                continue;
            }
            else{
                tags.forEach(function(tag){
                    if(tag.indexOf("order:") > -1);
                    else if(tag.indexOf("rating:") > -1){
                        console.log(tag.charAt(7));
                        if(tag.charAt(7) !== JSONPage["" + numberOfPosts]["rating"]){
                            fail = true;
                            return;
                        }
                    }
                    else if(JSONPage["" + numberOfPosts]["tag_string"].indexOf(tag) == -1){
                        fail = true;
                        return;
                    }
                });
            }
            if(fail){
                numberOfPosts++;
                if(numberOfPosts == 20){
                    numberOfPosts = 0;
                    pageNo++;
                    getJSON("https://danbooru.donmai.us/posts.json?page=" + pageNo + "&utf8=%E2%9C%93&tags=" + tags[0] + "+" + tags[1], setImageFromDanbooru, tags);
                    return;
                }
                continue;
            }
            else{
                if(JSONPage["" + numberOfPosts].file_size >= 4000000){
                    var endURL = JSONPage["" + numberOfPosts].large_file_url;       
                    var URL = "https://danbooru.donmai.us" + endURL;
                    console.log("URL: " + URL);
                    console.log("PageNo: " +pageNo);
                    console.log("PostNo: " +numberOfPosts);
                }
                var xhr = new GM_xmlhttpRequest(({
                    method: "GET", 
                    url: URL, 
                    responseType : "arraybuffer",
                    onload: function(response)
                    {
                        var blob;
                        if(endURL.indexOf(".jpg") > -1)
                            blob = new Blob([response.response], {type:"image/jpeg"});
                        else if(endURL.indexOf(".png") > -1)
                            blob = new Blob([response.response], {type:"image/png"});
                        else if(endURL.indexOf(".gif") > -1)
                            blob = new Blob([response.response], {type:"image/gif"});


                        var name = endURL.replace(/(data|cached)/g, ""); 
                        name = name.replace(/\//g, ""); 

                        console.log("----------------");
                        console.log("Blob: " + blob);
                        console.log("MIME: " + blob.type);
                        console.log("Name: " + name);

                        //SEND RESULTING RESPONSE TO 4CHANX FILES === QRSetFile
                        var detail = {file:blob, name:name};
                        if (typeof cloneInto === 'function') {
                            detail  = cloneInto(detail , document.defaultView);
                        }
                        console.log("Detail: " +detail);
                        document.getElementById("dump-list").firstChild.click();
                        document.dispatchEvent(new CustomEvent('QRSetFile', {bubbles:true, detail}));

                        //document.getElementById("url-button").click();
                        numberOfPosts++;
                        if(numberOfPosts == 20){
                            numberOfPosts = 0;
                            pageNo++;
                        }
                    }
                }));
                //break condition
                return;
            }
        }
    }
};

var getJSON = function(url, callback, extra) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
        var status = xhr.status;
        if (status == 200) {
            callback(null, xhr.response, extra);
        } else {
            callback(status);
        }
    };
    xhr.send();
};

window.onload = function(){
    console.log(document.links);
    var len = document.links.length;
    for(var i = 0 ; i < len ; i++){
        document.links[i].addEventListener("click", enhance4ChanX);
    }
};