User:Absconditus/cps.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.
/*cps (combat patrol scooter): author is http://lurk<!---->more.to/user:Хикки-задрот ; adaptation by: http://ru.wikipedia.org/wiki/user:Absconditus
Documentation is there -- [[user_talk:Absconditus/cpsjs]] */
//<source lang="javascript">
var cpsLoaderHtml = '<center><img src="http//upload.wikimedia.org/wikipedia/commons/d/de/Ajax-loader.gif"></img></center>';
var cpsBarHtml = '\
<div id="cpsControl">\
<div>\
<button id="cpsClear" onclick="cpsClear()" title="Clear comment field.">Clear</button>\
<span class="cpsNoDiff">\
<button id="cpsUp" onclick="cpsUp()" title="Previous diff link.">▲</button>\
<button id="cpsDown" onclick="cpsDown()" title="Next diff link.">▼</button>\
<button id="cpsLeft" onclick="cpsLeft()" title="Previous edit on this page.">◄</button>\
<button id="cpsRight" onclick="cpsRight()" title="Next edit on this page.">►</button>\
<button id="cpsKick" onclick="cpsKick()" title="Reload diff.">Reload</button>\
</span>\
<button id="cpsUndo" onclick="cpsUndo()" title="Revert all edits appeared after this one. Wait until the button turns green!">Revert</button>\
<button id="cpsBlock" onclick="cpsBlock()" class="cpsSysop" title="Link to this edit will be used in block reason. Wait until the button turns green!">Block</button>\
<button id="cpsClose" onclick="cpsHide()" style="align:right; float:right" title="Press this button to close cps window.">☒</button>\
</div>\
<input id="wpSummary" type="text" style="width:90%">\
<div id="userSummaryButtonsA"></div>\
<select id="mw-input-wpReason" class="cpsSysop"></select>\
<select id="mw-input-wpExpiry" class="cpsSysop"></select>\
</div>\
<div id="cpsHeader" class="cpsLoader cpsNoDiff"></div>\
<div id="cpsContent">\
<div id="cpsDiff" class="cpsLoader"></div>\
<div id="cpsList" class="cpsSysop cpsLoader">' + cpsLoaderHtml + '</div>\
</div>';
var cpsEmbedHtml = '\
<div id="cps">\
<div id="cpsEmbed">' + cpsBarHtml + '</div>\
</div>';
var cpsFrameHtml = '\
<div id="cps" style="display:none">\
<div id="cpsBackground" onclick="cpsHide()" title="HIDE"></div>\
<div id="cpsBar">' + cpsBarHtml + '</div>\
</div>';
var cpsHeaderHtml = '\
<p><a id="cpsTitle"></a> (<a id="cpsDiffLink" title="Diff">Diff</a> | <a id="cpsHistory" title="History">History</a> | <a id="cpsEdit" title="Edit">Edit</a>)</p>\
<p><i id="cpsTime"></i>: <a id="cpsUser" title="User page"></a> (<a id="cpsTalk" title="User talk">Talk</a> | <a id="cpsContribution" title="User contributions">Contribs</a> | <a id="cpsBlockLog" title="Block log">Blocks</a>)</p>\
<p><i id="cpsComment"></i></p>';

var cpsWorkspace = /diff=\w+|=edit|=submit|=history|:contributions|:recentchanges|:watchlist|:checkuser/i;
var cpsSummary = '';
var cpsLinks = [];
var cpsSysop = false;
var cpsToken = undefined;
var cpsHref = undefined;
var cpsTitle;
var cpsRevid;
var cpsPrev;
var cpsNext;
var cpsUser;
var cpsComment;
var cpsTime;
var cpsBlocked = true;
var cpsLink;

function cpsDisable(selector) { $(selector).attr('disabled', 'disabled'); }
function cpsEnable(selector) { $(selector).prop('disabled', false); }
function cpsSuccess(id) { return function (xml) { if ($('error', $(xml).text())[0]) { $(id).css('background-color', 'red'); cpsErrorInfo = xml; } else $(id).css('background-color', 'lime'); } }

function cpsParseHref(href, name) {
    if (href) {
        var value = href.match(RegExp(name + '=([^&]+)'));
        if (value) return value[1];
    }
    return undefined;
}

function cpsHideAll() { $('.cpsSummaryItem').toggle(); $('#cpsEmbed, .cpsOldLink, .cpsLink').toggle(); }
function cpsHide() { $('#column-one, .portlet').show(); $('#cps').fadeOut(); }
function cpsKick() { cpsQueryDiff(cpsHref); }
function cpsClear() { $('#wpSummary').attr('value', ''); $('.cpsSummaryItem').attr('class', 'cpsSummaryItem'); }
function cpsUp() { cpsLinks[cpsLink - 1].click(); }
function cpsDown() { cpsLinks[cpsLink + 1].click(); }
function cpsLeft() { cpsQueryDiff(cpsPrev); }
function cpsRight() { cpsQueryDiff(cpsNext); }
function cpsUndo() {
    cpsDisable('#cpsUndo');
    $.get('/w/api.php', { titles: cpsTitle, action: 'query', prop: 'info', format: 'xmlfm' }, function (xml) {
        $.post('/w/api.php', { action: 'edit', title: cpsTitle, token: cpsToken, summary: 'Undid revision ' + cpsRevid + ' via [[user talk:Absconditus/cps.js|cps.js]]: ' + $('#wpSummary').attr('value'), undo: cpsParseHref(cpsPrev, 'oldid'), undoafter: $('page', $(xml).text()).attr('lastrevid'), minor: '1' }, cpsSuccess('#cpsUndo'));
    });
}
function cpsBlock() {
    cpsDisable('#cpsBlock');
    $.post('/w/api.php', { action: 'block', user: cpsUser, token: cpsToken, expiry: $('#mw-input-wpExpiry option:selected').attr('value'), reason: $('#mw-input-wpReason option:selected').attr('value') + ($('#wpSummary').attr('value') ? ':&nbsp;' : '') + $('#wpSummary').attr('value'), nocreate: '1', autoblock: '1' }, cpsSuccess('#cpsBlock'));
}

function cpsQueryDiff(href) {
    if (href) {
        cpsHref = href;
        $('.cpsLoader').html(cpsLoaderHtml);
        $('#column-one, .portlet').hide();
        $('#cps').fadeIn();
        cpsDisable('#cps *:not(#cpsKick)');
        $.get(href + '&action=render&diffonly=1', function (xml) {
            $('#cpsHeader').html(cpsHeaderHtml);
            $('#cpsDiff').html($('.diff, .firstrevisionheader', $('<div>' + xml + '</div>')));
            cpsParseDiff();
        });
    }
}

function cpsParseDiff() {
    cpsTitle = decodeURIComponent(cpsParseHref($('#differences-nextlink, #differences-prevlink').attr('href'), 'title'));
    cpsRevid = cpsParseHref($('#mw-diff-ntitle1 a:first, #differences-nextlink').attr('href'), 'oldid');
    cpsPrev = $('#differences-prevlink').attr('href');
    cpsNext = $('#differences-nextlink').attr('href');
    cpsUser = $('.diff-ntitle .mw-userlink').text();
    cpsComment = $('.diff-ntitle .comment').html();
    cpsTime = $('#mw-diff-ntitle1 a:first')[0] ? $('#mw-diff-ntitle1 a:first').text().match(/(\d.+)/)[1] : '????';
    $('#cpsDiff .diff tr:first, #cpsDiff .firstrevisionheader').remove();
    if (cpsSysop) $.get('/wiki/Special:Block/' + cpsUser, function (xml) {
        if ($('#mw-input-wpReason-other', xml).attr('value').length == 0) cpsEnable('#cpsBlock');
        $('#mw-input-wpReason').replaceWith($('#mw-input-wpReason', xml));
        $('#mw-input-wpExpiry').replaceWith($('#mw-input-wpExpiry', xml));
        $('#cpsList').html($('#bodyContent ul', xml));
        $('#cpsList .mw-logevent-actionlink, #cpsList .mw-usertoollinks').remove();
    });
    cpsDisplay();
}

function cpsDisplay() {
    cpsEnable('#cps *:not(#cpsBlock)');
    if (cpsLinks[cpsLink]) cpsLinks[cpsLink].parent().addClass('cpsUsedLink');
    $('#cps button').css('background-color', '');
    cpsClear();
    if (!cpsPrev) cpsDisable('#cpsLeft, #cpsUndo');
    if (!cpsNext) cpsDisable('#cpsRight'); else $('#cpsUndo').css('background-color', 'yellow');
    if (!cpsLinks[cpsLink - 1]) cpsDisable('#cpsUp');
    if (!cpsLinks[cpsLink + 1]) cpsDisable('#cpsDown');
    $('#cpsTime').text(cpsTime);
    $('#cpsTitle').text(cpsTitle).attr('href', '/wiki/' + encodeURI(cpsTitle));
    $('#cpsDiffLink').attr('href', '/w/index.php?diff=' + cpsRevid);
	$('#cpsNewRev').attr('href', '/w/index.php?oldid=' + cpsRevid);
    $('#cpsHistory').attr('href', '/w/index.php?action=history&title=' + cpsTitle);
    $('#cpsEdit').attr('href', '/w/index.php?action=edit&title=' + cpsTitle);
    if (cpsUser) {
        $('#cpsUser').text(cpsUser).attr('href', encodeURI('/wiki/User:' + cpsUser));
        $('#cpsTalk').attr('href', encodeURI('/wiki/User_talk:' + cpsUser));
        $('#cpsContribution').attr('href', '/wiki/Special:Contributions/' + cpsUser);
		$('#cpsBlockLog').attr('href', '/w/index.php?title=special:log/block&page=User:' + cpsUser);
    }
    $('#cpsComment').html(cpsComment);
    $('#cpsContent').css('height', ($('#cpsBar').innerHeight() - $('#cpsHeader').outerHeight() - $('#cpsControl').outerHeight() - 25) + 'px');
}

function cpsStartup() {
    var diff = false;
    if (wgAction != 'edit' && wgAction != 'submit') {
        importStylesheetURI( '//en.wikipedia.org/wiki/user:Absconditus/cps.css?action=raw&ctype =text/css' );
        importStylesheetURI('//bits.wikimedia.org/ru.wikipedia.org/load.php?modules=mediawiki.action.history.diff&only=styles');
        if (/diff=\w+/.exec(location.href)) {
            diff = true;
            $(cpsEmbedHtml).insertBefore('.diff');
            $('.cpsNoDiff').remove();
        }
        else {
            function onclick(href, i) {
                return function () {
                    cpsLink = i;
                    cpsQueryDiff(href);
                }
            }
            $('#bodyContent').append(cpsFrameHtml);
            $('#bodyContent a[href*="diff="]:not(:contains("текущ."), :contains("проверить"))').each(function (i) {
                cpsLinks[i] = $('<a class="cpsLink">' + $(this).text() + '</a>').click(onclick(this.href, i)).appendTo($('<span></span>').insertAfter(this));
                $(this).addClass('cpsOldLink').hide();
            });
        }
    }
    cpsDisable('#cps *:not(#cpsKick)');
    if (wgAction == 'history') $('#cpsLeft, #cpsRight').remove();
    if (cpsSysop) $('#cpsReport').remove(); else { $('.cpsSysop').remove(); $('#cpsDiff').css('height', '100%'); };
    if (cpsSummary) $('#userSummaryButtonsA').prepend(cpsSummary.replace(/#(.*)@(.*)/g, '<a class="cpsSummaryItem" title="$2">$1</a>'));
    $('.cpsSummaryItem').click(function () {
        if (!$(this).hasClass('cpsSummaryItemChosen')) {
            var text = $('#wpSummary').attr('value');
            if (text.length > 0) text += ', ';
            $('#wpSummary').attr('value', text + this.title);
            $(this).addClass('cpsSummaryItemChosen');
        }
    });
    if (diff) cpsParseDiff();
}
if (cpsWorkspace.exec(decodeURIComponent(location.href))) {
    cpsSysop = (/sysop|moderator/).exec(wgUserGroups);
    /*
    XML format no longer supported. This code errors.
    $.get('/w/api.php', { titles: 'User:' + wgUserName + '/cps', action: 'query', prop: 'info|revisions', intoken: 'edit', rvprop: 'content', rvexpandtemplates: '1', rvlimit: '1', format: 'xmlfm' }, function (xml) {
        cpsToken = $('page', $(xml).text()).attr('edittoken');
        cpsSummary = $('rev', $(xml).text()).html();
        $('#p-tb ul').append('<li>cps: <a href="http//en.wikipedia.org/wiki/User:' + wgUserName + '/cps">Edit summary buttons</a> | <a onclick="cpsHideAll()" style="cursor:pointer">Hide</a></li>');
        cpsStartup();
    });*/
}
//</source>