Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

Make7

(8,543 posts)
Sun Dec 22, 2013, 01:22 AM Dec 2013

DU3 Mail multi-select delete GreaseMonkey script

Last edited Sun Dec 22, 2013, 03:45 AM - Edit history (3)

Want to have check boxes to select multiple DU Mails and delete them without having to open them? If you use FireFox and have the GreaseMonkey add-on installed this script will give you that functionality. (This should also work with Chrome/TamperMonkey or Opera(WebKit)/TamperMonkey - although I have not tested it in anything other than FireFox.)

This version is for testing. It will open an alert dialog to show the delete URL that would load and then loads the page to view the DU Mail that would be deleted. Once you are sure things are working as intended on your system, you can comment out the test lines and uncomment the lines that will actually perform the deletions (search for the text FOR TESTING to see what to to).

By default the script will bring up a confirm dialog before deleting each DU Mail, to defeat that you can set confirmEachDeleteFlag at the beginning of the code to false and no confirmation will be required.

[div class="excerpt" style="margin-left:1em; border:1px solid #bfbfbf; border-radius:0.4615em; box-shadow:-1px -1px 3px #999999 inset;"]// ==UserScript==
// @name DU Mail multi-select delete
// @description auto-loads selected DU Mail delete URLs
// @include http://www.democraticunderground.com/?com=inbox*
// @version 1
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==

var confirmEachDeleteFlag=true;

function selectToggle(cbox) {

    if(cbox.innerHTML==String.fromCharCode(9744))
        cbox.innerHTML=String.fromCharCode(9746);
    else
        cbox.innerHTML=String.fromCharCode(9744);
}

function checkAll(stat) {

    var msgs=document.getElementsByClassName("PM-delete-select&quot ;

    for(var i=0,lgth=msgs.length;i<lgth;i++) {

        if(stat==true)
            msgs[i].innerHTML=String.fromCharCode(9746);
        else
            msgs[i].innerHTML=String.fromCharCode(9744);
    }
}

function deleteNextMessage() {

    var sent=location.search.indexOf('sent');

    var page=GM_getValue("lstPage&quot ;
    var list=GM_getValue("selMsgs&quot ;

    if(list.length) {

        var msgs=list.split(',');
        var next=msgs.pop();

        list=msgs.join();

        GM_setValue("selMsgs",list);

        // CONFIRM DELETE MESSAGE
        try {
            var cftx='Delete the following message?\n\n';

            var mtch='view='+next+'"';
            var tofm='FROM';
            if(sent!=-1) {
                mtch='viewsent='+next+'"';
                tofm='TO';
            }
            var lmnt=document.getElementsByTagName("tbody&quot ;
            var tbrw=lmnt[0].getElementsByTagName("tr&quot ;

            for(var i=tbrw.length-1;i>=0;i--) {

                var usnm=tbrw[i].getElementsByTagName("a&quot [0];
                var ttle=tbrw[i].getElementsByTagName("a&quot [1];

                if(ttle.outerHTML.indexOf(mtch)!=-1) {

                    cftx=cftx+'\n'+tofm+': '+usnm.innerHTML+'\n\nTITLE: '+ttle.innerHTML;
                    break;
                }
            }
        }
        catch(err) {
            cftx=cftx+next;
        }
        var cnfm=false;
        if(confirmEachDeleteFlag==true)
            cnfm=confirm(cftx);

        if((cnfm==true)||(confirmEachDeleteFlag==false)) {

            if((page.length)&&(sent==-1)) {

                // location.replace(page+'&action=delete&mid='+next+'#autoDeleting');
                // FOR TESTING COMMENT OUT THE LINE ABOVE THIS AND UNCOMMENT THE TWO BELOW
                alert('HREF='+page+'&action=delete&mid='+next+'#autoDeleting');
                location.replace(page+'&view='+next+'#autoDeleting');
            }
            else if(page.length) {

                // location.replace(page+'&action=deletesent&mid='+next+'#autoDeleting');
                // FOR TESTING COMMENT OUT THE LINE ABOVE THIS AND UNCOMMENT THE TWO BELOW
                alert('HREF='+page+'&action=deletesent&mid='+next+'#autoDeleting');
                location.replace(page+'&viewsent='+next+'#autoDeleting');
            }
            else if(sent==-1) {

                // location.replace('http://'+location.hostname+'/?com=inbox&action=delete&mid='+next+'#autoDeleting');
                // FOR TESTING COMMENT OUT THE LINE ABOVE THIS AND UNCOMMENT THE TWO BELOW
                alert('http://'+location.hostname+'/?com=inbox&action=delete&mid='+next+'#autoDeleting');
                location.replace('http://'+location.hostname+'/?com=inbox&view='+next+'#autoDeleting');
            }
            else {
                // location.replace('http://'+location.hostname+'/?com=inbox&action=deletesent&mid='+next+'#autoDeleting');
                // FOR TESTING COMMENT OUT THE LINE ABOVE THIS AND UNCOMMENT THE TWO BELOW
                alert('http://'+location.hostname+'/?com=inbox&action=deletesent&mid='+next+'#autoDeleting');
                location.replace('http://'+location.hostname+'/?com=inbox&viewsent='+next+'#autoDeleting');
            }
        }
        else {
            deleteNextMessage();
        }
    }
    else if(page.length) {
        location.replace(page);
    }
}

function deleteSelected() {

    var cnfm=confirm("Delete selected messages?&quot ;

    if(cnfm==true) {

        var list="";
        GM_setValue("selMsgs",list);

        var page=location.href;

        fdst=page.indexOf('&action=');

        if(fdst==-1)
            GM_setValue("lstPage",page);
        else
            GM_setValue("lstPage",page.slice(0,fdst));

        var msgs=document.getElementsByClassName("PM-delete-select&quot ;

        var cmpr = String.fromCharCode(9746);

        for(var i=0,lgth=msgs.length;i<lgth;i++) {

            if(msgs[i].innerHTML==cmpr) {

                var mgid=msgs[i].id.slice(4);
                list=list+mgid+',';
            }
        }
        if(list.length) {

            list=list.slice(0,-1); // remove end comma

            GM_setValue("selMsgs",list);

            deleteNextMessage()
        }
    }
    else {
    checkAll(false);
    }
}

function addButton(prnt,text,nwid,plce) {

    var dbtn=document.createElement("input&quot ;

    dbtn.type='button';
    dbtn.id=nwid;
    dbtn.style.margin='0.6154em 0.6154em 0.6154em 0em';
    dbtn.value=text;

    if(plce=='top')
        prnt.insertBefore(dbtn,lmnt.firstChild);
    else
    prnt.appendChild(dbtn);
}

// DELETE SELECTED EMAILS
if(location.hash=='#autoDeleting') {

    deleteNextMessage();
}

// CREATE CHECKBOXES ON PAGE
else if(location.search.indexOf('&view')==-1) {

    try {
        // REMOVE FOCUS FROM MEMBER SEARCH TEXT FIELD
        document.getElementById("search&quot .blur();
    }
    catch(err) {
    }
    try {
        // ADD COLUMN HEADING
        trow=document.getElementsByTagName("thead&quot [0].getElementsByTagName("tr&quot [0];

        tbhd=document.createElement("th&quot ;
        labl=document.createTextNode("Select&quot ;
        tbhd.appendChild(labl);

        trow.insertBefore(tbhd,trow.getElementsByTagName("th&quot [0]);

        // ADD CHECKBOXES
        trow=document.getElementsByTagName("tbody&quot [0].getElementsByTagName("tr&quot ;
        for(i=0,lgth=trow.length;i<lgth;i++) {
            inst=trow[i].insertCell(0);
            inst.className="PM-delete-select";
            inst.style.textAlign="center";
            inst.style.cursor="pointer";
            inst.innerHTML="&#9744;";

            titl=trow[i].getElementsByClassName("title&quot [0].getElementsByTagName("a&quot [0].outerHTML;
            snip=titl.slice(29);
            strt=snip.indexOf('=') + 30;
            last=snip.indexOf('"') + 29;
            idnm='del_'+titl.slice(strt,last);

            inst.id=idnm;
            inst.onclick=function(){selectToggle(this);};
        }
        // ADD BUTTONS
        lmnt=document.getElementsByClassName("default-table-container&quot [0];

        addButton(lmnt,"Select All","sel_all_btn","bottom&quot ;
        document.getElementById("sel_all_btn&quot .onclick=function(){checkAll(true);};

        addButton(lmnt,"Deselect All","sel_none_btn","bottom&quot ;
        document.getElementById("sel_none_btn&quot .onclick=function(){checkAll(false);};

        addButton(lmnt,"Delete Selected","delete_btn","bottom&quot ;
        document.getElementById("delete_btn&quot .onclick=function(){deleteSelected();};

        addButton(lmnt,"Delete Selected","delete_btn_top","top&quot ;
        document.getElementById("delete_btn_top&quot .onclick=function(){deleteSelected();};

        addButton(lmnt,"Deselect All","sel_none_btn_top","top&quot ;
        document.getElementById("sel_none_btn_top&quot .onclick=function(){checkAll(false);};

        addButton(lmnt,"Select All","sel_all_btn_top","top&quot ;
        document.getElementById("sel_all_btn_top&quot .onclick=function(){checkAll(true);};
    }
    catch(err) {
    }
}
Apparently the DU software strips out some javascript keywords so this didn't work and GreaseMonkey needs the added grant declarations to work right - updated to a working version.

Latest Discussions»Help & Search»DU Community Help»DU3 Mail multi-select del...