javascript - Pulling comment scores from Reddit, greasemonkey script -
i'm looking write greasemonkey script, , need pull score each comment can add own stylizing after. here's have now:
the reddit source looks follows (this appears each comments)
<p class="tagline"> <span class="score unvoted">16 points</span>
and javascript i'm attempting write far, follows
var i, tags = document.queryselectorall('.tagline'); for(i=0;i<tags.length;i++) { var pullvotes = document.getelementsbyclassname('.score'); //gets object htmlcollection var collectionpull = array.prototype.slice.call(pullvotes); //my attempt convert htmlcollection array var uservote = collectionpull[0]; tags[i].innerhtml += "<span> ("+uservote+")</span>"; }
i "undefined". know there's reddit json use, couldn't find way pull score comments, static 1 set.
any appreciated, thanks!
first, went wrong:
var pullvotes = document.getelementsbyclassname('.score'); //gets object htmlcollection
yes, return htmlcollection object, line looks class named ".score" not class "score", line returns empty collection.
var collectionpull = array.prototype.slice.call(pullvotes); //my attempt convert htmlcollection array
i'm not sure you're trying achieve here, because htmlcollection have item(n)
method returns... yes, item @ index n
(as shown in documentation of htmlcollection class)
var uservote = collectionpull[0];
ok, if first line did work, assign first element of collection uservote
, meaning following line
tags[i].innerhtml += "<span> ("+uservote+")</span>";
would assign same value all scores on page.
fixed
sadly, don't know intended "(uservotes)" contain, don't think solve issues can information gave:
var i, tags = document.queryselectorall('.tagline'); for(i=0;i<tags.length;i++) { // elements class 'score' inside tagline var uservote = tags[i].getelementsbyclassname('score').item(0); // element uservote.style="background-color: red"; }
jquery
since reddit has jquery on page, can (for example)
var $ = unsafewindow.jquery; $('.tagline .score').css('background-color', 'red');
Comments
Post a Comment