Greasy Fork is available in English.
Get Vk raw link without external service.
当前为
// ==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 data = {
videos: [],
photos: [],
docs: []
}
var finishState = {
videos: false,
photos: false,
docs: false
}
var itemsCount = {
videos: 0,
photos: 0,
docs: 0
}
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 isFetchNeeded() {
var videosCount = document.querySelectorAll(".video_item").length;
var fetchNeeded = false;
if (videosCount > itemsCount.videos) {
fetchNeeded = true;
itemsCount.videos = videosCount;
}
return fetchNeeded;
}
function listenFetchNeeded() {
window.setInterval(function () {
if (!isFetchNeeded()) {
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
}
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) {
console.log(data);
console.log("************************************FINISH**************************************")
}
listenFetchNeeded();
start();