flyink13 / FlyVK

// ==UserScript==
// @name FlyVK
// @description Улучшения для ВКонтакте. Подробнее k-94.ru/FlyVK/
// @version     1.81
// @author flyink13

// @include     *://vk.com/*
// @include     *://*.vk.com/*
// @include     *://vk.me/*
// @include     *://*.vk.me/*
// @include     *://vk-cdn.net/*
// @include     *://*.vk-cdn.net/*
// @include     *://userapi.com/*
// @include     *://*.userapi.com/*
// @include     *://*.vk.cc/*
// @include     *://vkuseraudio.net/*
// @include     *://*.vkuseraudio.net/*

// @connect k-94.ru
// @grant GM_setValue
// @grant unsafeWindow
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_listValues

// @copyright 2016, flyink13 (Evgenii Kotliarov k-94.ru)

// @license MIT
// @icon http://k-94.ru/assets/logo.png
// @homepageURL https://k-94.ru/FlyVK/
// @updateURL https://openuserjs.org/meta/flyink13/FlyVK.meta.js
// ==/UserScript==
/* global FlyVK */

//*
// GM_adapter
function injectScript(code) {
    var script = document.createElement('script');
    script.appendChild(document.createTextNode(code));
    (document.body || document.head || document.documentElement).appendChild(script);
    script.outerHTML = '';
    delete script;
}

function GM_adapterWrap(GM_adapterId) {
    window.GM_adapterCallback = {};
    window.GM_adapter = GM_adapter;
    function GM_adapter(command, callback) {
        command.id = Math.random();
        command.aid = GM_adapterId;
        GM_adapterCallback[command.id] = function(response) {
            delete GM_adapterCallback[command.id];
            if (callback) callback(response);
        }
        window.postMessage (command, '*');
    }
}

function GM_adapterCallback(data, response) {
    response = JSON.stringify(response);
    var callback = 'GM_adapterCallback[' + data.id + ']';
    injectScript('(' + callback + ' && ' + callback + '(' + response + '));');
}

var GM_adapterId = Math.random();
injectScript('(' + GM_adapterWrap.toString() + ')(' + GM_adapterId + ')');

window.addEventListener('message', function(event) {
    var data = event.data || {};
    if (data.aid !== GM_adapterId) return;
    console.log(data);
    switch (data.type) {
        case 'GM_xmlhttpRequest':
            GM_xmlhttpRequest ({
                method: 'GET',
                url: data.url,
                onload: function (responseDetails) {
                    GM_adapterCallback(data, responseDetails.responseText);
                }
            });
            break;
        case 'loadScript':
            GM_xmlhttpRequest ({
                method: 'GET',
                url: data.url,
                onload: function (responseDetails) {
                    injectScript(responseDetails.responseText);
                    GM_adapterCallback(data);
                }
            });
            break;
    }
}, false);
// **********

function FlyVKWrap() {
    FlyVK.download = function (a, fn) {
        var link = document.createElement("a");
        link.href = a;

        var iframe = document.createElement("iframe");
        iframe.style.display = "none";
        iframe.onload = function () {
            setTimeout(function () {
                iframe.outerHTML = "";
            }, 15000);
        };
        iframe.src = location.protocol + "//" + link.host + "/#FlyDownload#" + encodeURIComponent(a) + "#" + encodeURIComponent(fn);
        document.body.appendChild(iframe);
    };

    FlyVK.other.addScript = function(url) {
        url = url.replace(/^\/\/k\-94\.ru/, 'https://k-94.ru');
        GM_adapter({
            type: 'loadScript',
            url: url,
        });
    }

    FlyVK.vloader = 1.81;
    FlyVK.settings.load();
};

if (location.hash.indexOf("FlyDownload") == 1) {
  var param = location.hash.split("#");

  var downloadLink = document.createElement("a");
  document.body.appendChild(downloadLink);
  downloadLink.href = decodeURIComponent(param[2]);
  downloadLink.setAttribute("download", decodeURIComponent(param[3]) + ".mp3");
  downloadLink.click();
} else {
    GM_xmlhttpRequest ({
        method: 'GET',
        url: 'https://k-94.ru/FlyVK/general.js?r=' + Math.random(),
        onload: function (responseDetails) {
            injectScript('(window.onFlyVK = ' + FlyVKWrap + ');');
            injectScript(responseDetails.responseText);
        }
    });
}

// */