
Untitled
By: a guest on
May 21st, 2012 | syntax:
None | size: 1.04 KB | hits: 16 | expires: Never
<html>
<head></head>
<body>
<script>
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
var url = changeInfo.url,
xhr = null,
fileName = ''
if(!url || !url.match(/\.(jpe?g|gif|png)$/)) return
fileName = url.replace(/^.*[\/\\]/g, '')
console.log("requesting: ", url)
xhr = new XMLHttpRequest()
xhr.open("GET", url, true)
xhr.onload = function() {
var contentType = xhr.getResponseHeader("Content-Type")
if(!contentType.match(/^image\//)) return
console.log("saving: ", url)
// TODO: save as "test(1).jpg" if "test.jpg" already exists.
webkitRequestFileSystem(PERSISTENT, 1024*1024, function(fs) {
fs.root.getDirectory('data', { create: true }, function(dir) {
dir.getFile(fileName, { create: true }, function(file) {
file.createWriter(function(writer) {
var bb = new WebKitBlobBuilder()
bb.append(xhr.response)
writer.write(bb.getBlob(contentType))
console.log("saved: ", fileName)
}, function(e) { console.log(e) })
})
})
})
}
xhr.send()
})
</script>
</body>
</html>