From 9fb8ab50e13615b7df1688276a7414d0e0a3132f Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Wed, 16 Oct 2013 14:31:18 -0700 Subject: [PATCH] MediaCrush embedding (the right way?) --- builds/4chan-X.user.js | 51 ++++++++++++++++++++++++++++-- builds/crx/script.js | 51 ++++++++++++++++++++++++++++-- src/Linkification/Linkify.coffee | 54 +++++++++++++++++--------------- src/Miscellaneous/Time.coffee | 1 - 4 files changed, 124 insertions(+), 33 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 0a0d80e83..97eaa366a 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -4808,11 +4808,56 @@ }, MediaCrush: { regExp: /.*(?:mediacru.sh\/)([0-9a-z_]+)/i, - style: 'border: 0; width: 640px; height: 480px; resize: both;', + style: 'border: 0;', el: function(a) { - return $.el('iframe', { - src: "https://mediacru.sh/" + a.dataset.uid + var el; + + el = $.el('div'); + $.cache("https://mediacru.sh/" + a.dataset.uid + ".json", function() { + var embed, file, files, status, type, _i, _j, _len, _len1, _ref; + + status = this.status; + if (![200, 304].contains(status)) { + return div.innerHTML = "ERROR " + status; + } + files = JSON.parse(this.response).files; + _ref = ['video/mp4', 'video/ogv', 'image/svg+xml', 'image/png', 'image/gif', 'image/jpeg', 'image/svg', 'audio/mpeg']; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + type = _ref[_i]; + for (_j = 0, _len1 = files.length; _j < _len1; _j++) { + file = files[_j]; + if (file.type === type) { + embed = file; + break; + } + } + if (embed) { + break; + } + } + if (!embed) { + return div.innerHTML = "ERROR: Not a valid filetype"; + } + return el.innerHTML = (function() { + switch (embed.type) { + case 'video/mp4': + case 'video/ogv': + return ""; + case 'image/png': + case 'image/gif': + case 'image/jpeg': + return ""; + case 'image/svg': + case 'image/svg+xml': + return ""; + case 'audio/mpeg': + return ""; + default: + return "ERROR: No valid filetype."; + } + })(); }); + return el; } }, pastebin: { diff --git a/builds/crx/script.js b/builds/crx/script.js index 11b7e0081..bbadff962 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -4815,11 +4815,56 @@ }, MediaCrush: { regExp: /.*(?:mediacru.sh\/)([0-9a-z_]+)/i, - style: 'border: 0; width: 640px; height: 480px; resize: both;', + style: 'border: 0;', el: function(a) { - return $.el('iframe', { - src: "https://mediacru.sh/" + a.dataset.uid + var el; + + el = $.el('div'); + $.cache("https://mediacru.sh/" + a.dataset.uid + ".json", function() { + var embed, file, files, status, type, _i, _j, _len, _len1, _ref; + + status = this.status; + if (![200, 304].contains(status)) { + return div.innerHTML = "ERROR " + status; + } + files = JSON.parse(this.response).files; + _ref = ['video/mp4', 'video/ogv', 'image/svg+xml', 'image/png', 'image/gif', 'image/jpeg', 'image/svg', 'audio/mpeg']; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + type = _ref[_i]; + for (_j = 0, _len1 = files.length; _j < _len1; _j++) { + file = files[_j]; + if (file.type === type) { + embed = file; + break; + } + } + if (embed) { + break; + } + } + if (!embed) { + return div.innerHTML = "ERROR: Not a valid filetype"; + } + return el.innerHTML = (function() { + switch (embed.type) { + case 'video/mp4': + case 'video/ogv': + return ""; + case 'image/png': + case 'image/gif': + case 'image/jpeg': + return ""; + case 'image/svg': + case 'image/svg+xml': + return ""; + case 'audio/mpeg': + return ""; + default: + return "ERROR: No valid filetype."; + } + })(); }); + return el; } }, pastebin: { diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index 923c66613..72ba89654 100755 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -293,33 +293,35 @@ Linkify = MediaCrush: regExp: /.*(?:mediacru.sh\/)([0-9a-z_]+)/i - style: 'border: 0; width: 640px; height: 480px; resize: both;' + style: 'border: 0;' el: (a) -> - $.el 'iframe', - src: "https://mediacru.sh/#{a.dataset.uid}" -# MediaCrush CORS When? -# -# el = $.el 'div' -# $.cache "https://mediacru.sh/#{a.dataset.uid}.json", -> -# {status} = @ -# return unless [200, 304].contains status -# {files} = JSON.parse req.response -# file = file for file of files when files.hasOwnProperty file -# el.innerHTML = switch file.type -# when 'video/mp4', 'video/ogv' -# """ -#""" -# when 'image/png', 'image/gif', 'image/jpeg' -# "" -# when 'image/svg', 'image/svg+xml' -# "" -# when 'audio/mpeg' -# "" -# el - + el = $.el 'div' + $.cache "https://mediacru.sh/#{a.dataset.uid}.json", -> + {status} = @ + return div.innerHTML = "ERROR #{status}" unless [200, 304].contains status + {files} = JSON.parse @response + for type in ['video/mp4', 'video/ogv', 'image/svg+xml', 'image/png', 'image/gif', 'image/jpeg', 'image/svg', 'audio/mpeg'] + for file in files + if file.type is type + embed = file + break + break if embed + return div.innerHTML = "ERROR: Not a valid filetype" unless embed + el.innerHTML = switch embed.type + when 'video/mp4', 'video/ogv' then """ +""" + when 'image/png', 'image/gif', 'image/jpeg' + "" + when 'image/svg', 'image/svg+xml' + "" + when 'audio/mpeg' + "" + else + "ERROR: No valid filetype." + el pastebin: regExp: /.*(?:pastebin.com\/(?!u\/))([^#\&\?]*).*/ diff --git a/src/Miscellaneous/Time.coffee b/src/Miscellaneous/Time.coffee index 40d581179..c76f18cf2 100755 --- a/src/Miscellaneous/Time.coffee +++ b/src/Miscellaneous/Time.coffee @@ -58,4 +58,3 @@ Time = S: -> Time.zeroPad @getSeconds() y: -> @getFullYear().toString()[2..] Y: -> @getFullYear() -