Task automation with Javascript: autorecommend in LinkedIn

javascript
Tags:

For some time now, I’ve been worried about “my time”, so I’ve decided to free some daily time with a task automation using Javascript. And to exemplify this, nothing better than creating an autocommander for LinkedIn posts. What I explain here is quite technical, and is not recommended for newcomers, nor am I responsible for its use (good or bad). I am also not responsible for its modification or misuse, nor for its consequences.

But above all, what you should do, if you think what I say is useful, is to buy me a beer so I can continue to find inspiration for this blog.

The goal: click on every post on LinkedIn

The target is to reduce the time I spend on Social Networks, and in this case, I am using LinkedIn, and I want to click on every post to “recommend” it, all over my feed.

Initial requirements

There are two types of requirements: basic and advanced. The basic one is the debugging console of the browser, which you can get only by pressing F12, and the second is knowledge of Javascript so you know what it is doing at all times.

The code will create a bot variable:

const bot = {
    __app: 'LinkedInBot by @manejandodatos',
    __donate: 'https://www.paypal.com/donate?hosted_button_id=U3NVZKCYKTJJS',
    __version: '2.0.3',
    url_li: "www.linkedin.com",
}

Let’s start by including some needed functions.

Some important considerations when it comes to automation

If you have worked in automating things from the browser, or you have done some webscrapping, you will know that it is basic to set delays or pauses. This is achieved with 3 functions, and two properties:

  • Selecting a random rumber between two numbers.
  • Pause the execution of the code for some time.
  • Execute a personalized pause.

The two properties included are min and max time for the pause, in milliseconeds (I use between 1 and 6 seconds, that’s is: 1000 to 6000).

For the case of today’s entry, none of this will be necessary, but I’ll leave it here in case you need it.

    limit_delay_min: 1000,
    limit_delay: 6000,
    
    _delay: ms => new Promise(res => setTimeout(res, ms)),
    _getDelay: (min, max) => Math.random() * (max - min) + min,
    getDelay: () => bot._delay(bot._getDelay(bot.limit_delay_min, bot.limit_delay)),

It is important, and in fact it has been included, a running property that allows to know if the script is running or not. You can always execute bot.stop() to force the script to stop execution. This is this way I use to control the script.

running: false, 
stop: () => bot.running = false,

The _start function

A _start function has been created to execute several things: it verifies that the URL where we are is the correct one, as indicated in bot.url, and in case everything is correct, the running property will be changed to true, and information about the bot will be shown.

    _start: () => {
            console.warn(`${bot.__app} ${bot.__version}`);
            console.warn(`Donate me at: ${bot.__donate}`);
            const can_run = window.location.host === url;
            bot.running = can_run;
            if (!can_run) {
                console.error(`URL esperada incorrecta: ${url}`);
            }
    },
    __app: 'LinkedInBot by @manejandodatos',
    __version: '2.0.3',
    url: "www.linkedin.com",

The function that does the task: ar

It is the function that performs the work, and that sets everything in motion until the conditions for its completion are met. For it, several variables have been defined, to put limits or to make of counter, or the classes to select. This way everything is more orderly, and in case something fails, to have everything ready at the beginning of the function.

Then we define the _auto() function, which is a function that is executed every X time (the one we set), and that will consist of:

  • Select the buttons
  • For every button, verify if the text is what it is expected: “Recomendar”
  • In case the button fits the conditions:
  • It makes a random pause and click
  • The text is aggregated to the list of publications
  • The page scrolls down
  • It sums one to the counter and back to start

When the number of clicks reaches the target limit (limit_numclicks), the execution is concluded, the number of Likes that have been made is displayed, and the summary of the publications that have been “Recommended” by the autoresponder is shown..

The code for the ar() function is:

ar: limit_numclicks => { // AutoRecomendar
        let numero = 50;
        let numclicks = 0;
        let i = 0;
        const search_like = ".react-button__trigger";
        const ynmg = 'Recomendar';
        const rlp = 'Recomendar la publicación';
        const bloques = "feed-shared-update-v2";
        const url = "https://www.linkedin.com/feed/";
        const publicaciones = [];

        let _auto = setInterval(() => {
            x = document.querySelectorAll(search_like);
            x.forEach(async megusta_btn => {
                if (megusta_btn.innerText.indexOf(ynmg) >= 0) {
                    let reco = megusta_btn.getAttribute("aria-label");
                    if (reco.indexOf(rlp) >= 0 && !publicaciones.includes(reco) && reco.clientHeight == 48) {
                        await bot.getDelay();
                        megusta_btn.click();
                        numclicks++;
                        publicaciones.push(reco);
                    }
                    } 
                });
                window.scrollTo(0, window.innerHeight * i );
                i++;
            if(i >= numero || numclicks > limit_numclicks){
                clearInterval(_auto);
                alert(`He hecho ${numclicks} Likes!!`);
                console.log(publicaciones);
            }
    }, 3000);
},

Executing the Autorecomendator

Fot the execution, you must be in your feed timeline, and once you’re there, press on F12 for the console:

LinkedIn
LinkedIn

Let’s copy the bot in the console :

Preparando LinkedIn

Our target is to click on the “Recomendar” buttons, knowing the there is two tipes.

Botones de Recomendar
Botones de Recomendar

The one that includes the icon 👍 and the one that doesn’t include the icon (because it is a comment). The difference between one type and another is located in the clientHeight property, which in the case of the one we are interested in, its height is 48 and so we indicate it in the conditions. We have everything ready!

Let’s execute the script by writting bot.ar(10), in the consola, being 10 the number of expected clicks. And, that’s all!

Errors that you can get

The main error that you can get is 0 recomendations. Why? Because it is prepared for spanish version. Maybe you need to modify variables ynmg and rlp to adjust to your language (in the ar() function).

Versión 2, for task automation in LinkedIn

After several trial and error versions, the initial version is completely obsolete, and a new concept is proposed, which is the one I am currently using. It is to make X auto scrolls down, to load content, and once the number is reached, proceed to select the “Recommend” buttons to click, at the end, a message will tell you how many recommendations you have made.

As an additional restriction, I have thought it convenient not to give more than one click per person.

I have not published the code of this version 2, which is still in experimental phase, but undoubtedly, the progress obtained with its use has been multiple. If you are interested, write me an email or DM me at instagram.

Happy coding!