Interacting with a Web Page using the Browser Console: A Simple Example

Interacting with a Web Page using the Browser Console: A Simple Example

Daily short news for you
  • A software that converts text to speech created by a Vietnamese programmer - J2TEAM - Text to Speech (Free). You can convert dozens of languages into dozens of different natural voices. The special thing is that it is free.

    In preliminary evaluation, the conversion of long texts or texts in pure Vietnamese is very good. However, when it includes English words, it sounds a bit funny 😅

    » Read more
  • How terrifying, Codeium - known as a competitor to Github Copilot, as it allows users to use it for free without limits. Recently, they introduced the Windsurf Editor - no longer just a VSCode Extension but a full Editor now - directly competing with Cursor. And the main point is that it... is completely free 🫣.

    » Read more
  • There is a rather interesting study that I came across: "Users never bother to read things they don't want to." (That's a bold statement, but it's more true than not. 😅)

    Don't believe it? I bet you've encountered situations where you've clicked on a button repeatedly and it doesn't respond, but in reality, it has displayed an error message somewhere. Or you've filled out everything and then when you hit the submit button, it doesn't go through. Frustrated, you scroll up or down to read and find out... oh, it turns out there's an extra step or two you need to take, right?

    It’s not far from the blog here. I thought that anyone who cares about the blog would click on the "Allow notifications" button just below the post. But the truth is, no one bothers to click it. Is it because they don't want to receive notifications? Probably not! I think it's because they just didn’t read that line.

    The evidence is that only when a notification pops up and takes up half the screen, or suddenly appears to grab attention, do they actually read it—and of course, it attracts a few more subscribers—something that was never achieved before.

    » Read more

The Problem

As you may know, before the emergence of React, Vue, etc., jQuery was the dominant force behind many websites. Almost every website used jQuery because it provided powerful DOM manipulation functions to change data. However, due to its bulkiness and complexity, along with the rise of modern web development tools, it is now on the "brink of death."

When accessing a web page, every action you take represents an interaction with the DOM. Besides users, there are other entities that can interact with the DOM, such as JavaScript. This means that you can use JavaScript to interact with the DOM just like a normal user would. From there, you can automate a series of repetitive actions without any user intervention.

Most modern browsers now provide a feature called the Console, which allows us to write and execute JavaScript code directly on the currently accessed web page. In today's article, I will present a concrete example of how to write JavaScript code to perform a repetitive action on a web page. Hopefully, through this article, readers can imagine more applications and apply them in their daily work.

DOM and JavaScript

Document Object Model (DOM) is a programming interface for web pages. It represents the web page as a tree-like structure of nodes and objects that programs can interact with to change the structure, style, and content of the document. DOM allows programming languages to interact with the page.

JavaScript can interact with the DOM in a powerful way. Think of the DOM as a Web API provided by the browser, granting us full access and control. Almost every great web experience today involves JavaScript. With the development of the web, we have more dynamic websites that focus on user experience. Many frameworks and libraries based on JavaScript enhance the interaction with the DOM and contribute to creating feature-rich websites.

Browsers allow us to run JavaScript code directly on a web page through their Dev Tools. In Chrome, after opening the Dev Tools and going to the Console tab, you can write JS code and execute it.

Browser Console

Taking advantage of this feature, many hackers used to trick users into pasting a piece of code to steal their data. But in this article, I won't delve into that topic. The purpose is to help readers understand how to use JavaScript to interact with the DOM.

It may sound lengthy, so let's take a simple example like "constantly reacting with emojis in Telegram messages" to help readers visualize how JavaScript can be used to interact with the DOM.

Constantly Reacting with Emojis in Telegram Messages

Most OTT messaging services now provide web versions for users to use. One feature I find interesting is the ability to react to messages using emojis to express emotions. I like it because sometimes I don't need to reply, I just need to drop an emoji. Or sometimes I react to let the sender know that I've read their message and express how I feel about it.

I have a habit of reacting to messages. Partly because I want them to know that I've read their message, partly because of my "playfulness." On Telegram, I have a group of friends who talk about various topics, and to make it less boring whenever my hands are "free," I wrote a script to automatically drop emojis into their messages.

To write the script, we first need to spend some time studying how the reaction feature works.

First, I notice that whenever I mouse over the time frame, emojis will appear, and when I click on an emoji, the reaction is successful. The same applies to other messages. Pay attention that the DOM of the emoji is always there, but it is hidden by CSS and only appears when we mouse over it.

How emoji reactions work

So the idea is to grab all the messages, loop through each one, and click on the emoji to perform the reaction.

First, let's create a function to get all the messages:

const getListMessage = () => {
  return (
    document.querySelectorAll(
      "div.message-content-wrapper.can-select-text > div"
    ) || []
  );
};

div.message-content-wrapper.can-select-text > div is the selector for each message.

Next, let's create a function to check if a message has already been reacted to by checking for the .Reactions element:

const checkReactMessage = (elm) => {
  if (elm) {
    return !!elm.querySelector(".Reactions");
  }
  return false;
};

Next, let's create a function to react to a message by clicking:

const react = (elm) => {
  if (elm) {
    elm.click();
  }
};

Finally, let's create the main function:

const main = () => {
  const listMessage = getListMessage();
  listMessage.forEach((elm) => {
    if (!checkReactMessage(elm)) {
      react(elm.querySelector('div.quick-reaction'));
    }
  });
};

After that, you can call the main function and see the results. Or if you want it to keep reacting to new messages, you can add the setInterval function.

setInterval(main, 3000);

The result is:

Result

Conclusion

DOM is a programming interface for web documents. JavaScript can easily interact with the DOM. Especially, being able to run JavaScript directly in the Console of a browser allows you to comfortably perform user actions automatically on their behalf.

Premium
Hello

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (1)

Leave a comment...
Avatar
Anh Đức Trừ10 months ago
bài viết của mình về Google Chrome Developer Tools bạn đọc thử nhé. https://www.blogchemgio.com/2022/06/google-chrome-developer-tools.html
Reply
Avatar
Xuân Hoài Tống10 months ago
Cảm ơn bạn, một bài viết khá chi tiết về developer tools 😄