User:Tomas79Sk/global.js

(function unvotePoll(window, $, mw) {

if (window.unvotePollLoaded) return;

window.unvotePollLoaded = true;

var url;

mw.loader.using(['mediawiki.util']).then(function {

var threadId = extractThreadId(mw.util.getParamValue('unvotePoll'));

url = mw.util.wikiScript('wikia');

if (threadId) unvote(threadId);

$('#WikiaBar ul.tools').append(

$('').append(

$('')

.attr('href', "#")

.text('PollUnvote')

.on('click', promptBox)

)

);

});

function unvote(threadId) {

$.getJSON(url, {

controller: 'DiscussionThread',

method: 'getThread',

threadId: threadId

}).error(function {

alert('(UnvotePoll) Error loading thread information');

}).then(function(data) {

var pollId = data.poll && data.poll.id;

if (!pollId) {

alert('(UnvotePoll) Failed to retrive poll data or thread is not a poll');

return;

}

$.post(url, {

controller: 'DiscussionPoll',

method: 'castVote',

pollId: pollId,

answerIds: ''

}).error(function {

alert('(UnvotePoll) Unvoting failed');

});

});

}

function promptBox {

var threadId = prompt('Insert link or thread ID of a poll to unvote:');

if (threadId) {

threadId = extractThreadId(threadId);

if (threadId) {

unvote(threadId);

} else {

alert('(UnvotePoll) Error: Invalid input');

}

}

}

function extractThreadId(str) {

var regex = new RegExp('^(?:' +

mw.config.get('wgServer').replace(/(https?:\/\/)/, '(?:https?://)?') +

'/f/p/)?(\\d+)(:?/r/\\d+.*)?$');

if (typeof str !== 'string' || !regex.test(str)) return false;

return str.replace(regex, '$1');

}

})(this, jQuery, mediaWiki);