User:Pyrospirit/rollbacksummary.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
This script is derived from the script at [[User:Voyagerfan5671/rollbacksummary.js]], with some 
slight enhancements.
*/

var RollbackBits = {
    Type: '',   // Defined by an onload function, below
                // @private: Technically, one should always use RollbackBits.GetType()
    CheckType: function () {
        if(document.getElementById('pagehistory')) { return 'hist'; }
        if(document.getElementById('mw-diff-ntitle2')) { return 'diff'; }
        if(wgCanonicalSpecialPageName && wgCanonicalSpecialPageName == 'Contributions') { return 'trib'; }
        else { return false; }
    },
    GetType: function () {
        if(this.Type == '') { this.Type = this.CheckType(); return this.Type; }
        else return this.Type;
    },
    PromptForSummary: function () {  // Designed to be called by the onclick of a rollback link
        var summary = prompt('Enter a summary for your rollback. Replacement strings: $rv, $u', '');
        var user = this.href.match(/[?&]from=([^&]*)/);
        var user = decodeURIComponent(user[1].replace("+", " "));
        if (summary) {
            while (summary.match(/\$u(ser)?|\$re?v(ert|v)? */i)) {
                summary = summary.replace(/\$u(ser)?/i, user);
                summary = summary.replace(/\$re?v(ert|v)? */i, 'Reverted edits by \[\[Special:Contributions\/' + user + '|' + user + '\]\] (\[\[user talk:' + user + '|talk\]\]). ');
            }
            this.href += '&summary=' + escape(summary).replace(/\+/,'%2B');
            return true;
        } else {
            return false;
        }
    }
};

function rollbackSummaryOnload() {
    var links = false;
    switch(RollbackBits.GetType()) {
        case 'hist': links = document.getElementById('pagehistory').getElementsByTagName('a'); break;
        case 'diff': links = document.getElementById('mw-diff-ntitle2').getElementsByTagName('a'); break;
        case 'trib': links = document.getElementById('bodyContent').getElementsByTagName('a'); break;
    }
    for(var linkid = links.length - 1; linkid >= 0; linkid--) { //append prompt links after rollback links
        if (links[linkid].href.match(/[?&]action=rollback[&]/)) {
            var rbsumm = links[linkid].cloneNode(false);
            rbsumm.onclick = RollbackBits.PromptForSummary;
            rbsumm.innerHTML = 'revert';
            switch(RollbackBits.GetType()) {
                case 'hist':
                    links[linkid].parentNode.appendChild(document.createTextNode(' | '));
                    links[linkid].parentNode.appendChild(rbsumm); break;
                case 'diff': case 'trib':
                    links[linkid].parentNode.appendChild(document.createTextNode(' ['));
                    links[linkid].parentNode.appendChild(rbsumm);
                    links[linkid].parentNode.appendChild(document.createTextNode('] '));
            }
        }
    }
}

$(rollbackSummaryOnload);