Last active
October 24, 2019 03:59
-
-
Save Pmmlabs/0caeee114c77b7037922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @id imdump@vkopt | |
// @name Скачивание всех диалогов в TXT | |
// @version 1.2 | |
// @namespace https://greasyfork.org/users/23 | |
// @author Pmmlabs@github | |
// @description Плагин для VkOpt, позволяющий получить zip-архив со всеми диалогами ВКонтакте | |
// @include *vk.com* | |
// @run-at document-end | |
// @downloadURL https://gist.github.com/Pmmlabs/0caeee114c77b7037922/raw/c4a79c6819ef1980eb43626693201afcfbe4105c/imdump.user.js | |
// @updateURL https://gist.github.com/Pmmlabs/0caeee114c77b7037922/raw/c4a79c6819ef1980eb43626693201afcfbe4105c/imdump.user.js | |
// @noframes | |
// @grant none | |
// ==/UserScript== | |
if (!window.vkopt_plugins) vkopt_plugins = {}; | |
(function () { | |
var PLUGIN_ID = 'imdump'; | |
vkopt_plugins[PLUGIN_ID] = { | |
limit: 200, | |
dialogs: [], | |
zip: null, | |
box: null, | |
ready: false, | |
onLocation: function (nav_obj, cur_module_name) { | |
if (cur_module_name == 'im' && !nav_obj.sel && !this.ready) { | |
this.ready = true; | |
Inj.Wait('cur.vkAlbumMenu', function () { | |
cur.vkAlbumMenu.addItem({ | |
i: 1, l: 'Скачать всё', onClick: function () { | |
vkopt_plugins[PLUGIN_ID].dialogs=[]; | |
vkLdr.show(); | |
JsZipConnect(function () { | |
vkLdr.hide(); | |
vkopt_plugins[PLUGIN_ID].zip = new JSZip(); // Создание объекта JSZip в ранее объявленную переменную | |
vkopt_plugins[PLUGIN_ID].box = vkAlertBox(IDL('Loading'), '<div id="imdump1"></div><div id="imdump2"></div>'); | |
vkopt_plugins[PLUGIN_ID].download(0); | |
}); | |
} | |
}) | |
}); | |
} | |
if (cur_module_name != 'im') | |
this.ready = false; | |
}, | |
download: function (_offset) { | |
dApi.call('messages.getDialogs', { | |
offset: _offset, | |
count: vkopt_plugins[PLUGIN_ID].limit, | |
preview_length: 1 | |
}, function (r, response) { | |
var total = response.shift(); | |
for (var i in response) | |
vkopt_plugins[PLUGIN_ID].dialogs.push(response[i].chat_id ? 2000000000 + response[i].chat_id : response[i].uid); | |
ge("imdump2").innerHTML = vkProgressBar(vkopt_plugins[PLUGIN_ID].dialogs.length, total, 350, '%, информация о диалогах'); | |
if (vkopt_plugins[PLUGIN_ID].dialogs.length < total) | |
vkopt_plugins[PLUGIN_ID].download(vkopt_plugins[PLUGIN_ID].dialogs.length); | |
else | |
vkopt_plugins[PLUGIN_ID].make(0); | |
}); | |
}, | |
make: function (dialog_id) { | |
if (dialog_id < vkopt_plugins[PLUGIN_ID].dialogs.length) { | |
ge("imdump2").innerHTML = vkProgressBar(dialog_id + 1, vkopt_plugins[PLUGIN_ID].dialogs.length, 350, '%, все диалоги'); | |
var uid = vkopt_plugins[PLUGIN_ID].dialogs[dialog_id] || vk.id; | |
var offset = 0; | |
var result = ''; | |
var user1 = 'user1'; | |
var msg_pattern = vkGetVal('VK_SAVE_MSG_HISTORY_PATTERN') || SAVE_MSG_HISTORY_PATTERN; | |
var date_fmt = vkGetVal('VK_SAVE_MSG_HISTORY_DATE_FORMAT') || SAVE_MSG_HISTORY_DATE_FORMAT; | |
msg_pattern = msg_pattern.replace(/\r?\n/g, '\r\n'); | |
date_fmt = date_fmt.replace(/\r?\n/g, '\r\n'); | |
var users = {}; | |
var users_ids = []; | |
var history_uids = {}; | |
var collect = function (callback) { | |
dApi.call('messages.getHistory', {uid: uid, offset: offset, count: 100}, function (r) { | |
ge("imdump1").innerHTML = vkProgressBar(offset, r.response[0], 350, '%, текущий диалог'); | |
var msgs = r.response; | |
var count = msgs.shift(); | |
msgs.reverse(); | |
var msg = null; | |
var res = ''; | |
var make_msg = function (msg, level) { | |
level = level || 0; | |
var from_id = msg.from_id || msg.uid; | |
if (msg.from_id) history_uids['%' + msg.from_id + '%'] = '1'; | |
if (!users['%' + from_id + '%']) { | |
users['%' + from_id + '%'] = 'id' + from_id + ' DELETED'; | |
users_ids.push(from_id); | |
} | |
var attach_text = ""; | |
for (var j = 0; msg.attachments && j < msg.attachments.length; j++) { | |
var attach = msg.attachments[j]; | |
switch (attach.type) { | |
case "photo": | |
var a = attach.photo; | |
var src = a.src_xxxbig || a.src_xxbig || a.src_xbig || a.src_big || a.src || a.src_small; | |
var link = "vk.com/photo" + a.owner_id + '_' + a.pid; | |
attach_text += link + " : " + src + "\r\n" + (a.text ? a.text + "\r\n" : ""); | |
break; | |
case "video": | |
var a = attach.video; | |
var link = "vk.com/video" + a.owner_id + '_' + a.vid; | |
attach_text += link + " : " + (a.title ? a.title + "\r\n" : "") + "\r\n" + (a.description ? a.description + "\r\n" : ""); | |
break; | |
case "audio": | |
var a = attach.audio; | |
var link = "vk.com/audio?id=" + a.owner_id + '&audio_id=' + a.aid; | |
attach_text += link + " : " + (a.performer || "") + " - " + (a.title || "") + "\r\n"; | |
break; | |
case "doc": | |
var a = attach.doc; | |
attach_text += a.url + " (" + vkFileSize(a.size) + "): " + a.title + "\r\n"; | |
break; | |
default: | |
attach_text += JSON.stringify(attach) + '\r\n'; | |
/* | |
case "wall": | |
break;*/ | |
} | |
} | |
//console.log(msg); | |
var date = dateFormat(msg.date * 1000, date_fmt); | |
var user = '%' + from_id + '%';//(msg.from_id==mid?user2:user1); | |
var text = vkCe('div', {}, (msg.body || '').replace(/<br>/g, "%{br}%")).innerText.replace(/%{br}%/g, '\r\n');// no comments.... | |
//text=text.replace(/\n/g,'\r\n'); | |
var ret = msg_pattern | |
.replace(/%username%/g, user) //msg.from_id | |
.replace(/%date%/g, date) | |
.replace(/%message%/g, text) | |
.replace(/%attachments%/g, (attach_text != "" ? "Attachments:[\r\n" + attach_text + "]" : "")); | |
var tab = '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'; | |
ret = ret.replace(/^.+$/mg, tab.substr(0, level) + "$&"); | |
if (msg.fwd_messages) | |
for (var i = 0; i < msg.fwd_messages.length; i++) | |
ret += make_msg(msg.fwd_messages[i], level + 1); | |
return ret; | |
}; | |
for (var i = 0; i < msgs.length; i++) { | |
msg = msgs[i]; | |
res += make_msg(msg); | |
} | |
result = res + result; | |
if (offset < count) { | |
offset += 100; | |
setTimeout(function () { | |
collect(callback); | |
}, 300); | |
} else { | |
//alert(result); | |
callback(result); | |
} | |
}); | |
}; | |
collect(function (t) { | |
if (users_ids.length == 1) | |
users_ids.push(uid); | |
dApi.call('getProfiles', {uids: users_ids.join(',')}, function (r) { | |
var file_name = []; | |
for (var i = 0; i < r.response.length; i++) { | |
var u = r.response[i]; | |
users['%' + u.uid + '%'] = u.first_name + " " + u.last_name; | |
} | |
for (var key in users) { | |
var uuid = parseInt((key || '0').replace(/%/g, '')); | |
if (history_uids[key] && !(window.vk && uuid == vk.id)) file_name.push(users[key] + '(' + uuid + ')'); | |
t = t.split(key).join(users[key]); | |
} | |
if (!file_name.length) | |
file_name.push(users[key] + '(' + uuid + ')'); | |
if (t != '') | |
vkopt_plugins[PLUGIN_ID].zip.file((1000+dialog_id+'').substr(1) + "messages_" + vkCleanFileName(file_name.join(',')).substr(0, 250) + ".txt", t); | |
vkopt_plugins[PLUGIN_ID].make(dialog_id + 1); | |
}); | |
}); | |
} else { | |
var content = vkopt_plugins[PLUGIN_ID].zip.generate({type: "blob"}); | |
saveAs(content, "Dialogs.zip"); | |
vkopt_plugins[PLUGIN_ID].box.hide(); | |
} | |
} | |
}; | |
if (window.vkopt_ready) vkopt_plugin_run(PLUGIN_ID); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment