3 ms·
This is great. I love how unapologetically "good enough" this code is—it does the absolute minimum needed to get the job done, with zero extra ceremony. A great
by lolinder 3y ago
This is great. I love how unapologetically "good enough" this code is—it does the absolute minimum needed to get the job done, with zero extra ceremony. A great example is the `vote` function:
function vote (id, how, auth, _goto) {
vis($('up_' + id), how == 'un');
vis($('down_' + id), how == 'un');
var unv = '';
if (how != 'un') {
unv = " | <a id='un_" + id + "' class='clicky' " +
"href='" + vurl(id, 'un', auth, _goto) + "'>" +
(how == 'up' ? 'unvote' : 'undown') + "</a>"
}
$('unv_' + id).innerHTML = unv;
new Image().src = vurl(id, how, auth, _goto);
}
Hide the up_ and down_ vote buttons, then either erase the innerHTML of the un_ vote div or construct new HTML for it from scratch. To top it all off, let's just inform the server of the vote by creating a new throwaway Image element, because it works everywhere and is less verbose than fetch.
- tetris11 3y agoAt first I thought "bleh, HN is powered with jQuery?" but actually no: $ and vis are one-liner functions defined above vote.
- twiss 3y ago> To top it all off, let's just inform the server of the vote by creating a new throwaway Image element, because it works everywhere and is less verbose than fetch. I don't think it's less verbose; fetch(vurl(...)) would work perfectly fine, and I think it's clearer what the intent is. fetch is also very widely supported by now. I don't blame anyone for writing it the way it was, back when fetch probably wasn't available, but I'd definitely just use that today.
- deleted 3y ago[deleted]
- enormousness 3y agoThis approach is also ripe for an XSS exploit.
- extraduder_ire 3y agoI think there's server-side checking to avoid that. Let us know if you have a PoC though.
- jokethrowaway 3y agoDo you mean CSRF? I don't see upvote links for some reason so I can't debug - but does auth contains a CSRF token? If not you could craft a page which upvote posts for the current visitor (if they're logged in HN) You could mitigate it with server side checks (or maybe some new browser tech I don't know about?) but I think the synchroniser token pattern is still the current solution.
- ash 3y ago> does auth contains a CSRF token? Yes, it looks like it does. Also, the vote auth param is different for every link.