Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

  • Updated 12-17-07: Added a refresh link to the XP Nodelet (click the @ to get a refreshed count of votes)
  • Updated 12-15-07: removed the need for javascript in a big string, fixed issues with voting booth and picked some better XPath's.
  • Updated 12-14-07: fix error with parent_node_id that caused it to be detected as a bot
  • Updated 12-13-07: use local XMLHttpRequest objects and removed extraneous code.

// // ==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

In reply to Re: Perlmonks AJAX Voting (greasemonkey plugin) by eric256
in thread Perlmonks AJAX Voting by eric256

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found