http://www.perlmonks.org?node_id=656736


in reply to Perlmonks AJAX Voting

// // ==UserScript== // @name Perlmonks AJAX Vote // @namespace http://www.perlmonks.org/?node_id=398318 // @description Allow background voting via AJAX // @include http://www.perlmonks.* // @include http://perlmonks.* // @exclude http://www.perlmonks.*/?node=Fullpage%20Chat // @exclude http://perlmonks.*/?node=Fullpage%20Chat // ==/UserScript== function postRequest (url, params, onLoad) { var xmlHttpReq = new XMLHttpRequest(); xmlHttpReq.open('POST', url, true); xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-for +m-urlencoded'); xmlHttpReq.onreadystatechange = function() { if (xmlHttpReq.readyState == 4) { onLoad(xmlHttpReq.responseText); }; }; xmlHttpReq.send(params); } function _cast_vote(node_id, voteString, vc, vote) { document.getElementById('pm_'+voteString).innerHTML='<b>Casting Vote. +..</b>'; postRequest('/?', 'displaytype=xml&vc='+vc+'&op=vote&.cgifields='+vot +eString+'&'+voteString+'='+vote, function () { document.getElementById('pm_'+voteString).innerHTML = 'Retriev +ing Node Reputation...'; postRequest('/?', 'displaytype=xml&node_id=' + node_id, function (retHTML) { var parser=new DOMParser(); var doc=parser.parseFromString(retHTML,'text/xml'); var x=doc.getElementsByTagName('field'); for(i=0;i<x.length;i++) { var item=x.item(i); if (item.attributes.getNamedItem('name').value == +'reputation') { document.getElementById('pm_'+voteString).inne +rHTML = 'Reputation: ' + item.childNodes[0].nodeValue; } }; }); }); }; function getNode_byPath (path) { var temp = document.evaluate(path, document, null, XPathResult.FIR +ST_ORDERED_NODE_TYPE, null); if (temp) { return temp.singleNodeValue; } else { return null; } } function getNodes_byPath(path,context) { var nodes = document.evaluate(path,context, null, XPathResult.ORDE +RED_NODE_SNAPSHOT_TYPE, null); var ret = []; for (var i = 0; i < nodes.snapshotLength; i++) { ret.push(nodes.snapshotItem(i)); } return ret; } function remove_byPath (path, context) { var nodes = getNodes_byPath(path, context); for (i in nodes) { nodes[i].parentNode.removeChild(nodes[i]); } } function cast_vote (node_id, voteString, vc, vote) { return function () { _cast_vote(node_id, voteString, vc, vote); }; } function _refresh_votes () { postRequest('/?', 'node_id=16046', function (retHTML) { var parser=new DOMParser(); var doc=parser.parseFromString(retHTML,'text/xml'); var x=doc.getElementsByTagName('XP'); for(i=0;i<x.length;i++) { var item=x.item(i); var xpNodelet = getNode_byPath("//tr[@id='nodelet_body +_row_XP_Nodelet']/td"); if (xpNodelet) xpNodelet.innerHTML = "You have " + item.attribut +es.getNamedItem('votesleft').value + ' votes left today.'; }; }); } function refresh_votes () { return function () { _refresh_votes() } } var xpNodelet = getNode_byPath("//tr[@id='nodelet_head_row_XP_Nodelet' +]/th"); if (xpNodelet) var refresh = document.createElement('a'); refresh.setAttribute("href",'javascript:void(0);'); refresh.removeAttribute("onclick"); refresh.addEventListener("click", refresh_votes(), true); refresh.innerHTML = " @"; xpNodelet.appendChild(refresh); } var inputNode = getNode_byPath("//input[@name='op' and @value='vote']" +); if (inputNode) { var formNode = inputNode.parentNode; var vc; var inputVC = getNode_byPath("//input[@name='vc']", formNode); if (inputVC) { vc = inputVC.value; } var votinginputs = getNodes_byPath("//label/input[@type='radio']", + formNode); for (var i in votinginputs) { var thisInput = votinginputs[i]; if (thisInput.value == 0) { var voteString = thisInput.name; var node_id = voteString.substring(6); var voting_both = document.createElement("div"); voting_both.class = 'reputation'; voting_both.id = "pm_" + voteString; var up_vote = document.createElement('a'); up_vote.setAttribute("href",'javascript:void(0);'); up_vote.removeAttribute("onclick"); up_vote.addEventListener("click", cast_vote(node_id, voteS +tring, vc, 1), true); up_vote.innerHTML = "++"; voting_both.appendChild(up_vote); var spacer = document.createElement("span"); spacer.innerHTML = "&nbsp;"; voting_both.appendChild(spacer); var down_vote = document.createElement('a'); down_vote.setAttribute("href",'javascript:void(0);'); down_vote.removeAttribute("onclick"); down_vote.addEventListener("click", cast_vote(node_id, vot +eString, vc, -1), true); down_vote.innerHTML = "--"; voting_both.appendChild(down_vote); thisInput.parentNode.insertBefore(voting_both, thisInput); } thisInput.parentNode.removeChild(thisInput); } // remove the original voting labels remove_byPath("//div[@class='vote']//tt", formNode); // delete the submit button remove_byPath("//input[@name='sexisgreat']", document); }

___________
Eric Hodges