WhatsApp Tricks Using JavaScript!!!

A friend once told me about using javascript which could pull the numbers of all the users in a WhatsApp group. Woah I thought! This for sure brought in a wave of excitement and I could not refrain myself from trying it out. So without further ado, I went ahead!

Scrolling through my WhatsApp list, I opened a group and went ahead to see all the members in it. Further, I inspected the code and noticed that the mobile number was in the URL for the users who had a profile picture.

BINGO! There I had a solution for my problem! But wait.. This couldn’t be that easy a task. So I scratched my brain to discover a few other scenarios. What about the people who weren’t in my contact list and hence couldn’t see their profile pictures? Their number appeared in the title section (where the name of a person is displayed).

Well, so for this I had to talk in the language which the computer understood and hence wrote this piece of code which lists out all the contact numbers of people in that group.

var chats = document.querySelectorAll(".contact.chat");
var contacts = [];
Array.prototype.forEach.call(chats, function(el) {
    var img = el.getElementsByTagName("img")[0];
    if (img !== undefined && img.src !== undefined) {
        if (img.src.indexOf("&u=") > 0 && img.src.indexOf("%40c") > 0) {
            contacts.push(img.src.split("u=")[1].split("%40c")[0]);
        } else {
            var title = el.getElementsByClassName("chat-title")[0];
            if (title !== undefined) {
                var number = title.innerText.replace("+", "").replace(/ /g, '');
                if (/^\d+$/.test(number)) {
                    contacts.push(title.innerText.replace("+", ""));
                } else {
                    console.log(title.innerText);
                }
            }
        }
    }

});
console.log(contacts);

This felt like an achievement!
I recalled the scene from the movie “The Social Network” where Mark Zuckerberg gets all the images of girls to create a facemash. I thought of giving it a try if I could get the profile pictures of all the users in the group. Well, this was fairly easy because all I had to do was to get all the images in the chatlist and download them. So I started working on it and created this:

function getAlImages() {
    var els = document.querySelectorAll(".drawer-section-body.chatlist img");
    var link = document.createElement("a");
    Array.prototype.forEach.call(els, function(el) {
        if (el.src !== undefined && el.src.indexOf("&u=") >= 0) {
            link.download = getParameterByName("u", decodeURIComponent(el.src));
            link.title = getParameterByName("u", decodeURIComponent(el.src));
            link.href = el.src;
            document.body.appendChild(link);
            link.click();
        }
    });
    document.body.removeChild(link);
    delete link;
}

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\<pre wp-pre-tag-1=""></pre>amp;amp;amp;amp;amp;amp;");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

This script did its job fairly well but had a little drawback. The images which were downloaded were actually thumbnails!
Alas! I couldn’t use them anywhere.

But as always, there is a solution for every problem which comes into the picture. And the solution for this one is as follows:
To download full-size images you can replace the “t=s” to “t=l” in the image URL. This can be achieved by using a simple replace function.

el.src = el.src.replace("?t=s", "?t=l")

Well if at all you are wondering the reason behind doing all this, the answer is just because I can 😉

Comments

  1. Lawwinia

    I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic info I was looking for this info for my mission.

Leave a Reply

Your email address will not be published. Required fields are marked *

17 − 4 =