Created
September 28, 2010 18:02
-
-
Save banyan/601452 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== | |
// @name Last.fm Download Helper | |
// @namespace http://d.hatena.ne.jp/bannyan/ | |
// @description It helps you Last.fm free download easily. | |
// @include http://*last.fm/* | |
// @include http://www.lastfm.jp/* | |
// ==/UserScript== | |
// Configure | |
// How many files download when botton is clicked. | |
// Default is 3, but it's OK to change as you like. | |
const RANGE = 3; | |
// How long does it wait after when one file downloaded. | |
// Default is 5000 millisecond, otherwise 503 error occur. | |
const DONWLOAD_DULATION = 10000; | |
// --------------------------------------------------------- | |
GM_addStyle(<><![CDATA[ | |
#GMDownloadButton { | |
border : 2px #fff solid; | |
font : italic bold 20px Georgia,"Times New Roman",Times,serif; | |
background : #444444; | |
position : fixed; | |
bottom : 20px; | |
cursor : pointer; | |
left : 0px; | |
color : #fff; | |
width : 80px; | |
height : 30px; | |
-moz-background-clip : border; | |
-moz-background-origin : padding; | |
-moz-background-inline-policy : continuous; | |
-moz-opacity : 0.75; | |
} | |
#GMDownloadButton:hover { | |
background : #d71102; | |
} | |
]]></>); | |
var DownloadHelper = function() { | |
var getDownloadUrls = function() { | |
var isProperUrl = function(string) { | |
return /^http:\/\/freedownloads\.last\.fm\/download\/[^ ]*\.mp3$/.test(string); | |
} | |
var links = document.evaluate( | |
'.//a', | |
document, | |
null, | |
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, | |
null | |
); | |
var downloadLinks = []; | |
for (var i = 0; i < links.snapshotLength; i++) { | |
var url = links.snapshotItem(i).href; | |
if (isProperUrl(url)) { | |
downloadLinks.push(url); | |
} | |
} | |
return uniq(downloadLinks); | |
} | |
var setStatus = function(currentIndex, totalFileSize) { | |
update(document.getElementById('GMDownloadButton'), { | |
'value' : (currentIndex + 1) + '/' + totalFileSize | |
}); | |
} | |
var urls = getDownloadUrls(); | |
var cursor = 0; | |
var isDone = false; | |
if (urls.length === 0) { | |
isDone = true; | |
} | |
return { | |
'download': function() { | |
if (isDone) return; | |
// forEach の break が分からない | |
urls.forEach(function(url, i) { | |
window.setTimeout(bind(this, function() { | |
window.open(encodeURI(url), '_blank'); | |
if (i === urls.length - 1) { | |
alert("You've successfully downloaded all of files in this page!!"); | |
isDone = true; | |
setStatus(i, urls.length); | |
return false; | |
} else if (i === cursor + RANGE - 1) { | |
setStatus(i, urls.length); | |
return false; | |
} else { | |
setStatus(i, urls.length); | |
} | |
}), DONWLOAD_DULATION * (i)); | |
console.log(i); | |
}); | |
cursor = cursor + RANGE; | |
}, | |
'totalFileSize': urls.length | |
} | |
} | |
var downloadHelper = DownloadHelper(); | |
document.body.appendChild( | |
update(document.createElement('input'), { | |
'id' : 'GMDownloadButton', | |
'type' : 'button', | |
'value' : '0/' + downloadHelper.totalFileSize | |
}) | |
).addEventListener('click', function() { | |
downloadHelper.download(); | |
}, false); | |
// update stolen from LDR - Signal (c) id:brazil | |
function update(obj, params) { | |
if(obj.setAttribute){ | |
for(var key in params) | |
obj.setAttribute(key, params[key]); | |
} else { | |
for(var key in params) | |
obj[key] = params[key]; | |
} | |
return obj; | |
} | |
// bind stolen from Tumblr - Strobo (c) id:brazil | |
function bind(obj, func) { | |
func = (func instanceof Function)? func : obj[func]; | |
return function() { | |
func.apply(obj, arguments); | |
} | |
} | |
// uniq stolen from http://d.hatena.ne.jp/javascripter/20080730/1217413682 (c) id:javascripter | |
function uniq(arr) { | |
var o = {}; | |
return Array.filter(arr, | |
function(i) i in o? false: o[i] = true); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment