erinx icon

8chan Upvote

erinx | PRO | 05/19/15 06:18:39 PM UTC | 0 ⭐ | 3510 👁️ | Never ⏰ | []
JavaScript |

8.57 KB

|

None

|

0 👍

/

0 👎

// ==UserScript==
// @name        8chan Upvote
// @namespace   erin
// @description Up and downvote posts on 8chan :^)
// @include     *//8ch.net/*/res/*
// @version     1
// @grant       none
// @run-at document-end
// ==/UserScript==
 
function chanUpvote(){
  
  var d, db, h, namespace, $, $$, version;
 
  d = document;
  db = document.body;
  h = document.getElementsByTagName('head')[0];
 
  namespace = "8chanUpvote";
  version = 0.01;
  
  if(window.location.href.indexOf('8ch.net') > 0){
    var $siteDomain = '8ch.net';    
  }else{
    var $siteDomain = '8chan.co'; 
  }
 
  $ = function(s, p){
    if(!p || p == null){
      p = db;
    }
    return p.querySelector(s);
  };
 
  $$ = function(s, p){
    if(!p || p == null){
      p = db;
    }
    return p.querySelectorAll(s);
  };
 
  $.getVal = function(k, v){
    if(typeof(Storage) !=="undefined"){
      if(v == null){
        if(localStorage.getItem(namespace + "." + k) != null){
          return localStorage.getItem(namespace  + "." + k);
        }else{
          return 'undefined';
        }
      }else{
        if(localStorage.getItem(namespace + "." + k) != null){
          return localStorage.getItem(namespace + "." + k);
        }else{
          return v;
        }
      }
    }else{
      return 'storage unavailable';
    }
  };
 
  $.setVal = function(k, v){
    if(typeof(Storage) !=="undefined"){
      if(v == null){
        return 'undefined';
      }else{
        return localStorage.setItem(namespace  + "." + k, v);
      }
    }else{
      return 'storage unavailable';
    }
  };
 
  $.delVal = function(k){
    return localStorage.removeItem(namespace + "." + k);
  };
 
  $.elm = function(t, a, s){
    var e = d.createElement(t);
    if(a){
      for (key in a){
        e.setAttribute(key, a[key]);
      }
    }
    if(s){
      s.appendChild(e);
    }
    return e;
  };
 
  $.htm = function(s, v){
    if(v == null){
      return s.innerHTML;
    }else{
      s.innerHTML = v;
    }
    return s;
  };
 
  $.each = function(a, c, e){
    for(var i = 0; i < a.length; i++){
      c(a[i], i);
      if(i == (a.length)-1){
        if(e && e != null){
          return e(a[i], i);
        }else{
          return a;
        }
      }
    }
  };
 
  $.xhr = function(t, u, i, c, p){
    if(i != null){
      if(t == 'POST'){
        var xd = new FormData();
        for (key in i){
          xd.append(key, i[key]);
        }
      }else{
        xd = '?';
        for (key in i){
          xd += key + '=' + i[key] + '&';
        }
        xd = xd.substring(0, (xd.length-1));
        u += xd;
      }
    }
    var x = new XMLHttpRequest();
    x.open(t, u);
    if(p != null){
      for (key in p){
        x.setRequestHeader(key, p[key]);
      }
    }
    x.onreadystatechange = function(){
      if (x.readyState == 4) {
        return c(x);
      }
    }
    if(t == 'POST' && i != null){
      x.send(xd);
    }else{
      x.send();
    }
  };
 
  $.JSON = function(s){
    if(typeof s == 'string'){
      return JSON.parse(s);
    }else{
      return JSON.stringify(s);
    }
  };
 
  $.time = function(t, c, l){
    if(c == false){
      return clearInterval(t);
    }else{
      if(l == true){
        return setInterval(function(){
          c();
        },t);
      }else{
        return setTimeout(function(){
          c();
        },t);
      }
    }
  };
 
  $.val = function(s, v){
    if(!v || v == null){
      return s.value;
    }else{
      s.value = v;
    }
    return s;
  };
 
  $.event = function(t, i, s){
    if (i == null) {
      i = {};
    }
    if (s == null) {
      s = d;
    }
    return s.dispatchEvent(new CustomEvent(t, i));
  };
 
  $.att = function(s, a, v) {
    if (!v || v == null || v == false) {
      if (v == false) {
        return s.removeAttribute(a);
      } else {
        return s.getAttribute(a);
      }
    } else {
      s.setAttribute(a, v);
      return s;
    }
  };
 
  $.destroy = function(s) {
    return s.parentNode.removeChild(s);
  };
 
  $.css = function(s){
    var e = d.createElement('style');
    e.type = 'text/css';
    if (e.styleSheet){
      e.styleSheet.cssText = s;
    } else {
      e.appendChild(d.createTextNode(s));
    }
    h.appendChild(e);
    return e;
  };
 
  $.onCursor = function(myField, myValue){
    if (document.selection) {
      myField.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
    }else if(myField.selectionStart || myField.selectionStart == '0'){
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      myField.value = myField.value.substring(0, startPos)
      + myValue
      + myField.value.substring(endPos, myField.value.length);
    }else{
      myField.value += myValue;
    }
  };
 
  $.after = function(e, s){
    s.parentNode.insertBefore(e, s.nextSibling);
    return e;
  };
 
  $.getSelected = function(){
    var text = "";
    if (window.getSelection) {
      text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
      text = document.selection.createRange().text;
    }
    return text;
  };
 
  /*------------------------------------------*/
  
  var k, w;
  var $siteDomain = '8ch.net';
  w = window.location.href;
  k = {};
 
  if($siteDomain == '8ch.net'){    
    k.end = w.split('.net')[1];    
  }else{
    k.end = w.split('.co')[1];
  }
  k.proto = window.location.protocol;
  if(k.end.indexOf('mod.php?') > 0){
    if(k.end.length > 3 && w.split('mod.php?/')[1].indexOf('/') > 0 && w.indexOf('/src/') < 1){ 
      k.board = w.split('mod.php?/')[1].split('/')[0]; 
    }else{ 
      k.board = null; 
    }
    if(w.indexOf('/res/') > 1){ 
      k.thread = w.split('/res/')[1].split('+')[0].split('.')[0]; 
    }else if(w.indexOf('catalog') > 2){ 
      k.thread = 'catalog'; 
    }else{ 
      k.thread = 'mod'; 
    }
  }else{
    if(k.end.length > 3 && w.split($siteDomain+'/')[1].indexOf('/') > 0 && w.indexOf('/src/') < 1){ 
      k.board = w.split($siteDomain+'/')[1].split('/')[0]; 
    }else{ 
      k.board = null; 
    }
    if(w.indexOf('/res/') > 1){ 
      k.thread = w.split('/res/')[1].split('+')[0].split('.h')[0]; 
    }else if(w.indexOf('catalog') > 2){ 
      k.thread = 'catalog'; 
    }else{ 
      k.thread = 'index'; 
    }
  }
  
  
/*-------------------------------------------------------*/
  
 
  
 function makeVoters(){
   
   $.each($$('.reply'), function(post, pi){
     if(!$('.voter', post)){
       var $postId = $('.post_anchor', post).id;
       var $el = {};
       $el['class'] = 'voter';
       $el = $.elm('span', $el, $('label', post));
       $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> )';
       
       $('#voteUp-' + $postId).addEventListener('click', function(){ sendVote($postId, 'up'); }, false);
       $('#voteDown-' + $postId).addEventListener('click', function(){ sendVote($postId, 'dwn'); }, false);
       
     }    
   }, function(jk, kl){ getVotes(); });
      
 }
  
  
 function getVotes(){     
     var xhr = new XMLHttpRequest();
     xhr.open("GET", k.proto + '//erin.wtf/api/vote/vote.php?method=get&thread=' + k.thread + '&board=' + k.board);
     xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
          var $re = $.JSON(xhr.responseText);
          for(key in $re){ 
            console.log($re[key]);
            if($('#votes-' + key)){
              $('#votes-' + key).innerHTML = $re[key];
            }
          }          
        }
     }
     xhr.send();        
 }
  
  
 function sendVote(po, vo){
     var xhr = new XMLHttpRequest();
     xhr.open("GET", k.proto + '//erin.wtf/api/vote/vote.php?method=vote&thread=' + k.thread + '&board=' + k.board + '&post=' + po + '&vote=' + vo);
     xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
          getVotes();
        }
     }
     xhr.send();    
 }
  
 function build(){
    makeVoters();   
    getVotes();
 }
  
  build();
  $.time(3000, function(){ getVotes(); makeVoters(); }, true);
  d.addEventListener('8chan-Z.NewPosts', function(){ build(); }, false);
  d.addEventListener('threadUpdate', function(){ build(); }, false);
  d.addEventListener('new_post', function(){ build(); }, false);
  
  
}
chanUpvote();

Comments