Blotter updated: 06/17/12Show/Hide Show All

Image

Tag History
(edit info)
Rating

Prev | Index | Next

Comments

redweasel
#235636
1 year ago
working on a script to make doing stuff on ponibooru easier. makes like "tag" mode where it tags a picture instead of going to it. or "delete" mode for admins. might as well put it here if people want.

as always,
https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
make a new script and add what's below:

// ==UserScript==
// @name           ponibooru mode changer
// @namespace      hax
// @include        http://ponibooru.413chan.net/
// @include        http://ponibooru.413chan.net/post/list
// @include        http://ponibooru.413chan.net/post/list/*
// ==/UserScript==

var chooser = document.createElement("select")

function addChoice(label,handler) {
    let opt = document.createElement("option")
    opt.appendChild(document.createTextNode(label))
    opt.thhvalue = handler;
    chooser.appendChild(opt);
}

chooser.addEventListener('change',function (event) {
    let choice = chooser.options[chooser.selectedIndex].thhvalue;
    choice();
},false);

var body = document.getElementsByTagName('body')[0];
body.insertBefore(chooser,body.firstChild);

var workspace = document.createElement('div');
body.insertBefore(workspace,chooser.nextSibling);

//////////////////////////////////////////////////////////////////////////////////////

var findID = new RegExp("view/([0-9]+)");

function setClicker(what) {
    for each (let span in document.getElementsByTagName('span')) {
        if(span.className != "thumb") continue;
        for each (let a in span.getElementsByTagName('a')) {
            if(a==undefined) continue;
            // XXX: this test is because sometimes a is the number 1 (wtf?)
            if(a.href == undefined) continue;
            if(a.clicker==null) {
                let m = findID.exec(a.href);
                if(m==null) {
                    GM_log("wtf is "+a);
                    a.number = 666;
                } else {
                    a.number = parseInt(m[1]);
                }
            } else {
                a.removeEventListener('click',a.clicker,true);
            }
            a.clicker = what(a);
            if(a.clicker != false)
                a.addEventListener('click',a.clicker,true);
        }
    }
}

function clearWorkspace() {
    while(workspace.firstChild!=null) {
        workspace.removeChild(workspace.firstChild);
    }
}

// https://developer.mozilla.org/en/Code_snippets/HTML_to_DOM#Safely_parsing_simple_HTML.c2.a0to_DOM

function pageExaminer() {
    let frame = document.createElement('iframe');
    frame.setAttribute("collapsed", "true");
    frame.setAttribute("type","content-targetable");
    frame.setAttribute("sandbox","allow-top-navigation");
    frame.style.display = 'none';
    frame.style.visible = 'false';
    document.documentElement.appendChild(frame);
    return frame;
}

function examinePage(number,onLoad) {
    let examiner;
    if(examiners[number] == undefined) {
        examiner = pageExaminer();
        examiners[number] = examiner;
    } else {
        examiner = examiners[examiner];
    }
    examiner.addEventListener('load',function(event) { onLoad(examiner,event); return true; },true);
    examiner.src = "/post/view/"+number;
}


var examiners = {};

function addTags(number,tags) {
    GM_log(number + ": adding "+tags);
    function mergeTags(examiner,event) {
        let doc = examiner.contentDocument;
        let div = doc.getElementById('imgdata');
        let img = doc.getElementById('main_image');
        img.parentNode.removeChild(img);
        img = null;
        if(div==null) {
            GM_log("arg div is not supposed to be null");
        }
        let form = div.getElementsByTagName('form')[0];
        let token = undefined;
        let oldtags = undefined;
        for each (let element in form.getElementsByTagName('input')) {
            if(element.tagName != 'INPUT') continue;
            if (element.name=='auth_token') {
                token = element.value;
            } else if(element.name == 'tag_edit__tags') {
                oldtags = element.value;
            }
         /*   if(!(oldtags==undefined || token == undefined)) {
                break;
            }*/
        }
        if(token == undefined) {
            GM_log("Could not find auth token.");
        } else {
            the_auth_token = token;
        }

        if(oldtags==undefined) {
            GM_log("OLd tags not found!");
            throw "OARGC";
        }
        let params = "auth_token="+token+
            "&image_id="+number+
            "&tag_edit__tags="+encodeURI(oldtags)+" "+encodeURI(tags);
        
        GM_log(number+": merging "+oldtags+" with "+tags);
        GM_xmlhttpRequest({
            method: form.method,
            url: form.action,
            headers: {
                "Content-type": "application/x-www-form-urlencoded",
                "Content-length": params.length,
                "Connection": "close"},
            data: params,
            onload: function(responseDetails) {
                GM_log(number+": has now tags "+tags);
        	}
        });
    }

    examinePage(number,mergeTags);
}

function deleteImage(number) {
    function onLoad(examiner,event) {
        let doc = examiner.contentDocument;
        let div = doc.getElementById('imgdata');
        let img = doc.getElementById('main_image');
        if(img!=undefined) {
            img.parentNode.removeChild(img);
            img = null;
        }
        if(div==null) {
            GM_log("arg div is not supposed to be null");
        }
        let form = div.getElementsByTagName('form')[0];
        let token = undefined;
        for each (let element in form.getElementsByTagName('input')) {
            if(element.tagName != 'INPUT') continue;
            if (element.name=='auth_token') {
                token = element.value;
            }
        }
       let params = 
        "auth_token="+token+
        "&image_id="+number;
 
        GM_xmlhttpRequest({
                method: "POST",
                url: "/image_admin/delete",
                headers: {
                    "Content-type": "application/x-www-form-urlencoded",
                    "Content-length": params.length,
                    "Connection": "close"},
                data: params,
                onload: function(responseDetails) {
                    GM_log(number+" has been deleted.");
            	}
        });
    }
    examinePage(number,onLoad);
}


function prepareTags() {
    clearWorkspace();
    let tags = document.createElement('input');
    tags.setAttribute('type','text');
    workspace.appendChild(tags);
    setClicker(function (a) {
        return function(event) {
            event.preventDefault();
            GM_log("Tag " + a.number + " with " + tags.value);
            addTags(a.number,tags.value);
            return false;
        };
    });
}

addChoice("nothing",function() { GM_log("can't see shit captain"); });
addChoice("reset",function() { 
    clearWorkspace();
    setClicker(function (a) {
        return false;
    });
});

function preparePresetTags(tags) {
    return function() {
        clearWorkspace();
        setClicker(function (a) {
            return function(event) {
                event.preventDefault();
                addTags(a.number,tags);
                return false;
            };
        });
    };
}


addChoice("repetitive",preparePresetTags("repetitive"));
addChoice("tags",prepareTags);
addChoice("delete",function() { 
    setClicker(function(a) {
        return function(event) {
            event.preventDefault();
            deleteImage(a.number);
            return false;
        }
    });
});

addChoice("alerty",function() { 
    clearWorkspace();
    setClicker(function (a) {
        return function(event) {
            event.preventDefault();
            alert("Got an AAA "+a.number);
            return false;
        };
    }); 
});
DaisyHead
#235641
1 year ago
how long did it take you to write that?
Jackarunda
#235642
1 year ago
I CAN'T READ ANY CODE BUT LUA ;_;
YandereShy
#235649
1 year ago
why is that fagget strider on that?
redweasel
#235651
1 year ago
it took me most of yesterday afternoon. <_<

hard part was waiting for the individual picture page to load, to get needed info from it.

plan on commenting it better and adding a div to put log messages in (i.e. "you can click next now we're done tagging")
redweasel
#235653
1 year ago
strider is my bishie <3
Turin_Aramaia
#235713
1 year ago
note to browser cache collectors: ponibooru comments are autodeleted. You'll need to save manually or use Offline Explorer to save the comments. (Offline Explorer won't save images that have had their tags changed before you get to them. Needs a different script to manage that, kinda like 'on failure, re-load referral page, if error= "image not exist" then break callback.')
Turin_Aramaia
#235715
1 year ago
i meant to say "ponibooru image/comment pages are autodeleted" It's only loaded into memory, it does not go in the cache.
Fuzzy_Logic
#236448
1 year ago
Do you think you could add an auto-pager to this? Something that loads another page of pictures as you scroll down? There's an existing auto-pager extension for firefox in general, but I can't get it to work with the latest firefox... ;-;