2

I have this button And i want you to help me to click it with javascript code. Here is the class of the button.

Before click

 <a class="UFILikeLink" href="#" role="button" aria-label="Like this" aria-live="polite" data-ft="{"tn":">"}" data-reactid=".19l">

<i class="UFILikeLinkIcon img sp_nbjSKc2Bl8j sx_49c162" data-reactid=".19l.0"></i>

<span data-reactid=".19l.1">Like</span></a>

After click

<a class="UFILikeLink UFILinkBright" href="#" role="button" aria-label="Unlike this" aria-live="polite" data-ft="{"tn":">"}" data-reactid=".19l">

<i class="UFILikeLinkIcon img sp_nbjSKc2Bl8j sx_df3f80" data-reactid=".19l.0">

</i><span data-reactid=".19l.1">Like</span></a>

And here is the javascript code that im trying to make it work but i get error

var el = document.getElementsByClassName('UFILikeLink'); 
for (var i=0; i<el.length; i++) {
   var ele = el[i];
   if (ele.parentNode.className.indexOf('Unlike this') == -1) {
     ele.click();
   }
}

But when i try the code again it is pushing the already pushed buttons. Can you help me find the correct "if statement" so the code to see if any button is pushed, not to push it again?

Thanks for your time.

2
  • "Unlike this" is not defined on the element as a class. Its an aria-label Commented Sep 7, 2015 at 10:37
  • ele.parentNode.getAttribute("aria-label").indexOf("Unlike this") == -1 Commented Sep 7, 2015 at 10:39

1 Answer 1

3

Clicked Button has class UILinkBright. So, we can check existence of this class. If element has no this class, then we will trigger click.

var el = document.getElementsByClassName('UFILikeLink'); 
for (var i=0; i<el.length; i++) {
   var ele = el[i];
   if (!hasClass(ele, "UFILinkBright")) {
     ele.click();
   }
}


function hasClass(element, cls) {
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}

This is for aria-label attribute code:

var el = document.getElementsByClassName('UFILikeLink'); 
for (var i=0; i<el.length; i++) {
   var ele = el[i];
   if (ele.getAttribute('aria-label').indexOf('Unlike this') == -1) {
     ele.click();
   }
}

JSFiddel for aria-lable attribtue code.

Sign up to request clarification or add additional context in comments.

6 Comments

Whilst not making a difference to the result, checking the aria-label instead of the class is more readable to understand what the code is doing.
Yes of course. i didnt notice the "Bright" word ... i thought it had something to do with the "Unlike this" but was not quite sure. I can accept your answer in 8 minutes! Thanks again
@TRo your adjustment to my code gets an error. Uncaught TypeError: Cannot read property 'indexOf' of null at <anonymous>:5:49 at Object.InjectedScript._evaluateOn (<anonymous>:875:140) at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34) at Object.InjectedScript.evaluate (<anonymous>:664:21)
Can you please post what is hasClass statement? Any link that describes its work.
Try without the parent node, I only adjusted a part of your code. ele.getAttribute("aria-label").indexOf("Unlike this") == -1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.