derevnia / vkAudioSyncPlugin

// ==UserScript==
// @name vkAudioSyncPlugin
// @include https://vk.com/audios163991*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// @description    Часть плагина для автоматического скачивания, без основного скрипта не работает http://ggenikus-daily-shit.tumblr.com/post/19528066794/vkontakte
// @author ggenikus
// ==/UserScript==
var load, execute, loadAndExecute;
load = function (a, b, c) {
    var d;
    d = document.createElement("script"), d.setAttribute("src", a), b != null && d.addEventListener("load", b), c != null && d.addEventListener("error", c), document.body.appendChild(d);
    return d
}, execute = function (a) {
    var b, c;
    typeof a == "function" ? b = "(" + a + ")();" : b = a, c = document.createElement("script"), c.textContent = b, document.body.appendChild(c);
    return c
}, loadAndExecute = function (a, b) {
    return load(a, function () {
        return execute(b)
    })
};
loadAndExecute("//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", function () {
    (function (window, undefined) {
        var w;
        w = window;
        if (w.self != w.top) {
            return;
        }
        console.log("Starting alla...");
        checkIfAudioPage(w);
    })(window);

    function isOnAudioPage() {
        return (location == "https://vk.com/audios163991");
    }

    var pageChanged;

    function checkIfAudioPage(w) {
        setTimeout(function () {
            console.log("Checking url: " + w.location);

            if (isOnAudioPage()) {
                console.log("Alla in audio");
                main();
            }
            checkIfAudioPage(w);
        }, 10000);
    }

    var isLoadAll = false;

    function main() {
        addButtonToPage();
        setTimeout(function () {
            console.log("In main()");

            loadAllSongs();
            waitUntilLoadALl();
            if (!$("#audio_search input")
                .text() && location == "https://vk.com/audios") {
                sendRequestToAlla();
            }
        }, 3000);
    }

    function waitUntilLoadALl() {
        setTimeout(function () {
            if (!isLoadAll) {
                waitUntilLoadALl();
            } else {

                if (!$("#audio_search input")
                    .text() && location == "https://vk.com/audios163991") {
                    sendRequestToAlla();
                }
            }
        }, 2000)
    }

    function addButtonToPage() {
        // Check if exists
        if (!$('#alla')[0]) {
            $('#side_filters')
                .
            append("</br><div class='audio_filter selected'><div id='alla' class='label' >Сихронизация!</div></div>");
            $('#alla')
                .click(function () {
                    window.open("http://localhost:1106/");
                })
        }
    }

    function loadAllSongs() {
        setTimeout(function () {
            if ($("#more_link")
                .css('display') != 'none') {
                $("#more_link")
                    .trigger('click');
                loadAllSongs();
            } else {
                isLoadAll = true;
                $('#side_filters')
                    .
                append("</br><div class='audio_filter selected'><div id='alla' class='label' >Синхронизация завершена!</div></div>");
            }
            //        if ($('#footer_wrap')[0]) {
            //        }
        }, 100);
    }

    function getAllSongs() {
        var songs = [];
        $('.audio.fl_l .area ')
            .each(function () {
                var song = {
                    artist: $(this)
                        .find('.info .title_wrap b a')
                        .text() ||
                        $(this)
                        .find('.info .audio_title_wrap b a')
                        .text(),

                    title: $(this)
                        .find('.info .audio_title_wrap .title')
                        .text() ||
                        $(this)
                        .find('.info .title_wrap .title a')
                        .text() ||
                        $(this)
                        .find('.info .title_wrap .title')
                        .text(),

                    url: $(this)
                        .find('.play_btn input')
                        .attr('value')
                        .split(',')[0]
                };

                songs.push(JSON.stringify(song));


            });
        return songs
    }

    function sendRequestToAlla() {
        $.ajax({
            type: "POST"
            , url: "http://localhost:1106/songs/"
            , data: {
                songs: getAllSongs()
            }
            , statusCode: {}

        })
    }
});