independence777 |
30.08.2010 15:03 |
3dmax, у меня чуть другой код ((
помоги, чо тут сносить?
Код:
// ==UserScript==
// @name VK musdic downloader
// @version 1.10
// @date 2010-07-06
// @author [email protected]><e
//
// @include http://vkontakte.ru/*
// @include http://vk.com/*
// @include http://*.vkontakte.ru/*
// @include http://*.vk.com/*
// ==/UserScript==
(function(){
var tools = {
serialize : function(obj){
var i, length, arr = [];
if(obj instanceof HTMLElement)
obj = tools.serializeToObject(obj);
for(i in obj)
arr.push(i + '=' + encodeURIComponent(obj[i]));
return arr.join('&');
},
deserialize : function(str){
var obj = {}, i = 0, k, length;
str = str.split('&');
for(length = str.length; i < length; ++i){
k = str[i].split('=');
obj[k[0]] = decodeURIComponent(k[1]);
}
return obj;
},
ajax : function(method, url, data, callback){
var request = XMLHttpRequest();
data = data || '';
if(typeof data != 'string')
data = tools.serialize(data);
request.open(method.toUpperCase(), url || location.href, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.onreadystatechange = function(){
if (request.readyState != 4) return;
if (request.status == 200) {
if(typeof callback == 'function')
callback(request.responseText, request);
} else {
throw new Error('Error while processing XMLHttpRequest #' + request.status);
}
};
request.send(data);
},
foreach: function(nodes, callback){
for(var i = 0; i < nodes.length; ++i){
callback.call(nodes[i]);
}
},
formatSize : function(length){
var i = 0, type = ['б','Кб','Мб','Гб','Тб','Пб'];
while(parseInt(length / 1000) && i < type.length) {
length /= 1024;
i++
}
return length.toFixed(2) + ' ' + type[i];
},
createElement: function(tag, opts){
var element = document.createElement(tag), key;
opts = opts || {};
return tools.extend(element, opts);
},
extend: function(origObj, newObj){
for(var key in newObj){
if(typeof newObj[key] == 'object'){
origObj[key] = {};
tools.extend(origObj[key], newObj[key]);
} else origObj[key] = newObj[key]
}
return origObj;
}
},
imgBS64 = 'data:image/gif;base64,R0lGODlhEAAQAJEAAJqxxv///199nQAAACH5BAAAAAAALAAAAAAQABAAAAIjDISpCLafDpwv2EDTxVns7l3Z9k0kR5WdBbbhybqKBDY01RQAOw%3D%3D';
if(~location.hash.indexOf('dataTransfer')){
window.addEventListener("message", function(event){
var data = tools.deserialize(event.data);
tools.ajax('head', data.file, '', function(text, obj){
var length = obj.getResponseHeader('Content-Length');
window.parent.postMessage(tools.serialize({'id':data.id, 'length': length}), '*');
});
},false);
return;
}
window.addEventListener('DOMContentLoaded', function(){
var style = tools.createElement('style', {'innerHTML' : '.__getBitrate {cursor:help}'});
document.querySelector('head').appendChild(style);
var audios = document.querySelectorAll('div.audioRow'),
iframe = tools.createElement('iframe', {
'style' : {
'position' : 'absolute',
'top' : '-999px',
'left' : '-999px',
'height' : '1px',
'width' : '1px'
},
'id' : '_dataTransferFrame'
});
document.body.appendChild(iframe);
function processAudio(){
var img = this.querySelector('img.playimg'),
file = img.getAttribute('onclick').match(/operate\(\d+,\s*'([^']+)',\s*\d+\)/i),
self = this;
if(!file){
file = img.getAttribute('onclick').match(/operate\(\d+,(\d+),(\d+),'([^']+)',\d+\)/i);
if(!file) throw new Error('Cannot get file url for #' + this.id);
file = 'http://cs' + file[1] + '.' + location.host + '/u' + file[2] + '/audio/' + file[3] + '.mp3';
} else file = file[1];
var download = tools.createElement('a', {'href':file}),
downloadImg = tools.createElement('img', {'src':imgBS64});
download.appendChild(downloadImg);
img.parentNode.appendChild(download);
img.parentNode.style.width = '50px';
var subdomain = file.match(/^(http:\/\/\w+\.(?:vkontakte\.ru|vk\.com))/);
if(!subdomain) throw new Error('Cannot get subdomain for #' + this.id);
subdomain = subdomain[1];
var btn = this.querySelector('div.duration');
if(!btn) throw new Error('Cannot get duration label');
btn.className += ' __getBitrate';
btn.title = 'Нажмите, чтобы получить битрейт';
btn.onclick = function(){
var elem = {
'id' : self.id,
'file' : file
},
url = subdomain + '/#dataTransfer';
if(iframe.src != url){
iframe.src = url;
iframe.onload = function(){
iframe.contentWindow.postMessage(tools.serialize(elem), '*');
};
} else iframe.contentWindow.postMessage(tools.serialize(elem), '*');
this.onclick = null;
this.className = this.className.replace('__getBitrate', '');
this.title = '';
};
}
tools.foreach(audios, processAudio);
setInterval(function(){
if(!audios[0].parentNode){
audios = document.querySelectorAll('div.audioRow');
tools.foreach(audios, processAudio);
}
}, 200);
window.addEventListener("message", function(event){
var data = tools.deserialize(event.data),
div = document.querySelector('#' + data.id + ' div.duration'),
duration = div.innerHTML.split(':');
duration = duration[0] * 60 + +duration[1];
div.innerHTML += ' | ' + ((data.length / duration / 125) | 0) + ' Кбит/с | ' + tools.formatSize(data.length);
},false);
}, true);
})();
|