Auto reconnect to Battlefield 4 server

    On the weekend you need to relax. It is a fact.
    But there is a wonderful phrase "Not a day without a line of code."
    So this weekend I decided to arrange a warm-up for the brain :-)

    I love the Battlefield series. Well, I like her!

    Battlelog for Battlefield 3 had wonderful server queues. Clicked and wait.
    What was my disappointment when I discovered that in the fourth part you have to sit and press the button to play with friends.

    The first thing that came to mind was a simple timer:

    setInterval(function(){var button=document.getElementById('ugm-reconnect'); if(button != null);},1000);

    Presses the Reconnect button once per second.

    But! Firstly, this is not correct, because it loads the browser and processor with “blank” clicks when we are already connected to the server.
    Secondly, what if DICE will track these clickers?

    Rummaging through the DICE code, and they have a fairly advanced front-end framework, I found "native" handlers there.
    Specifically: there is a launcher object with the registerForEvent () method.
    I watched how they themselves use it, and wrote this simple code:

    // Подписка на ошибки класса generic
    launcher.registerForEvent("error.generic", function(event, game, personaId, errorType, errorCode, errorString) {
            // Если это ошибка типа "Сервер полон"
            if(errorType == "ERR_SERVERCONNECT_SERVERFULL") {
                var button = jQuery("#ugm-reconnect");
    			if(button.length > 0){
    				// Нажать на кнопку реконнекта со случайной задержкой

    Well, in order not to write in the console every time, I made extensions for Chrome and Firefox .

    Also popular now: