26. Jul 2021 |

How to declare jQuery variables

var $allParas = $("p"); – When a variable starts with a $ sign, like in the following line of code, it’s a convention that developers use to indicate when a variable is storing a jQuery collection. Which means that you don’t have to do it, but you can if you’d like.

Many developers like to prefix the names of variables that store jQuery collection objects with a $, because it makes it obvious that they can call jQuery methods on them. It’s particularly useful if your code has a mix of variables that store jQuery collections and variables that store DOM elements.

Also acceptable to use var pic = $("#picture"); variable which stores a jQuery collection object. That collection does indeed contain a reference to the selected DOM element object, but it also contains many other properties and methods.

Source: Khan Academy

21. Nov 2020 |

Let the Spotify play your favourite songs by 30 seconds

Inspired by this article and the economic situation for musicians I wrote this little script. This lets Spotify to play your favourite artists on rotation and automatically presses “next” after every 33 (+ littlebit of random) seconds. 30 seconds is the minimum amount of time a song must be listened to before Spotify registers a single “play”.

Loading the song also takes little bit of time so you can not maximize the play count by making the loop take exactly 30 seconds long.

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min);
}

function doSomething() {
    var button = document.querySelector('[aria-label="Next"]');
    button.click();
}

(function loop() {
    var rand = getRandomInt(33000, 37000);
    console.log(rand);
    setTimeout(function() {
        doSomething();
        loop();
    }, rand);
}());

If you are using Spotify in your browser the press ctrl+shift+k or just F12, open console tab, paste this code there and press enter. This code does nothing but pressing the button with title attribute “Next” after every 33-37 seconds (random between these times).

Example of how it works in browser.

It is one option to support your favourite artists 🙂