User:DannyS712/FPC voter.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:DannyS712/FPC voter. |
// Install the newest version with:
// <code><nowiki> {{subst:Iusc|User:DannyS712/FPC voter.js}} </nowiki></code>
// or with
// <code><nowiki> importScript( 'User:DannyS712/FPC voter.js' ); // Backlink: [[User:DannyS712/FPC voter.js]] </nowiki></code>
//
// If forking this script, please note my contributions / give me credit
//<nowiki>
$(function (){
var FPC_config = {
name: '[[User:DannyS712/FPC voter|FPC voter]]',
version: 1.0,
debug: false
};
var FPC_summary = 'Vote with ' + FPC_config.name + ' (version ' + FPC_config.version + ')';
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
if (mw.config.get('wgTitle').includes('Featured picture candidates')) {
mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'FPC voter', 'ca-FPC-vote', 'vote');
$('#ca-FPC-vote').on('click', function() {
FPC();
} );
}
} );
} );
function FPC(){
var vote = prompt("Please enter your vote (not including \'''bolding\''')", "Support");
var reason = prompt("Please enter your rational (optional)", "");
var not_vote = "*'''" + vote + "''' – ";
if (reason !== null && reason !== "") {
not_vote = not_vote + reason + ' ';
}
not_vote = not_vote + '~~~~';
console.log(not_vote);
var page = mw.config.get( 'wgPageName' );
var content = get_page( page );
if (FPC_config.debug) console.log( content );
var comment = "<!-- additional votes go above this line -->";
var with_comment = not_vote + '\n' + comment;
var new_content = content.replace( comment, with_comment);
if (FPC_config.debug) console.log( new_content );
set_page( page, new_content );
}
function get_page( name ){
var page_to_get = {
action: 'query',
titles: name,
prop: 'revisions',
rvprop: 'content',
format: 'json',
formatversion: 2
};
var result = null;
$.ajax({
url: scriptUrl,
type: 'get',
data: page_to_get,
dataType: 'json',
async: false,
success: function(page) {
if (FPC_config.debug) console.log( page );
result = page.query.pages["0"].revisions["0"].content;
if (FPC_config.debug) console.log( result );
}
});
return result;
}
function set_page ( page, new_content){
console.log( page, new_content );
var to_send = {
action: 'edit',
title: page,
text: new_content,
summary: FPC_summary,
token: mw.user.tokens.get( 'csrfToken' )
};
console.log( to_send );
$.when(
$.post( scriptUrl, to_send, function( response ){ } )
).done( function() {
location.reload();
} );
}
});
//</nowiki>