Tuesday, 26 November 2013

Working example for grasemonkey - show me only elements where post has >= 1k shares

Here is short example.
Few weeks ago I started learning javascript. This language is great it is very useful for websites where you want make little autobot or any part-site remover.
Here is an working example on real site, it is very easy.

// ==UserScript==
// @name        kwejk low rate remover
// @namespace  
// @include     http://kwejk.pl/*
// @version     1
// @grant       none
// ==/UserScript==

//window.onload = function(){
    //alert("loaded...");
    var elements = document.getElementsByClassName("mediaPair");
    for(var i=0;i<elements.length;i++)
    {
        //alert(i);
        var fbc = elements[i].getElementsByClassName("fb_share_ab")[0]
        .getElementsByClassName("facebook_share_size_Small")[0]
        .getElementsByClassName("facebook_share_count_inner")[0].innerHTML;
       
        //alert("["+fbc+"]" + " " +fbc.length);

            //alert(i + "/" + elements.length + " votes " + fbc + " isNumber? " + isNumber(fbc) + " " + (Number(fbc) < 1000));
            if(isNumber(fbc))
            {
                if(Number(fbc) < 1000)
                {
                    elements[i].outerHTML = "";
                    i--;
                }
            }
            //alert("stop");
    }
//}

function isNumber (o) {
  return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
}