MediaWiki:Gadget-VFHvote.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes.
- Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
- Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
- Opera, Konqueror and Safari users can just click the Reload button.
- Chrome: press Ctrl+F5 or Shift+F5
// JavaScript gadget to assist users in voting for articles on VFH
//<nowiki> Prevent three-tilde and four-tilde from being substituted when closing an edit
//
v={};
v.v=function() {
if(v.f.vote.selectedIndex===0||v.f.com.value==='') {
alert('Pick a vote first...');
return false;
}
v.co=v.f.com.value+' '+v.f.sig.value;
v.a=v.f.vote.selectedIndex;
var api = new mw.Api();
api.get ({
action: 'query',
indexpageids: 1,
prop: 'revisions|info',
rvlimit: 1,
rvprop: 'content',
titles: v.p
}).done(v.vc).fail(function() {
console.log("VFHvote: Failed to fetch voting page content");
});
v.b.innerHTML='Saving vote...';
return false;
};
// APPLY EDITS TO BALLOT PAGE
// Inserts both the vote/comment and, in the case of a vote, adjusts the for/against score.
// Assumes the parameters of {Votevfh} are present in an exact order; looks for the start of the
// next parameter to append vote/comment to the previous one.
v.vc=function(o) {
// Prefix for identifying gadget
var prefix = '[[MediaWiki:Gadget-VFHvote.js|←]]';
v.id = o.query.pageids[0];
v.r=o.query.pages[v.id].revisions[0]['*'];
switch(v.a)
{case 1:
v.r=v.r.replace(/\|againstnumber/g,'#'+v.co+' \n|againstnumber');
v.r=v.r.replace(/\|fornumber=.*/g,'|fornumber='
+(+v.r.match('fornumber=.*')[0].split('=')[1]+v.n));
v.sm=prefix + 'Voted for';
break;
case 2:
v.r=v.r.replace(/\|comments/g,'#'+v.co+' \n|comments');
v.r=v.r.replace(/\|againstnumber=.*/g,'|againstnumber='
+(+v.r.match('againstnumber=.*')[0].split('=')[1]+v.n));
v.sm=prefix + 'Voted against';
break;
case 3:
v.r=v.r.replace(/\n}}/g,'\n*'+v.co+'\n}}');
v.sm=prefix + 'Comment';
break;
default:
alert('Error reading vote page');
}
v.t = mw.user.tokens.get('csrfToken');
var p = 'title=' + encodeURIComponent(v.p)
+ '&token=' + encodeURIComponent(v.t)
+ '&summary='+ v.sm + '&text=' + encodeURIComponent(v.r);
var api = new mw.Api();
api.post ({
action: 'edit',
summary: v.sm,
text: v.r,
title: v.p,
token: mw.user.tokens.get('csrfToken')
}).done(v.sc).fail(function() {
console.log("VFHvote: Failed to edit voting page");
});
};
v.sc=function(o) {
if(v.q) {
v.b.innerHTML='<span>Vote saved! [<a href="/wiki/'+encodeURI(v.p)
+ '">go to nomination</a>] [<a href="javascript:void(0);" '
+ 'onclick="v.b.style.display=\'none\';">dismiss</a>]</span>';
} else {
var api = new mw.Api();
api.post ({
action: 'parse',
pst: 1,
text: '{{vt}}'+v.r+'{{vf}}'
}).done(v.u).fail(function() {
console.log("VFHvote: Failed to parse voting page");
});
}
};
v.u=function(o) {
document.getElementById('bodyContent').innerHTML=o.parse.text['*'];
};
// PREPARE A VOTE OR COMMENT in the correct format.
//
// Color changing
v.cv=function() {
var z=(v.c.indexOf('vtmp') > -1) ? true : false;
var a=(z) ? "{{" : "'''";
var b=(z) ? "}} " : "'''. ";
var c;
var d;
switch(v.f.vote.selectedIndex) {
case 1: c='For'; d='#ada'; break;
case 2: c='Against'; d='#fbb'; break;
case 3: c='Comment'; d='#ccf'; break;
}
v.f.parentNode.setAttribute("style",
v.f.parentNode.getAttribute("style") + "background-color: " + d + ";");
v.f.com.value=a+c+b;
v.f.com.focus();
v.f.submitvote.value=(v.f.vote.selectedIndex <= 2) ? 'Vote' : 'Comment';
};
v.op=function(o,k) {
if(o===undefined) {
var display = v.f.childNodes[11].style.display;
display = (display === 'none') ? 'block' : 'none';
}
else {
document.cookie='v'+o+'='+k+';path=/;expires=Fri, 1 Jan 2038 01:01:01 GMT;';
}
};
//https://css-tricks.com/snippets/javascript/get-url-variables/
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] === variable) {
return pair[1];
}
}
return(false);
}
// MAINLINE--Hooked below when Document Object Model becomes ready.
// Invoked either for a VFH ballot page or for an article containing {VFH} when
// clicked to from a ballot page (which adds ?vfhlink to the URL).
// Generates a floating DIV containing a form at the bottom of the page being browsed.
v.s=function() {
//https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
var isIE = /*@cc_on!@*/false || !!document.documentMode;
if(isIE||document.cookie.indexOf('vdis') > -1) {
return;
}
if(document.getElementById('isvfhpage')||document.getElementById('isvfhvotepage')) {
var l=document.getElementsByClassName('vfharticlelink');
for(var n = 0; n < l.length; n++) {
l[n].firstChild.href+='?vfhlink='
+ encodeURIComponent(l[n].title.replace(/\s/g,'_').replace(/VFH\//,''));
}
}
v.q=getQueryVariable('vfhlink');
if(document.getElementById('isvfhvotepage')||v.q) {
v.b = document.createElement('div');
// Uncyclopedians get 1 vote, Anon gets half a vote
v.n = (mw.config.get('wgUserName')) ? 1 : 0.5;
v.c = document.cookie;
var s = (v.c.indexOf('vsg') > -1) ? '~~~' : '~~~~';
v.b.innerHTML='<form name="vform" action=""'
+ ' onsubmit="return v.v();">Vote for this article!'
+ '<select name="vote" onchange="v.cv();" '
+ 'style="margin: auto 2px;vertical-align:0">'
+ '<option selected="selected" disabled="disabled" '
+ 'value="x">---vote---</option><option value="f">'
+ 'For</option><option value="a">Against</option>'
+ '<option value="c">Comment</option></select>'
+ '<input type="text" name="com"> <input type="text" '
+ 'disabled="disabled" name="sig" size="3" '
+ 'style="background: transparent;border:none" '
+ 'class="Unicode" value="'+s+'"> <input type="submit" '
+ 'name="submitvote" value="Vote"> '
+ '<span style="cursor:pointer;color:blue" '
+ 'title="options" onclick="v.op()">[?]</span> '
+ '<span style="cursor:pointer;color:blue" title="close" '
+ 'onclick="v.b.style.display=\'none\';">[x]</span>'
+ '<span style="display:none" ><label>'
+ '<input type="checkbox" name="ot" '
+ 'onchange="v.op(\'tmp\',this.checked);" value="t"> '
+ 'Use vote templates</label> <label>'
+ '<input type="checkbox" name="os" '
+ 'onchange="v.op(\'sg\',this.checked);'
+ 'v.f.sig.value=(this.checked) ? \'~~~\' : \'~~~~\';" value="s"> '
+ 'Use 3 tildes in sig</label> <label>'
+ '<input type="checkbox" name="od" '
+ 'onchange="v.op(\'dis\',this.checked)" value="d">'
+ 'Disable this thing</label></span> </form>';
v.b.style.cssText='display: inline-table; right: 0; bottom: 0; '
+ 'width: auto; height: auto; border: 1px solid black; '
+ 'background-color: #ccf; padding: 2px;';
v.b.style.position=(v.q) ? 'fixed' : 'relative';
v.b.style.transition = "ease-in 1s";
v.p=(v.q) ? 'Uncyclopedia:VFH/'+decodeURIComponent(v.q) : mw.config.get('wgPageName');
document.getElementById((v.q) ? 'content' : 'isvfhvotepage').appendChild(v.b);
v.f=document.forms.namedItem('vform');
v.f.ot.checked=(v.c.indexOf('vtmp') > -1) ? true : false;
v.f.os.checked=(v.c.indexOf('vsg') > -1) ? true : false;
}
};
$(document).ready(v.s);
//</nowiki>