MediaWiki:PinThreadList/code.js

var pinIcon = 'https://vignette.wikia.nocookie.net/mogapedia/images/5/5f/Pin.png/revision/latest?cb=20171102222925&path-prefix=fr'; var unpinIcon = 'https://vignette.wikia.nocookie.net/mogapedia/images/d/d3/Unpin.png/revision/latest?cb=20171102230647&path-prefix=fr'; function TestGroups { // Check if the user is a modo. Return True if yes. for (i=0; i<['bureaucrat', 'sysop', 'content-moderator'].length; i++) { if (mw.config.get('wgUserGroups').indexOf(['bureaucrat', 'sysop', 'content-moderator'][i]) > -1) { console.log('true'); return true; }       }    console.log('false'); return false; } $(function {    if (TestGroups) {        $('.thread').hover;        $('.thread-left h4').replaceWith(function{ return $(' ').append($(this).contents); });       $('.thread-left p:first-of-type').append('  ');        $('.pin').click(function  { var $parents = $(this.closest('.thread')); var id = $parents.attr('data-id'); pinThread(id); });   }    $.ajax({ url: 'http://fr.mogapedia.wikia.com/api.php', data: { 'format': 'json', 'action': 'query', 'titles': 'MediaWiki:Custom-PinThreadList', 'prop': 'revisions', 'rvprop': 'content' },       dataType: 'json', type: 'GET', cache: false, })   .done(function (data) { var threadsId = getThreads(data); if (threadsId) { addPinThreadList; for (i=0; i<threadsId.length;i++) { $('ul.PinThreadList').prepend($('li.thread[data-id='+threadsId[i]+']')); }           if (TestGroups) { $('.PinThreadList .pin img').attr('src', unpinIcon); $('.PinThreadList .pin').unbind("click"); $('.PinThreadList .pin').click(function {                    var $parents = $(this.closest('.thread'));                    var id = $parents.attr('data-id');                    unpinThread(id);                }); }       }    }); }); function getThreads (object) { var pageId; for (var key in object.query.pages) { pageId = key.toString; }   if (pageId != -1) { var text = object.query.pages[pageId].revisions[0]['*']; if (!text) { return null; }       return text.split("\n"); }   else { return null; } } function pinThread (id) { if ($('ul.PinThreadList').length === 0) { addPinThreadList; }   $('ul.PinThreadList br').before($('li.thread[data-id='+id+']')); if (TestGroups) { $('li.thread[data-id='+id+'] .pin img').attr('src', unpinIcon); $('li.thread[data-id='+id+'] .pin').unbind("click"); $('li.thread[data-id='+id+'] .pin').click(function {            var $parents = $(this.closest('.thread'));            var id = $parents.attr('data-id');            unpinThread(id);        }); }   editDatabase(id); } function unpinThread (id) { $('ul.ThreadList').prepend($('li.thread[data-id='+id+']')); $('ul.PinThreadList li.thread[data-id='+id+']').remove; if (TestGroups) { $('li.thread[data-id='+id+'] .pin img').attr('src', pinIcon); $('li.thread[data-id='+id+'] .pin').unbind("click"); $('li.thread[data-id='+id+'] .pin').click(function {            var $parents = $(this.closest('.thread'));            var id = $parents.attr('data-id');            pinThread(id);        }); }   $.ajax({        url: 'http://fr.mogapedia.wikia.com/api.php',        data: {            'format': 'json',            'action': 'query',            'titles': 'MediaWiki:Custom-PinThreadList',            'prop': 'revisions',            'rvprop': 'content'        },        dataType: 'json',        type: 'GET',        cache: false,    }) .done(function (data) {       var content = getThreads(data);        editDatabase(id, true, content.join('\n'));    }); } function addPinThreadList { $('div.ContentHeader').before(' '); $('.PinThreadList').before(' Messages épinglés '); } function editDatabase (id, unpin, content) { var text; if (unpin) { text = content.replace(id, ''); $.ajax({       url: 'http://fr.mogapedia.wikia.com/api.php',        data: {            'action': 'edit',            'title': 'MediaWiki:Custom-PinThreadList',            'text': text,            'token': mw.user.tokens.get('editToken')        },        dataType: 'json',        type: 'POST',        }); }    else { text = '\n'+id; $.ajax({       url: 'http://fr.mogapedia.wikia.com/api.php',        data: {            'action': 'edit',            'title': 'MediaWiki:Custom-PinThreadList',            'appendtext': text,            'token': mw.user.tokens.get('editToken')        },        dataType: 'json',        type: 'POST',        }); } }