load-image-fetch.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * JavaScript Load Image Fetch
  3. * https://github.com/blueimp/JavaScript-Load-Image
  4. *
  5. * Copyright 2017, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, fetch, Request */
  12. ;(function (factory) {
  13. 'use strict'
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['./load-image', './load-image-meta'], factory)
  17. } else if (typeof module === 'object' && module.exports) {
  18. factory(require('./load-image'), require('./load-image-meta'))
  19. } else {
  20. // Browser globals:
  21. factory(window.loadImage)
  22. }
  23. }(function (loadImage) {
  24. 'use strict'
  25. if (typeof fetch !== 'undefined' && typeof Request !== 'undefined') {
  26. loadImage.fetchBlob = function (url, callback, options) {
  27. if (loadImage.hasMetaOption(options)) {
  28. return fetch(new Request(url, options)).then(function (response) {
  29. return response.blob()
  30. }).then(callback).catch(function (err) {
  31. console.log(err)
  32. callback()
  33. })
  34. } else {
  35. callback()
  36. }
  37. }
  38. }
  39. }))