From cfe10c5532d535ffdaf703d656934c53474ab0d5 Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Tue, 14 Jun 2016 05:38:20 +0200 Subject: [PATCH] Add gfycat support --- background.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index 409c4bf..53b2175 100644 --- a/background.js +++ b/background.js @@ -96,7 +96,7 @@ function getBetterUrl(url, callback) { url = url.replace("gifv", "mp4"); } - if(isTwitterUrl(url)) { + if(isTwitterImageUrl(url)) { if(url.indexOf(':') != url.lastIndexOf(':')) { //Match if there are occurences of ':' other than the ':' in 'https://', such as in '.jpg:large' url = url.substr(0, url.lastIndexOf(':')); } @@ -154,6 +154,19 @@ function getBetterUrl(url, callback) { url = url + '/imgpsh_fullsize'; callback(url); + } else if(isGfycatUrl(url)) { + var id = url.substr(url.lastIndexOf('/') + 1); + + var request = new XMLHttpRequest(); + request.open('GET', 'http://gfycat.com/cajax/get/' + id, true); + request.onload = function () { + var json = JSON.parse(request.responseText); + + var webmUrl = json.gfyItem.webmUrl; + + callback(webmUrl); + }; + request.send(); } else { callback(url); } @@ -194,7 +207,7 @@ function cleanExtension(url) { return url; } -function isTwitterUrl(url) { +function isTwitterImageUrl(url) { return url.indexOf('pbs.twimg.com/media/') != -1; } @@ -210,6 +223,10 @@ function isSkypeUrl(url) { return url.indexOf('api.asm.skype.com/v1/objects/') != -1; } +function isGfycatUrl(url) { + return url.indexOf('gfycat.com/') != -1; +} + function isCoolUrl(url, callback) { url = url.toLowerCase();