Greasy Fork

Greasy Fork is available in English.

Local Vk Downloader

Get Vk raw link without external service.

当前为 2020-04-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Local Vk Downloader
// @namespace    vkDownloadAuto
// @version      1.1.0
// @description  Get Vk raw link without external service.
// @match        https://m.vk.com/mail*
// ==/UserScript==
var peer = null;
var fetchedVideosMap = {};
var fetchedVideos = [];
var fetchStack = [];
var fetchNeeded = false;

var data = {
    videos: [],
    photos: [],
    docs: []
}
var finishState = {
    videos: false,
    photos: false,
    docs: false
}

function vkPlainGet(url, callback) {
    return ajax.plainpost(
        url,
        { _ajax: 1 },
        (function (e) {
            callback(e);
        }),
        (function () {
            callback(null)
        }),
        !0
    );
}

function removeImages(documentString) {
    return documentString.replace("<img", "<div").replace("</img>", "</div>");
}

function parseDocumentToHtml(documentString) {
    var htmlElement = document.createElement('html');
    if (documentString.indexOf("<body") != -1) {
        documentString = documentString.substring(documentString.indexOf("<body"), documentString.indexOf("</body>") + 7);
    }
    htmlElement.innerHTML = documentString;
    return htmlElement;
}

function isFetched(peer, href) {
    return fetchedVideosMap[peer][href];
}

function storeFetchedVideo(peer, videoItem) {
    if (isFetched(peer, videoItem.href)) {
        return;
    }
    fetchedVideosMap[peer][videoItem.href] = videoItem;
    fetchedVideos.push(videoItem);
}

function fetchVideo(peer, videoItem, callback) {
    vkPlainGet(videoItem.href, function (result) {
        videoItem.sources = getSources(result);
        storeFetchedVideo(peer, videoItem);
        callback();
    });
}

function checkFetchStack() {
    if (!fetchStack.length) {
        finishSegment("videos");
        return;
    }
    while (fetchStack.length) {
        var fetchItem = fetchStack.shift();
        var videoItem = fetchItem.item;
        if (isFetched(fetchItem.peer, videoItem.href)) {
            continue;
        }
        var callback = function () { };
        if (fetchStack.length == 0) {
            callback = function () {
                finishSegment("videos");
            }
        }
        fetchVideo(fetchItem.peer, videoItem, callback);
        return;
    }
}

function listenFetchNeeded() {
    window.setInterval(function () {
        if (!fetchNeeded) {
            return;
        }
        checkFetchStack();
    }, 500);
}

function getSources(htmlString) {
    var htmlElement = parseDocumentToHtml(removeImages(htmlString));
    var sources = htmlElement.querySelectorAll("source[type='video/mp4']");
    if (!sources || !sources.length) {
        recursiveHandleVideoItem(items, ++idx, length, videos, callback);
        return;
    }
    var sourcesLinks = [];
    for (var i = 0; i < sources.length; ++i) {
        sourcesLinks.push(sources[i].src);
    }
    return sourcesLinks;
}

function getPeer() {
    var hash = window.location.search;
    var idx = hash.indexOf("peer");
    if (idx == -1) {
        return null;
    }
    var end = hash.indexOf("&", idx);
    if (end == -1) {
        end = hash.length;
    }
    return hash.substring(idx + 5, end);
}

function putVideoItemToFetchStack(itemData, peer) {
    fetchStack.push({ item: itemData, peer: peer });
}

function getVideos(peer) {
    var items = document.querySelectorAll(".video_item");
    if (!items || !items.length) {
        return;
    }
    for (var i = 0; i < items.length; ++i) {
        var item = items[i];
        var itemData = {};

        var videoTitle = item.querySelector(".vi_title_text");
        if (videoTitle) {
            itemData.title = videoTitle.innerText;
        }
        if (itemData.title.indexOf("Без названия") == -1) {
            continue;
        }

        var videoHref = item.querySelector(".video_href");
        if (!videoHref) {
            continue;
        }
        itemData.href = videoHref.href;

        var previewImg = item.querySelector("img");
        if (previewImg) {
            itemData.preview = previewImg.src;
        }

        var videoLength = item.querySelector(".thumb_label");
        if (videoLength) {
            itemData.length = videoLength.innerText;
        }

        putVideoItemToFetchStack(itemData, peer);

    }
}

function getPhotos() {
    finishSegment("photos");
}

function getDocs() {
    finishSegment("docs");
}

function getAllMedia(peer) {
    console.log("\n\n***********************************ALL_MEDIA************************************")
    data = {
        videos: [],
        photos: [],
        docs: []
    }
    finishState = {
        videos: false,
        photos: false,
        docs: false
    }
    fetchNeeded = true;
    if (!fetchedVideosMap[peer]) {
        fetchedVideosMap[peer] = {};
    }
    getVideos(peer);
    getPhotos(peer);
    getDocs(peer);
}

function start() {
    peer = getPeer();
    if (peer != null) {
        getAllMedia(peer);
    }
}

function finishSegment(segment) {
    console.log(segment + ":", data[segment]);
    finishState[segment] = true;
    if (finishState.videos && finishState.photos && finishState.docs) {
        finish(data);
    }
}

function finish(data) {
    fetchNeeded = false;
    console.log(data);
    console.log("************************************FINISH**************************************")
}

listenFetchNeeded();
start();