Участник:Хикки-задрот/cpsuserjs

Материал из Lurkmore

Перейти к: навигация, поиск
//

var cpsLinks = []; var cpsFilters = []; var cpsShowHelp = false; var cpsSysop = false; var cpsFilterRegexp = undefined; var cpsHref = undefined; var cpsEditToken = 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).removeAttr('disabled'); } function cpsSuccess(id) { return function () { $(id).css('background-color', 'lime'); } } function cpsSummaryClear() { $('#wpSummary').attr('value', ); $('.cpsSummaryItem').attr('class', 'cpsSummaryItem'); } function cpsSummaryAdd(item) {

   if (!$(item).hasClass('cpsSummaryItemChosen')) {
       var text = $('#wpSummary').attr('value');
       if (text.length > 0) text += ', ';
       $('#wpSummary').attr('value', text + item.title);
       $(item).addClass('cpsSummaryItemChosen');
   }

} function cpsParseHref(href, name) {

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

}

function cpsWrite(data, summary, text, success) {

   data.action = 'edit';
   $.get('/index.php', data, function (xml) {
       var form = $('#editform', xml)[0];
       if (form) with (form) {
           $(form).hide().appendTo('#bodyContent');
           if (text) wpTextbox1.value = wpTextbox1.value.replace(/$/g, text);
           try { Wikify(); } catch(e) {}
           data = { title: data.title, action: 'submit', wpStarttime: wpStarttime.value, wpEdittime: wpEdittime.value, wpEditToken: wpEditToken.value, wpSummary: summary, wpTextbox1: wpTextbox1.value };
           if (wpWatchthis) if (wpWatchthis.checked) data.wpWatchthis = '1';
           if ($('#wpAnonymousedit:checked')[0]) data.wpAnonymousedit = '1';
           $(form).remove();
           $.post('/index.php', data, success);
       }
   });

}

function cpsToggle() { $('#cps, .cpsLink, .cpsOldLink').toggle(); } function cpsKick() { cpsQueryDiff(cpsHref); } function cpsUp() { cpsLinks[cpsLink - 1].click(); } function cpsDown() { cpsLinks[cpsLink + 1].click(); } function cpsLeft() { cpsQueryDiff(cpsPrev); } function cpsRight() { cpsQueryDiff(cpsNext); } function cpsUndo() {

   cpsDisable('#cpsUndo');
   cpsWrite({ title: cpsTitle, oldid: cpsParseHref(cpsPrev, 'oldid') }, $('#wpSummary').attr('value'), null, cpsSuccess('#cpsUndo'));

} function cpsReport() {

   cpsDisable('#cpsReport');
   cpsWrite({ title: 'Lurkmore:Ban/public' }, 'Auto', '*+ cpsRevid + ' ' + cpsTitle + ' — ' + $('#wpSummary').attr('value'), cpsSuccess('#cpsReport'));

} function cpsBlock() {

   cpsDisable('#cpsBlock');
   var data = { title: 'Special:Block', action: 'submit', wpBlockAddress: cpsUser, wpBlockExpiry: $('#cpsExpiry option:selected').attr('value'), wpBlockReasonList: $('#cpsReason option:selected').attr('value') + ': конкретно: вот эта правка', wpCreateAccount: '1', wpEnableAutoblock: '1', wpEditToken: cpsEditToken };
   if ($('#cpsReason option:selected').hasClass('cpsReason_Anon1'))
       data.wpAnonOnly = '1';
   $.post('/index.php', data, cpsSuccess('#cpsBlock'));

}

function cpsQueryDiff(href) {

   if (href) {
       cpsHref = href;
       cpsDisable('#cps *:not(#cpsKick)');
       $.get(href + '&action=render&diffonly=1', function (xml) {
$('#cpsDiff').html($('.diff, .firstrevisionheader', $('
' + xml + '
')));
           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();
   $('#cpsBlocklist').empty();
   if (cpsSysop) $.get('/Special:Block/' + cpsUser, function (xml) {
       cpsBlocked = $('#mw-bi-reason', xml).attr('value').length > 0 ? true : false;
       cpsEditToken = $('form', xml)[0].wpEditToken.value;
       $('#cpsBlocklist').append($('#bodyContent ul', xml));
       $('#cpsBlocklist .mw-logevent-actionlink, #cpsBlocklist .mw-usertoollinks').remove();
       cpsDisplay();
   }); else cpsDisplay();

}

function cpsDisplay() {

   cpsEnable('#cps *');
   if (cpsLinks[cpsLink]) cpsLinks[cpsLink].parent().css('background-color', 'lime');
   $('#cps button').css('background-color', );
   $('#cpsReason option:eq(0), #cpsExpiry option:eq(0)').attr('selected', 'selected');
   cpsSummaryClear();
   if (!cpsPrev) cpsDisable('#cpsLeft, #cpsUndo');
   if (!cpsNext) cpsDisable('#cpsRight'); else $('#cpsUndo').css('background-color', 'yellow');
   if (cpsBlocked) cpsDisable('#cpsBlock');
   $('#cpsTime').text(cpsTime);
   $('#cpsTitle').text(cpsTitle).attr('href', '/' + encodeURI(cpsTitle));
   $('#cpsDiffLink').attr('href', '/index.php?diff=' + cpsRevid);
   $('#cpsHistory').attr('href', '/index.php?title=' + cpsTitle + '&action=history');
   $('#cpsEdit').attr('href', '/index.php?title=' + cpsTitle + '&action=edit');
   $('#cpsUser').text(cpsUser).attr('href', '/Special:Contributions/' + cpsUser);
   $('#cpsComment').html(cpsComment);
   if (!cpsLinks[cpsLink - 1]) cpsDisable('#cpsUp');
   if (!cpsLinks[cpsLink + 1]) cpsDisable('#cpsDown');
   if (cpsFilterRegexp) $('.diff-addedline .diffchange').each(function () {
       $(this).html($(this).html().replace(cpsFilterRegexp, '$1'));
       for (var i in cpsFilters) if ($(this).html().search(cpsFilters[i].r) != -1) {
           cpsSummaryAdd($('#userSummaryButtonsA a:contains("' + cpsFilters[i].s + '")')[0]);
           $('#cpsReason option:eq(' + cpsFilters[i].b + ')').attr('selected', 'selected');
           $('#cpsExpiry .' + $('#cpsReason option:selected').attr('id')).attr('selected', 'selected');
       }
   });

}

function cpsTutorial() {

   function animate(id) {
       return function () {
           $('#' + id).animate({ 'opacity': 0.1 }, 1000).animate({ 'opacity': 1 }, 1000);
           setTimeout(animate(id), 2000);
       }
   }
   if (!cpsShowHelp && localStorage.cpsTutorial) {
       cpsShowHelp = true;
       $('#cps, .cpsOldLink').show();
       $('.cpsLink').hide();
       cpsEnable('#cps *');
       alert($('#cpsTutorial', localStorage.cpsTutorial).text());
       $('div', localStorage.cpsTutorial).each(function () {
           animate(this.id).call();
           $('#' + this.id).css('background-color', 'lime').removeAttr('onclick').click(function () {
               alert($('#' + this.id, localStorage.cpsTutorial).text()); 
           });
       });
   }

}

function cpsStartup() {

   var nextcall = null;
   var include = localStorage.cpsInclude.replace('diff', 'diff=\\w+').replace('edit', 'action=edit|action=submit').replace('history', 'action=history').replace('ban', 'Lurkmore:Ban').replace('cb', ':Contributions').replace('rc', ':RecentChanges').replace('wl', ':Watchlist');
   if (RegExp(include, 'i').exec(decodeURIComponent(location.href))) {
       if (wgAction != 'edit' && wgAction != 'submit') {
           $('#cps, .cpsLink').remove();
           $(localStorage.cpsFrame).insertAfter('#top');
           importStylesheetURI('http://lurkmore.ru/skins/common/diff.css');
           importScriptURI('http://lurkmore.ru/skins/common/wikificator.js');
           cpsDisable('#cps *:not(#cpsKick)');
           if (/diff=\w+/.exec(location.href)) {
               $('.cpsNoDiff').remove();
               nextcall = function () {
                   cpsHref = location.href;
                   cpsParseDiff();
               };
           }
           else {
               function onclick(href, i) {
                   return function () {
                       cpsLink = i;
                       cpsQueryDiff(href);
                   }
               }
               $('#bodyContent a[href*="diff="]:not(:contains("текущ."), :contains("проверить"))').each(function (i) { cpsLinks[i] = $('<a style="color:green;font-weight:bold;" href="#cps">' + $(this).text() + '</a>').click(onclick(this.href, i)).appendTo($('').insertAfter(this)); $(this).addClass('cpsOldLink').hide() });
           }
       }
   } else return;
   if (cpsSysop) $('#cpsReport').remove(); else $('.cpsSysop').remove();
   if (localStorage.cpsHide == 'true') $('#cps').hide();
   if (localStorage.cpsAnon == 'true') $('#wpAnonymousedit').attr('checked', 'checked');
   if (localStorage.cpsExpiry) $('#cpsExpiry').append('<option value="">другое</option>' + localStorage.cpsExpiry.replace(/#(.*)@(.*)/g, '<option value="$2">$1</option>'));
   if (localStorage.cpsSummary) {
       $('#userSummaryButtonsA').prepend('<a style="cursor:pointer" onclick="cpsSummaryClear()">очистить</a>' + localStorage.cpsSummary.replace(/#(.*)@(.*)/g, '<a class="cpsSummaryItem" style="cursor:pointer" title="$2">$1</a>'));
       $('.cpsSummaryItem').click(function () { cpsSummaryAdd(this); });
   }
   if (localStorage.cpsFilters) {
       var filters = localStorage.cpsFilters.match(/#.*@.*@\d*/g);
       if (filters) {
           cpsFilterRegexp = ;
           for (var i in filters) {
               var parts = filters[i].match(/#(.*)@(.*)@(\d*)/);
               if (parts) {
                   cpsFilterRegexp += (cpsFilterRegexp[0] ? '|' : ) + parts[1];
                   cpsFilters[i] = { r: RegExp('(' + parts[1] + ')', 'gi'), s: parts[2], b: parts[3] };
               }
           }
           cpsFilterRegexp = RegExp('(' + cpsFilterRegexp + ')', 'gi');
       }
   }
   if (localStorage.cpsReason) with ($('#cpsReason')) {
       empty();
       append('<option value="">другое</option>');
       var reasons = localStorage.cpsReason.match(/#.*@.*@\d/g);
       for (var i in reasons) {
           var parts = reasons[i].match(/#(.*)@(.*)@(\d)/);
           append('<option id="cpsReason_' + i + '" class="cpsReason_Anon' + parts[3] + '" value="' + parts[1] + '">' + parts[1] + '</option>');
           $('#cpsExpiry option:contains("' + parts[2] + '")').addClass('cpsReason_' + i);
       }
       change(function () { $('#cpsExpiry .' + $('#cpsReason option:selected').attr('id')).attr('selected', 'selected'); });
   }
   $('li[class*="mw-tag"]').css('background-color', 'yellow');
   if(nextcall) nextcall.call();

}

function cpsReload(nextcall) {

   $.get('/index.php', { action: 'raw', title: 'Участник:' + wgUserName + '/cps', templates: 'expand', ctype: 'text/javascript' }, function (xml) {
       var items = ;
       $('div', xml).each(function () { localStorage.setItem($(this).attr('class'), $(this).html()); items += $(this).attr('class') + ' '; });
       alert('Боевой Патрульный Самокат успешно загружен. Следующие элементы были определены: ' + items);
       localStorage.cpsLoaded = true;
       if(nextcall) nextcall.call();
   });

}

$('#p-tb ul').append('
  • <a id="cpsToggle" onclick="cpsToggle()" style="color:green;cursor:pointer">БПС</a> | <a id="cpsReload" onclick="cpsReload(cpsStartup)" style="color:green;cursor:pointer">Перезагрузка</a> | <a id="cpsTutorial" onclick="cpsTutorial()" style="color:green;cursor:pointer">Справка</a></li>'); cpsSysop = (/sysop|moderator/).exec(wgUserGroups); if (!localStorage.cpsLoaded) cpsReload(cpsStartup); else cpsStartup(); //</div>