Steps for triggering a click event on a div with a button role within a class containing multiple elements

Can anyone help me figure out how to auto-click every button in Instagram's "hide story from" settings using console? I tried the following code:

for (let i = 0; i < 300; i++) {
  document.getElementsByClassName('wbloks_1')[i]
        .addEventListener('click', function (event) {
        });
}
Unfortunately, this code isn't working. Does anyone have a solution? Here is the HTML element:

<div data-bloks-name="bk.components.Flexbox" class="wbloks_1" style="pointer-events: none; margin-right: 12px; flex-shrink: 0; align-items: center; flex-direction: row; justify-content: flex-end;"><div data-bloks-name="bk.components.Flexbox" class="wbloks_1" style="pointer-events: none;"><div data-bloks-name="bk.components.Flexbox" role="button" aria-label="Toggle checkbox" class="wbloks_1" style="pointer-events: none; display: none;"><div data-bloks-name="ig.components.Icon" class="wbloks_1" style="-webkit-mask-image: url(&quot;https://i.instagram.com/static/images/bloks/icons/generated/circle-check__filled__24-4x.png/219f67ac4c95.png&quot;); -webkit-mask-size: contain; background-color: rgb(0, 149, 246); flex-shrink: 0; width: 24px; height: 24px;"></div></div><div data-bloks-name="bk.components.Flexbox" role="button" aria-label="Toggle checkbox" class="wbloks_1" style="pointer-events: none; display: none;"><div data-bloks-name="ig.components.Icon" class="wbloks_1" style="-webkit-mask-image: url(&quot;https://i.instagram.com/static/images/bloks/icons/generated/circle-check__filled__24-4x.png/219f67ac4c95.png&quot;); -webkit-mask-size: contain; background-color: rgba(0, 149, 246, 0.3); flex-shrink: 0; width: 24px; height: 24px;"></div></div><div data-bloks-name="bk.components.Flexbox" role="button" aria-label="Toggle checkbox" class="wbloks_1" style="pointer-events: none;"><div data-bloks-name="ig.components.Icon" class="wbloks_1" style="-webkit-mask-image: url(&quot;https://i.instagram.com/static/images/bloks/icons/generated/circle__outline__24-4x.png/2f71074dce25.png&quot;); -webkit-mask-size: contain; background-color: rgb(54, 54, 54); flex-shrink: 0; width: 24px; height: 24px;"></div></div></div></div>

Answer №1

Place all the items into an array and remove the initial item. Click on it, trigger the function again until there are no more elements present. This is a simple queue implementation.

const executeQueue = () => {
  // gather all the buttons in an array
  const buttons = Array.from(document.querySelectorAll('.wbloks_1'));

  const proceed = () => {
    // retrieve the next button from the beginning of the array
    const button = buttons.shift();

    // click on it
    button.click()

    // if there are still elements remaining, re-execute after 6 seconds
    if (buttons.length) window.setTimeout(proceed, 6000);
  }
  
  // initiate the first iteration
  proceed();
}

executeQueue();
<input type="checkbox" class="wbloks_1">
<input type="checkbox" class="wbloks_1">
<input type="checkbox" class="wbloks_1">
<input type="checkbox" class="wbloks_1">
<input type="checkbox" class="wbloks_1">

Answer №2

For implementing a delay feature (mentioned in your previous comment), you can utilize setTimeout to trigger a function on all buttons through event delegation after a specified time period (measured in milliseconds).

In a simulated scenario, I've set up a button container that logs the text content of each clicked button.

The delayClick function takes an array of buttons (retrieved using query selection), clicks the first one, and then schedules another invocation with the remaining buttons via setTimeout after a second. This process continues until there are no more buttons in the array.

const container = document.querySelector('.container');
const buttons = document.querySelectorAll('.wbloks_1');

container.addEventListener('click', handleClick);

function handleClick(e) {
  if (e.target.matches('button')) {
    console.log(e.target.textContent);
  }
}

function delayClick(buttons) {
  if (!buttons.length) return;
  const [head, ...tail] = buttons;
  head.click();
  setTimeout(delayClick, 1000, tail);
}

delayClick(buttons);
<section class="container">
  <button class="wbloks_1">Button 1</button>
  <button class="wbloks_1">Button 2</button>
  <button class="wbloks_1">Button 3</button>
  <button class="wbloks_1">Button 4</button>
  <button class="wbloks_1">Button 5</button>
</section>

Additional resources

Answer №3

const widgets = document.querySelectorAll('.wbloks_1');
widgets.forEach(button => button.click());

Shout out to @Andy for the help! Customize Delay:

document.querySelectorAll('.wbloks_1').forEach((button, index) => {
  setTimeout(() => {
    button.click();
  }, index * 1000); // Adjust the delay time (in milliseconds) based on your needs
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Working with AngularJS: Implementing a Service in a Controller

A service has been developed in AngularJS, but it is not being utilized in the controller. Service.js var appService = angular.module("appService", []); appService.service("bddService", function() { var bdds = bdd; this.getBdds = function(){ ...

Implementing Browser Back or Back button in AngularJS

Currently, I am developing an application that utilizes route methods to navigate between webpages for different modules. Essentially, it is a single page application with route methods responsible for loading the HTML content in the body section. The iss ...

A compilation of category listings derived from two arrays of objects that share a common parent ID

I have a challenge with two arrays of objects connected by a parent ID. My goal is to create a categorized list where each category contains the corresponding data set. The structure should consist of a header (category) followed by buttons (data) related ...

Vue warning: Issue encountered in created hook - Attempting to access property 'get' of an undefined variable is causing a TypeError

I encountered an error while using axios: [Vue warn]: Error in created hook: "TypeError: Cannot read property 'get' of undefined" export default { methods: { loadUsers(){ axios.get("api/user").then(data => ...

Ways to adjust the brightness of any color when hovered over

Is it possible to create a universal effect where an element becomes darker or lighter when hovered over, regardless of the initial color? Here is an example: .change-color{ Background:green; width:100px; height:100px } .change-color:hover{ Background ...

The dynamic trio: Ajax, PHP, and MySql

Having some trouble with Ajax. Nothing seems to be happening when I change the select value. I've set up a div called textHint to display the result. This is my select setup: <form> <select id="choix" name="choix" onchang ...

Ways to style specific dates on a datepicker?

Is there a way to customize the appearance of the first and last selected days in my ui datepicker for booking, similar to the design shown in the image linked below? var dateFormat = "DD/MM/YY", from = $("#checkin,. ...

Default scrollbars in Chrome are designed to seamlessly blend in

Recently, I established a Wordpress website and am aiming to incorporate custom scroll bars in specific divs. Utilizing the WP jScrollPane plugin has been effective except when viewed on Chrome using a Windows platform. A peculiar issue arises where the d ...

Timeout feature for image slider in Angular JS

Hey there, I've been trying to get my AngularJS image slider to work like a slideshow where the images transition smoothly from slide to slide. I managed to code the functionality for navigating to the next and previous images, but when I attempted to ...

What are some ways to create a traditional HTML form submission and incorporate jQuery solely for the callbacks?

Given that my page consists solely of a simple "name and email" registration form, I see no reason why I shouldn't stick to the traditional approach: <form action="/Account/Register/" method="POST" id="registration-form"> <fields ...

Attempting to forward an image in a node-js/express application

I am facing an issue with a broken image link when trying to access it through Express: app.get('/fileThumbnail', function(req, res) { var url = proxiedURL +"?" + querystring.stringify(req.query); logger.info('/fileThumbnail going to url& ...

Return to the initial stage of a multistep process in its simplest form following a setTimeout delay

I recently customized the stepsForm.js by Copdrops and made some modifications. Although everything works well, I'm struggling to navigate back to the initial step (first question) after submitting the form due to my limited knowledge of JavaScript. ...

Disabling animations in Reactjs with CSSTransition and Group Transition

Currently, I am experimenting with REACTJS to build a basic app featuring Transitions. In my project file, I have imported CSSTransitions and Group Transition. However, when attempting to implement CSSTransition for specific news items, the animations are ...

When utilizing customize-cra to modify antd less variables, it results in the generation of numerous redundant CSS files during the build

While utilizing customize-cra to override antd less variable, I have encountered an issue where it generates multiple duplicate CSS files during the build process. According to the documentation provided by antd, if I opt for the default import of CSS by ...

in vuejs, a legendary row is added to the table every 25 or 50 records

VueJS v-for is functioning properly: <tr v-for="request in requests"> <td>{{request.name}}</td> <td> .. etc .. </td> </tr> Now, I want to insert a legend row after every 25 or 50 records. Here's what I tri ...

Looking for a solution to my issue - my for loop is looping more times than it should

I am designing a confirm dialog using jQuery and Bootstrap. When the user clicks on 'Yes' or 'No', it should trigger an action, such as drawing two squares each time. The first time I click either button, it draws 2 squares. However, wi ...

Angular is not properly integrating Bootstrap features

I've been attempting to create bootstrap accordion items, but unfortunately they're not functioning correctly as shown in the Bootstrap documentation or YouTube tutorials. In my angular.json file, I have included both bootstrap and jQuery for te ...

Repair the navigation bar once it reaches the top of the screen using ReactJS

I have a webpage that contains specific content followed by a bar with tabs. My goal is to have this bar stay fixed at the top of the screen once it reaches that position while scrolling down, and only allow the content below the fixed bar to continue scro ...

HTML and JavaScript - Facing issues rendering HTML content during the conversion process from Markdown format

In this particular scenario, my goal is to transform the content inside #fileDisplayArea into markdown format. However, I am encountering an issue where the HTML code within the div element is not being rendered. <div id="fileDisplayArea"># Title ...

utilize console.log within the <ErrorMessage> element

Typically, this is the way the <ErrorMessage> tag from Formik is utilized: <ErrorMessage name="email" render={(msg) => ( <Text style={styles.errorText}> ...