2

I cannot find the correct element name for the "heart" that i would like to click via a javascript bookmark listed on http://ink361.com/#/tag/

when viewing page the elements are listed as 'likes' 'a.likes.does' after click and 'a.likes' prior click? - someone should help find the correct name so i can update the code.

I am trying to get this bookmarklet to click all the hearts on the page' I am unsure if the 'likes' is correct or if i should change it to something else, or if another part of the code is incorrect?

How do I change the code to get it to work?

javascript: e = document.getElementsByClassName('likes');
for (i = 0; i < e.length; i++) {
    e[i].click();
}
void(0);

If this will not work, basically i am trying to perform a javascript preferably as a bookmarklet to click the likes on the page.

When viewing the page i am unsure the correct class name so currently the javascript is not working for reasons unknown?

john doe
  • 21
  • 5

3 Answers3

2

Are you intending to use the click jQuery function? This won't work within a bookmarklet without explicitly creating a new script tag referencing jQuery..

As IanW commented on your question, you will need to create your own click function.

Community
  • 1
  • 1
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
  • “This won’t work within a bookmarklet” surprises me – [this post](http://coding.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/) and [Ben Alman’s jQuery Bookmarklet Generator](http://benalman.com/projects/run-jquery-code-bookmarklet/) rather suggest otherwise… – kopischke May 07 '12 at 19:10
  • Both of which create a reference to jQuery... Updated answer to be more concise though. – Rob Cooper May 07 '12 at 19:34
  • Never said that wasn’t required – but neither did your original answer. To quote the inimitable Chalky White, “We copacetic now” ;). – kopischke May 07 '12 at 19:46
  • It was assumed since the OP isn't referencing jQuery in their site. Thanks for "copacetic" never heard that word before :) – Rob Cooper May 07 '12 at 20:06
  • Ideally the code should be used as a bookmarklet or by performing a javascript action on the page. The problem with my code is I don't think I have the correct element name. and unsure how to find it, or what i need to perform a click all action of the "hearts on the page using javascript" – john doe May 08 '12 at 02:22
  • @johndoe from what I could see, if you get the 'click' working (changing it so it uses a callback to add the 'like'), then I see no reason why this shouldn't work. – Rob Cooper May 08 '12 at 05:07
  • Check out the code from @kopischke's comment. If you include jQuery, this whole thing becomes a lot easier :) – Rob Cooper May 09 '12 at 06:27
  • @johndoe: see [my comment on ocanal’s answer](http://stackoverflow.com/questions/10487268/javascript-bookmarklet-not-working/10487413#comment13592132_10487476). – kopischke May 09 '12 at 08:07
2

you can simulate a mouse click event like this,

function clickAll() {
   var clickEvt = document.createEvent("MouseEvents");
   clickEvt.initMouseEvent("click", true, true, window,
                         0, 0, 0, 0, 0, false, false, false, false, 0, null);

   var e = document.getElementsByClassName('likes');
   for(i=0;i<e.length;i++) { e[i].dispatchEvent(clickEvt); }
}
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • That would work however it currently doesn't work to click the elements but does it have potential to be used as a bookmarklet? Is 'likes' the correct class name? How do i find the correct class name? – john doe May 08 '12 at 02:09
  • This is ideal I simply want to click or trigger all the likes on the current page, by performing a javascript on the page. But currently this doesn't when i run it as a bookmarklet, or by using a DO Javascript app. – john doe May 08 '12 at 02:24
  • @johndoe: runs just fine on my console – have you tested it there? `likes` is indeed the correct class name (just inspect the page source when logged in – if you are using Safari, which I assume you do, as you speak of using `do javascript`, use the WebKit Inspector). As a side note, better use `document.querySelectorAll('.likes')` instead of `getElementsByClassName` – it’s much faster. – kopischke May 09 '12 at 08:02
-1

Your code look OK. The problem is that I can't even click on the hears with the mouse. They don't have a click eventlistener attached.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Actually, they do – if you connect the site to your Instagram account (kinda makes sense – who else is to “like” Instagram pictures than an Instagram user?). – kopischke May 07 '12 at 18:56
  • The problem with the code is i am unable to find the element name. I think it is 'likes' but i can't seem to get the code to function as a bookmarklet or by performing a 'DO javascript" command to the webpage. – john doe May 08 '12 at 02:12