Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 1.04 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <html>
  2. <head></head>
  3. <body>
  4. <script>
  5. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  6.         var url = changeInfo.url,
  7.             xhr = null,
  8.             fileName = ''
  9.  
  10.         if(!url || !url.match(/\.(jpe?g|gif|png)$/)) return
  11.         fileName = url.replace(/^.*[\/\\]/g, '')
  12.  
  13.         console.log("requesting: ", url)
  14.         xhr = new XMLHttpRequest()
  15.         xhr.open("GET", url, true)
  16.         xhr.onload = function() {
  17.                 var contentType = xhr.getResponseHeader("Content-Type")
  18.                 if(!contentType.match(/^image\//)) return
  19.                 console.log("saving: ", url)
  20.                 // TODO: save as "test(1).jpg" if "test.jpg" already exists.
  21.                 webkitRequestFileSystem(PERSISTENT, 1024*1024, function(fs) {
  22.                         fs.root.getDirectory('data', { create: true }, function(dir) {
  23.                                 dir.getFile(fileName, { create: true }, function(file) {
  24.                                         file.createWriter(function(writer) {
  25.                                                 var bb = new WebKitBlobBuilder()
  26.                                                 bb.append(xhr.response)
  27.                                                 writer.write(bb.getBlob(contentType))
  28.                                                 console.log("saved: ", fileName)
  29.                                         }, function(e) { console.log(e) })
  30.                                 })
  31.                         })
  32.                 })
  33.         }
  34.         xhr.send()
  35. })
  36. </script>
  37. </body>
  38. </html>