Went into the University for the first time

I went into the university for the first time today. It is less than a kilometer from the apartment. I had to go there to finalize certain documents regarding my accomodation. I arrived at the university during the lunch time and hence I had to wait for a couple of hours for the office staffs to return. During the time, I roamed around the campus. The place is aesthetically pleasing, however, it is pretty small, especially since where I did my bachelor's, IIT Patna, had a campus spread across over 500 acres. I also went to the nearby Lidl to get a few frozen pizzas. I had that for lunch, evening snack and dinner. I had thought about cooking something for dinner, but I felt a bit lazy to do the same. However, I did modify the last frozen pizza by adding some mushrooms, pizza sauce and pepper. I think this could be a good trick moving forward - buy a few frozen pizzas and chopped vegetables from Lidl and fuse them. I think this would make the pizzas a bit more healthier and the effor

Sending a Facebook Message Challenge: Harder than it looks

I have been having conversations with a friend regarding how easy it is to use JavaScript to automate a lot of processes that we usually do, why it is easier to program in an interpreted language than compiled language, etc. Pondering about these conversations, I challenged myself to send a message to my girlfriend on Facebook Messenger using JavaScript console alone.

Before I started, these are the ground rules that I set:

  1. I can use both keyboard and mouse, but the usage should be restricted to the JavaScript console alone.
  2. No other tab in the developer tools other than JavaScript console shall be used.
  3. I shall start with about:blank as the start page.

The first step is to open Messenger. That step was pretty easy considering the fact that I already knew how to use the window object to my advantage. I used the following command to open Messenger.

window.location.href = "http://www.messenger.com";

Luckily, I had already logged into Facebook and the cookies were not cleared. So I did not have to login again.

The next task is to click on the sidebar to activate my girlfriend's chat. I did not expect that this would be so difficult, but without being able to use Elements tab, it's almost impossible to get the handle to the button. I decided to make the console display all the children that document.body returned. But that would end up with me having too many children to look it. The same thing happened as I tried to make the console show all the elements having a tag name a.

After pondering around for a while, it stuck me: I can get all the HTML content of an element into a variable and do string manipulation to get the handle to the button to activate the chat. I decided to go with body and search for the nickname that I had set for my girlfriend.

splits = document.body.innerHTML.split('Seethootty');
lastFewChars = splits[0].slice(-600);

Honestly, I did not get the number -600 in the first attempt. It does not even have to be -600. I just needed to see the part of the string where it showed an id=*** in the code and I got id="js_9" in one of the parents. I observed that the next child to the div with aforementioned id is an element of type "a" (link). Therefore I decided to do the following to click on the link:

div = document.getElementById("js_9");
div = div.firstElementChild;

Now that her chat is up, the next step is to fill in a message and send it. I decided to follow the same procedure as last time to split the text.

divs = document.body.innerHTML.split("Type a message")
To my suprise, this gave me three elements in return, instead of two. I had to do the following for i=0 and i=1 and find the right div.

From the text I found the right div to be having id="placeholder-8eugf". From what I could see by looking around at the parents and siblings around that div, facebook is not using ordinarily used form input or textarea in the chat. In order to add content into the chat and get it sent just got a lot more challenging than I thought it would.

I made many different kinds of attempts. As a last resort, I tried to use a series of createEvent usages to trigger clicking on the message box and initiating key press events, but that did not work in my favour. Perhaps I must try to learn how Facebook has achieved in making something like this so that I could replicate the method in my own projects in the future.

If you were able to achieve what I was trying to do here, please do let me know how by commenting on this post.


Popular posts from this blog

Regarding a Covid-19 related project that I worked on a few months ago

Went into the University for the first time

Preparing my journey to Switzerland