gavin19

Reddit - Highlight posts

Sep 10th, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          Reddit - Highlight posts on click
  3. // @namespace     null
  4. // @author        gavin19
  5. // @description   Add random background-colour to posts.
  6. // @match         http://reddit.com/*
  7. // @match         https://reddit.com/*
  8. // @match         http://*.reddit.com/*
  9. // @match         https://*.reddit.com/*
  10. // @include        http://reddit.com/*
  11. // @include        https://reddit.com/*
  12. // @include        http://*.reddit.com/*
  13. // @include        https://*.reddit.com/*
  14. // @version       1.00
  15. // ==/UserScript==
  16.  
  17. var loc = window.location.href;
  18. if (loc.match(/\/comments\//i)) {
  19. var authors = {};
  20. var colours = [
  21. "blue","turquoise","silver","navy","yellow","orangered"
  22. ];
  23. var x = document.querySelectorAll('div.md');
  24. for(var i=0,len=x.length;i<=len-1;i++){
  25.     x[i].addEventListener('click', function(){
  26.         var a = this.parentNode.parentNode.parentNode.querySelector('a.author').innerHTML;
  27.         if(authors.hasOwnProperty(a)){
  28.             for(var q=0,len=x.length;q<=len-1;q++){
  29.                 if(x[q].parentNode.parentNode.parentNode.querySelector('a.author').innerHTML === a){
  30.                     x[q].removeAttribute("style");
  31.                     delete authors[a];
  32.                 }
  33.             }
  34.         }
  35.         else {
  36.             authors[a] = colours[Math.floor(Math.random()*colours.length)];
  37.             this.setAttribute('style','background-color:'+authors[a]);
  38.             for(var d=0;d<=x.length-1;d++){
  39.                 if(x[d].parentNode.parentNode.parentNode.querySelector('a.author').innerHTML === a){
  40.                     x[d].setAttribute('style','background-color:'+authors[a]);
  41.                 }
  42.             }
  43.         }
  44.  
  45.     });
  46. }
  47. }
Add Comment
Please, Sign In to add comment