Created
January 15, 2013 02:01
-
-
Save kanzure/4535401 to your computer and use it in GitHub Desktop.
greasemonkey-springerlink-downloader
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 Springer Liberator | |
// @namespace http://userscripts.org/springer-liberator | |
// @description A script to scrape articles from Springer | |
// @include http://link.springer.com/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; | |
window.URL = window.URL || window.webkitURL; | |
// Function to pad strings | |
function pad(n, width, z) { | |
z = z || '0'; | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} | |
$(document).ready(function(){ | |
// Create new liberate button | |
previewData = $('span.icon-view').html(); | |
liberateBtn = document.createElement('span'); | |
$(liberateBtn).addClass("action").addClass("icon-download").html(previewData); // Copy preview button data | |
previewUrl = $(liberateBtn).children("a").attr("href"); | |
$(liberateBtn).children("a").click(function() { $.downloadImages(previewUrl); }); | |
$(liberateBtn).children("a").attr("href","#"); | |
$(liberateBtn).children("a").html("Liberate"); // Change link title | |
$(liberateBtn).children("a").addClass("liberate") | |
$(liberateBtn).children("a").removeAttr("viewtype") | |
$('span.icon-view').after(liberateBtn); | |
//<progress min="0" max="100" value="0">0% complete</progress> | |
// Only start download on click | |
$.downloadImages = function(previewUrl){ | |
// Get document length | |
citation_firstpage = $('meta[name=citation_firstpage]').attr("content"); | |
citation_lastpage = $('meta[name=citation_lastpage]').attr("content"); | |
citation_length = citation_lastpage - citation_firstpage + 1; | |
// Create list of URL's to download | |
previewUrls = []; | |
baseUrl = previewUrl.substr(0,previewUrl.lastIndexOf("/")+1); | |
for (page=0; page<citation_length; page++) { | |
previewUrls.push(baseUrl+pad(page,3)+".png"); | |
} | |
//Loop through and retrieve each image | |
previewBlobs = []; | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', previewUrls.pop(), true); | |
xhr.responseType = 'blob'; | |
xhr.onload = function() { | |
if(this.status == 200) { | |
blob = this.response; | |
previewBlobs.push(blob); | |
} | |
// When downloads are complete call upload function | |
if(previewUrls.length == 0){ | |
uploadFiles(previewBlobs); | |
return; | |
} | |
xhr.open("GET", previewUrls.pop(), true); | |
xhr.send(); | |
}; | |
xhr.send(); | |
// Script to send all images to the server | |
function uploadFiles(blobs) { | |
var formData = new FormData(); | |
formData.append("title", $('meta[name=citation_title]').attr("content")); | |
formData.append("doi", $('meta[name=citation_doi]').attr("content")); | |
for (var i = 0, file; file = blobs[i]; ++i) { | |
formData.append("images[]", file, pad(i,3)+".png"); | |
} | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', "http://localhost/upload.php", true); | |
xhr.onload = function() { | |
alert(xhr.status + " - " + xhr.response); | |
if(xhr.status == 200){ | |
document.location = xhr.response; | |
} | |
alert("upload done"); | |
}; | |
xhr.send(formData); // multipart/form-data | |
alert("upload sent"); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment