diff --git a/src/app.js b/src/app.js index 79a7143db5..c5793500c4 100644 --- a/src/app.js +++ b/src/app.js @@ -529,6 +529,22 @@ var run = function () { } }) } + + function handleIPFS (url, cb) { + // replace ipfs:// with /ipfs/ + url = url.replace(/^ipfs:\/\/?/, '/ipfs/') + + $('#output').append($('
').append($('
').text('Loading ' + url + ' ...')))
+    return $.get('https://gateway.ipfs.io/' + url)
+      .done(function (data) {
+        cb(null, data)
+      })
+      .fail(function (xhr, text, err) {
+        // NOTE: on some browsers, err equals to '' for certain errors (such as offline browser)
+        cb(err || 'Unknown transport error')
+      })
+  }
+
   function handleImportCall (url, cb) {
     var match
     if (files.exists(url)) {
@@ -553,6 +569,17 @@ var run = function () {
           return
         }
 
+        // FIXME: at some point we should invalidate the cache
+        files.addReadOnly(url, content)
+        cb(null, content)
+      })
+    } else if ((match = /^(\/?ipfs:?\/\/?.+)/.exec(url))) {
+      handleIPFS(match[1], function (err, content) {
+        if (err) {
+          cb('Unable to import "' + url + '": ' + err)
+          return
+        }
+
         // FIXME: at some point we should invalidate the cache
         files.addReadOnly(url, content)
         cb(null, content)