diff options
Diffstat (limited to 'public/assets/javascripts/ui/lib/Parser.js')
| -rw-r--r-- | public/assets/javascripts/ui/lib/Parser.js | 84 |
1 files changed, 63 insertions, 21 deletions
diff --git a/public/assets/javascripts/ui/lib/Parser.js b/public/assets/javascripts/ui/lib/Parser.js index 705ff04..dfff7b2 100644 --- a/public/assets/javascripts/ui/lib/Parser.js +++ b/public/assets/javascripts/ui/lib/Parser.js @@ -2,12 +2,18 @@ var Parser = { integrations: [{ type: 'image', regex: /\.(jpeg|jpg|gif|png|svg)(\?.*)?$/i, - async: false, fetch: function(url, done) { var img = new Image () img.onload = function(){ - done("", "", img.naturalWidth, img.naturalHeight, "") + var width = img.naturalWidth, height = img.naturalHeight img = null + done({ + token: "", + thumbnail: "", + title: "", + width: width, + height: height, + }) } img.src = url if (img.complete) { @@ -18,9 +24,30 @@ var Parser = { return '<img src="' + media.url + '" onerror="imgError(this);">'; } }, { + type: 'video', + regex: /\.(mp4|webm)(\?.*)?$/i, + fetch: function(url, done) { + var video = document.createElement("video") + video.addEventListener("loadedmetadata", function(){ + var width = video.videoWidth, height = video.videoHeight + video = null + done({ + token: "", + thumbnail: "", + title: "", + width: width, + height: height, + }) + }) + video.src = url + video.load() + }, + tag: function (media) { + return '<video src="' + media.url + '" onerror="imgError(this);">'; + } + }, { type: 'youtube', regex: /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i, - async: false, fetch: function(url, done) { var id = (url.match(/v=([-_a-zA-Z0-9]{11})/i) || url.match(/youtu.be\/([-_a-zA-Z0-9]{11})/i) || url.match(/embed\/([-_a-zA-Z0-9]{11})/i))[1].split('&')[0]; var thumb = "http://i.ytimg.com/vi/" + id + "/hqdefault.jpg" @@ -34,8 +61,14 @@ var Parser = { part: "id,contentDetails,snippet,status", }, success: function(result){ - var res = res.items[0] - done(id, thumb, 640, 360, res.snippet.title); + var res = result.items[0] + done({ + token: id, + thumbnail: thumb, + title: res.snippet.title, + width: 640, + height: 360, + }) } }) }, @@ -45,7 +78,6 @@ var Parser = { }, { type: 'vimeo', regex: /vimeo.com\/\d+$/i, - async: true, fetch: function(url, done) { var id = url.match(/\d+$/i)[0]; $.ajax({ @@ -54,7 +86,13 @@ var Parser = { success: function(result){ if (result.length == 0) { return done(id, "", 640, 360) } var res = result[0] - done(id, res.thumbnail_large, res.width, res.height, res.title) + done({ + token: id, + thumbnail: res.thumbnail_large, + title: res.title, + width: res.width, + height: res.height, + }) } }) }, @@ -66,7 +104,6 @@ var Parser = { { type: 'soundcloud', regex: /soundcloud.com\/[-a-zA-Z0-9]+\/[-a-zA-Z0-9]+\/?$/i, - async: true, fetch: function (url, done) { $.ajax({ type: 'GET', @@ -75,7 +112,13 @@ var Parser = { + '&client_id=' + '0673fbe6fc794a7750f680747e863b10', success: function(result) { - done(result.id, ""); + done({ + token: result.id, + thumbnail: "", + title: "", + width: 100, + height: 100, + }) } }); }, @@ -87,9 +130,14 @@ var Parser = { }, { type: 'link', regex: /^http.+/i, - async: false, fetch: function(url, done) { - done("", "") + done({ + token: "", + thumbnail: "", + title: "", + width: 100, + height: 100, + }) }, tag: function (media) { return '<a href="' + media.url + '" target="_blank">' + media.url + '</a>' @@ -101,16 +149,10 @@ var Parser = { parse: function (url, cb) { var matched = Parser.integrations.some(function(integration){ if (integration.regex.test(url)) { - integration.fetch(url, function(token, thumbnail, width, height, title){ - cb({ - token: token, - thumbnail: thumbnail, - type: integration.type, - title: title, - width: width, - height: height, - url: url, - }) + integration.fetch(url, function(res){ + res.url = url + res.type = integration.type + cb(res) }) return true } |
