Advertisement
erinx

8chan Upvote

May 19th, 2015
3,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        8chan Upvote
  3. // @namespace   erin
  4. // @description Up and downvote posts on 8chan :^)
  5. // @include     *//8ch.net/*/res/*
  6. // @version     1
  7. // @grant       none
  8. // @run-at document-end
  9. // ==/UserScript==
  10.  
  11. function chanUpvote(){
  12.  
  13.   var d, db, h, namespace, $, $$, version;
  14.  
  15.   d = document;
  16.   db = document.body;
  17.   h = document.getElementsByTagName('head')[0];
  18.  
  19.   namespace = "8chanUpvote";
  20.   version = 0.01;
  21.  
  22.   if(window.location.href.indexOf('8ch.net') > 0){
  23.     var $siteDomain = '8ch.net';    
  24.   }else{
  25.     var $siteDomain = '8chan.co';
  26.   }
  27.  
  28.   $ = function(s, p){
  29.     if(!p || p == null){
  30.       p = db;
  31.     }
  32.     return p.querySelector(s);
  33.   };
  34.  
  35.   $$ = function(s, p){
  36.     if(!p || p == null){
  37.       p = db;
  38.     }
  39.     return p.querySelectorAll(s);
  40.   };
  41.  
  42.   $.getVal = function(k, v){
  43.     if(typeof(Storage) !=="undefined"){
  44.       if(v == null){
  45.         if(localStorage.getItem(namespace + "." + k) != null){
  46.           return localStorage.getItem(namespace  + "." + k);
  47.         }else{
  48.           return 'undefined';
  49.         }
  50.       }else{
  51.         if(localStorage.getItem(namespace + "." + k) != null){
  52.           return localStorage.getItem(namespace + "." + k);
  53.         }else{
  54.           return v;
  55.         }
  56.       }
  57.     }else{
  58.       return 'storage unavailable';
  59.     }
  60.   };
  61.  
  62.   $.setVal = function(k, v){
  63.     if(typeof(Storage) !=="undefined"){
  64.       if(v == null){
  65.         return 'undefined';
  66.       }else{
  67.         return localStorage.setItem(namespace  + "." + k, v);
  68.       }
  69.     }else{
  70.       return 'storage unavailable';
  71.     }
  72.   };
  73.  
  74.   $.delVal = function(k){
  75.     return localStorage.removeItem(namespace + "." + k);
  76.   };
  77.  
  78.   $.elm = function(t, a, s){
  79.     var e = d.createElement(t);
  80.     if(a){
  81.       for (key in a){
  82.         e.setAttribute(key, a[key]);
  83.       }
  84.     }
  85.     if(s){
  86.       s.appendChild(e);
  87.     }
  88.     return e;
  89.   };
  90.  
  91.   $.htm = function(s, v){
  92.     if(v == null){
  93.       return s.innerHTML;
  94.     }else{
  95.       s.innerHTML = v;
  96.     }
  97.     return s;
  98.   };
  99.  
  100.   $.each = function(a, c, e){
  101.     for(var i = 0; i < a.length; i++){
  102.       c(a[i], i);
  103.       if(i == (a.length)-1){
  104.         if(e && e != null){
  105.           return e(a[i], i);
  106.         }else{
  107.           return a;
  108.         }
  109.       }
  110.     }
  111.   };
  112.  
  113.   $.xhr = function(t, u, i, c, p){
  114.     if(i != null){
  115.       if(t == 'POST'){
  116.         var xd = new FormData();
  117.         for (key in i){
  118.           xd.append(key, i[key]);
  119.         }
  120.       }else{
  121.         xd = '?';
  122.         for (key in i){
  123.           xd += key + '=' + i[key] + '&';
  124.         }
  125.         xd = xd.substring(0, (xd.length-1));
  126.         u += xd;
  127.       }
  128.     }
  129.     var x = new XMLHttpRequest();
  130.     x.open(t, u);
  131.     if(p != null){
  132.       for (key in p){
  133.         x.setRequestHeader(key, p[key]);
  134.       }
  135.     }
  136.     x.onreadystatechange = function(){
  137.       if (x.readyState == 4) {
  138.         return c(x);
  139.       }
  140.     }
  141.     if(t == 'POST' && i != null){
  142.       x.send(xd);
  143.     }else{
  144.       x.send();
  145.     }
  146.   };
  147.  
  148.   $.JSON = function(s){
  149.     if(typeof s == 'string'){
  150.       return JSON.parse(s);
  151.     }else{
  152.       return JSON.stringify(s);
  153.     }
  154.   };
  155.  
  156.   $.time = function(t, c, l){
  157.     if(c == false){
  158.       return clearInterval(t);
  159.     }else{
  160.       if(l == true){
  161.         return setInterval(function(){
  162.           c();
  163.         },t);
  164.       }else{
  165.         return setTimeout(function(){
  166.           c();
  167.         },t);
  168.       }
  169.     }
  170.   };
  171.  
  172.   $.val = function(s, v){
  173.     if(!v || v == null){
  174.       return s.value;
  175.     }else{
  176.       s.value = v;
  177.     }
  178.     return s;
  179.   };
  180.  
  181.   $.event = function(t, i, s){
  182.     if (i == null) {
  183.       i = {};
  184.     }
  185.     if (s == null) {
  186.       s = d;
  187.     }
  188.     return s.dispatchEvent(new CustomEvent(t, i));
  189.   };
  190.  
  191.   $.att = function(s, a, v) {
  192.     if (!v || v == null || v == false) {
  193.       if (v == false) {
  194.         return s.removeAttribute(a);
  195.       } else {
  196.         return s.getAttribute(a);
  197.       }
  198.     } else {
  199.       s.setAttribute(a, v);
  200.       return s;
  201.     }
  202.   };
  203.  
  204.   $.destroy = function(s) {
  205.     return s.parentNode.removeChild(s);
  206.   };
  207.  
  208.   $.css = function(s){
  209.     var e = d.createElement('style');
  210.     e.type = 'text/css';
  211.     if (e.styleSheet){
  212.       e.styleSheet.cssText = s;
  213.     } else {
  214.       e.appendChild(d.createTextNode(s));
  215.     }
  216.     h.appendChild(e);
  217.     return e;
  218.   };
  219.  
  220.   $.onCursor = function(myField, myValue){
  221.     if (document.selection) {
  222.       myField.focus();
  223.       sel = document.selection.createRange();
  224.       sel.text = myValue;
  225.     }else if(myField.selectionStart || myField.selectionStart == '0'){
  226.       var startPos = myField.selectionStart;
  227.       var endPos = myField.selectionEnd;
  228.       myField.value = myField.value.substring(0, startPos)
  229.       + myValue
  230.       + myField.value.substring(endPos, myField.value.length);
  231.     }else{
  232.       myField.value += myValue;
  233.     }
  234.   };
  235.  
  236.   $.after = function(e, s){
  237.     s.parentNode.insertBefore(e, s.nextSibling);
  238.     return e;
  239.   };
  240.  
  241.   $.getSelected = function(){
  242.     var text = "";
  243.     if (window.getSelection) {
  244.       text = window.getSelection().toString();
  245.     } else if (document.selection && document.selection.type != "Control") {
  246.       text = document.selection.createRange().text;
  247.     }
  248.     return text;
  249.   };
  250.  
  251.   /*------------------------------------------*/
  252.  
  253.   var k, w;
  254.   var $siteDomain = '8ch.net';
  255.   w = window.location.href;
  256.   k = {};
  257.  
  258.   if($siteDomain == '8ch.net'){    
  259.     k.end = w.split('.net')[1];    
  260.   }else{
  261.     k.end = w.split('.co')[1];
  262.   }
  263.   k.proto = window.location.protocol;
  264.   if(k.end.indexOf('mod.php?') > 0){
  265.     if(k.end.length > 3 && w.split('mod.php?/')[1].indexOf('/') > 0 && w.indexOf('/src/') < 1){
  266.       k.board = w.split('mod.php?/')[1].split('/')[0];
  267.     }else{
  268.       k.board = null;
  269.     }
  270.     if(w.indexOf('/res/') > 1){
  271.       k.thread = w.split('/res/')[1].split('+')[0].split('.')[0];
  272.     }else if(w.indexOf('catalog') > 2){
  273.       k.thread = 'catalog';
  274.     }else{
  275.       k.thread = 'mod';
  276.     }
  277.   }else{
  278.     if(k.end.length > 3 && w.split($siteDomain+'/')[1].indexOf('/') > 0 && w.indexOf('/src/') < 1){
  279.       k.board = w.split($siteDomain+'/')[1].split('/')[0];
  280.     }else{
  281.       k.board = null;
  282.     }
  283.     if(w.indexOf('/res/') > 1){
  284.       k.thread = w.split('/res/')[1].split('+')[0].split('.h')[0];
  285.     }else if(w.indexOf('catalog') > 2){
  286.       k.thread = 'catalog';
  287.     }else{
  288.       k.thread = 'index';
  289.     }
  290.   }
  291.  
  292.  
  293. /*-------------------------------------------------------*/
  294.  
  295.  
  296.  
  297.  function makeVoters(){
  298.    
  299.    $.each($$('.reply'), function(post, pi){
  300.      if(!$('.voter', post)){
  301.        var $postId = $('.post_anchor', post).id;
  302.        var $el = {};
  303.        $el['class'] = 'voter';
  304.        $el = $.elm('span', $el, $('label', post));
  305.        $el.innerHTML = ' (<a style="cursor:pointer;" class="voteUp" id="voteUp-' + $postId + '">&#x25B2;</a> /<a style="cursor:pointer;" class="voteDown" id="voteDown-' + $postId + '">&#x25BC;</a> : <span id="votes-' + $postId + '">0</span> )';
  306.        
  307.        $('#voteUp-' + $postId).addEventListener('click', function(){ sendVote($postId, 'up'); }, false);
  308.        $('#voteDown-' + $postId).addEventListener('click', function(){ sendVote($postId, 'dwn'); }, false);
  309.        
  310.      }    
  311.    }, function(jk, kl){ getVotes(); });
  312.      
  313.  }
  314.  
  315.  
  316.  function getVotes(){    
  317.      var xhr = new XMLHttpRequest();
  318.      xhr.open("GET", k.proto + '//erin.wtf/api/vote/vote.php?method=get&thread=' + k.thread + '&board=' + k.board);
  319.      xhr.onreadystatechange = function(){
  320.         if(xhr.readyState == 4){
  321.           var $re = $.JSON(xhr.responseText);
  322.           for(key in $re){
  323.             console.log($re[key]);
  324.             if($('#votes-' + key)){
  325.               $('#votes-' + key).innerHTML = $re[key];
  326.             }
  327.           }          
  328.         }
  329.      }
  330.      xhr.send();        
  331.  }
  332.  
  333.  
  334.  function sendVote(po, vo){
  335.      var xhr = new XMLHttpRequest();
  336.      xhr.open("GET", k.proto + '//erin.wtf/api/vote/vote.php?method=vote&thread=' + k.thread + '&board=' + k.board + '&post=' + po + '&vote=' + vo);
  337.      xhr.onreadystatechange = function(){
  338.         if(xhr.readyState == 4){
  339.           getVotes();
  340.         }
  341.      }
  342.      xhr.send();    
  343.  }
  344.  
  345.  function build(){
  346.     makeVoters();  
  347.     getVotes();
  348.  }
  349.  
  350.   build();
  351.   $.time(3000, function(){ getVotes(); makeVoters(); }, true);
  352.   d.addEventListener('8chan-Z.NewPosts', function(){ build(); }, false);
  353.   d.addEventListener('threadUpdate', function(){ build(); }, false);
  354.   d.addEventListener('new_post', function(){ build(); }, false);
  355.  
  356.  
  357. }
  358. chanUpvote();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement