Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created May 20, 2013 21:48
Show Gist options
  • Save yocontra/5615848 to your computer and use it in GitHub Desktop.
Save yocontra/5615848 to your computer and use it in GitHub Desktop.
External image loader for chrome apps
# Just pass in the parent of the image tags.
# Image tags should have a .external-img class and data-src should be the external url
define ->
cache = {}
loader = ($el) ->
lazyLoad = (img) ->
el = $ img
url = el.attr 'data-src'
return el.attr('src', cache[url]) if cache[url]?
xhr = new XMLHttpRequest
xhr.open 'GET', url
xhr.responseType = 'blob'
xhr.onload = (e) ->
blob = window.webkitURL.createObjectURL @response
cache[url] = blob
el.attr 'src', blob
xhr.send()
imgs = $el.find(".external-img")
lazyLoad img for img in imgs
return $el
return loader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment