Search code examples
javascriptbookmarklet

How to click on an element of a website with javascript bookmarklet?


what i want to do is to click on this icons (there are like 16 per website, one by one, click the first, go to the next available, etc....

To understanding... Image 1

And sometimes it has this other eye icon: (it changes the code a bit) Image 2

I know we could do it from the console, but for comfort i would prefer to do this from the bookmarks (adding the code to a bookmark, then click it when i need it) So, what i know (thanks to @Sphinx) is that we have to write in a bookmarkletjavascript:$("span.icon.icon_eye").click() but the main problem is that when i click it, it clicks the first icon eye, and i need to click first the 1st eye icon, then the second, then the third, etc...


Solution

  • Based on your comments, below codes should be what you need.

    1. bind click event for all span.icon_eye.

    2. Click each <span class="icon icon_eys"> second by second (setTimeout).

    function clickSomething() {
      $('span.icon.icon_eye').on('click', function(){console.log('You clicked', $(this).html())});
      $('span.icon.icon_eye').each(function(index){setTimeout(()=>{$(this).click()}, index*1000);});
    }
    
    setTimeout(clickSomething, 1000)
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
    <span class="icon icon_eye">A</span>
    <span class="icon icon_eye">B</span>
    <span class="icon icon_eye">C</span>
    <span class="icon icon_eye">D</span>
    <span class="icon icon_eye">E</span>
    <span class="icon icon_eye">F</span>
    </div>