AmrElgendy AmrElgendy - 12 days ago 6
Javascript Question

Error in javascript code for facebook

I wrote some code to like all posts on Facebook.

var inputs = document.getElementsByClassName('_15ko _5a-2 touchable');
for(var i=0; i<inputs.length;i++) {
inputs[i].click();
};


The code is working well but the class
'_15ko _5a-2 touchable'
is the same class for (unlike)
So, When I scroll the page , More posts appear and trying to put the code again , The code working well with new posts but it remove likes from old posts.
Is there any solution?

Answer

You can try to check the innerHTML of the target, if the value is "Like" the click event is activated, if the value is "Dislike" the script ignore, example:

var inputs = document.getElementsByClassName('_15ko _5a-2 touchable');
for(var i=0; i<inputs.length;i++) {
    if (inputs[i].innerHTML == "Like") {
    inputs[i].click();
    }
    };

This will work if the button is something like:

<a class="_15ko _5a-2 touchable">Like</a>

To ignore the _2q8z class you can use the length of the className property, example:

//_15ko _5a-2 touchable = 21 (length)
var inputs = document.getElementsByClassName('_15ko _5a-2 touchable');
for(var i=0; i< inputs.length;i++) {
    if (inputs[i].className.length == 21) {
    inputs[i].click();
    }
    };

I will put a working example, hope it helps.

function Test() {
var elems2 = 0;
elems = document.getElementsByClassName("test1");
for (var x = 0; x < elems.length; x ++) {
elems2 ++;
}
alert(elems2 + " elements");
}

function Filter() {
var elems2 = 0;
elems = document.getElementsByClassName("test1");
for (var x = 0; x < elems.length; x ++) {
if (elems[x].className.length == 5) {
elems2 ++;
}  
}
alert(elems2 + " elements");
}
.test1 {
display: block;
padding: 10px;
background: #f1f1f1;
margin: 10px;
}
<div class="test1">Element</div>
<div class="test1">Element</div>
<div class="test1">Element</div>
<div class="test1 _2qz8">Element to ignore</div>
<div class="test1 _2qz8">Element to ignore</div>
<input type="button" value="Count All Elements" onClick="Test();">
<input type="button" value="Count only with class test1" onClick="Filter();">