Last active
August 29, 2015 14:24
-
-
Save visualphoenix/9dd5bb8fed6d818e55c7 to your computer and use it in GitHub Desktop.
Inline Reddit Images/Gifv/Youtube/Videos Imgur/Youtube/Gifv Bookmarklet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: (function () { | |
var things = document.querySelectorAll("div.thing"); | |
for (var j = things.length - 1; j >= 0; j--) { | |
var thing = things[j] | |
, entry = thing.querySelectorAll("div.entry")[0] | |
, thumb = thing.querySelectorAll("a.thumbnail")[0] | |
, title = thing.querySelectorAll("a.title")[0]; | |
if (thumb || !thumb && title) { | |
var href = thumb && thumb.href || title.href; | |
href = href.replace(/^http:\/\//i, 'https://'); | |
var ele; | |
if (/\.(mp4|webm|gifv)$/i.test(href) || (/^.*(?:\/\/|\.)(?:gfycat.com\/)/i.test(href)) || (/imgur\.com/i.test(href) && !/\/a\//.test(href) && /\.(gif)$/i.test(href))) { | |
ele = document.createElement('video'); | |
var id; | |
if (/imgur\.com/i.test(href) && !/\/a\/|\/gallery\//.test(href)) { | |
id = /^.*(?:imgur\.com)\/(?:gallery\/)?(\w+)\/?.*$/.exec(href)[1]; | |
href = "//i.imgur.com/" + id + ".gifv"; | |
} else if (/^.*(?:\/\/|\.)(?:gfycat.com\/)/i.test(href)) { | |
ele.setAttribute('id', 'gfyVid1'); | |
ele.setAttribute('class', 'gfyVid1'); | |
ele.setAttribute('muted', 'muted'); | |
} | |
var mp4id = null, webmid = null; | |
if (/^.*(?:\/\/|\.)(?:gfycat.com\/)/i.test(href)) { | |
id = /\/(\w+)(?:\.\w+|\?|\#.*)?$/.exec(href)[1]; | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.open( "GET", 'https://gfycat.com/cajax/get/' + id, false ); | |
xmlHttp.send( null ); | |
var gfy = JSON.parse(xmlHttp.responseText).gfyItem; | |
var max_width = 480; | |
var o_width = gfy.width; | |
var o_height = gfy.height; | |
var ratio = o_height / o_width; | |
var w = o_width > max_width ? max_width : o_width; | |
var h = o_width > max_width ? Math.round(max_width * ratio) : o_height; | |
ele.setAttribute("width", w); | |
ele.setAttribute("height", h); | |
href = id + '.gifv'; | |
mp4id = gfy.mp4Url; | |
webmid = gfy.webmUrl; | |
} else { | |
id = /(.*\w+)\.\w+?$/.exec(href)[1]; | |
mp4id = id + ".mp4"; | |
webmid = id + ".webm"; | |
} | |
if (/\.(gifv)$/i.test(href)) { | |
ele.autoplay = true; | |
ele.loop = true; | |
} | |
if (/\.(webm|gifv)$/i.test(href)) { | |
var s = document.createElement('source'); | |
s.type = "video/webm"; | |
s.src = webmid; | |
ele.appendChild(s); | |
} | |
if (/\.(mp4|gifv)$/i.test(href)) { | |
var s = document.createElement('source'); | |
s.type = "video/mp4"; | |
s.src = mp4id; | |
ele.appendChild(s); | |
} | |
if (/imgur\.com/i.test(href) && !/\/a\//.test(href)) { | |
var s = new Image; | |
s.src = id + ".jpg"; | |
ele.appendChild(s); | |
} | |
entry.appendChild(ele); | |
} else if (/^.*(?:\/\/|\.)(?:youtu\.be\/|youtube.com\/)/i.test(href)) { | |
ele = document.createElement("iframe"); | |
var id = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/.exec(href); | |
var iframe_url = "https://www.youtube.com/embed/" + id[1] + "?autoplay=0&autohide=1"; | |
ele.setAttribute("src", iframe_url); | |
ele.setAttribute("frameborder", '0'); | |
ele.setAttribute("allowfullscreen", '1'); | |
entry.appendChild(ele); | |
} else if (/^.*(?:\/\/|\.)(?:gifyoutube.com\/)/i.test(href)) { | |
ele = document.createElement("iframe"); | |
var id = /^.*(?:gifyoutube\.com\/gif\/)([^#\&\?]*).*/.exec(href); | |
var iframe_url = "https://www.gifyoutube.com/embed/" + id[1]; | |
ele.setAttribute("src", iframe_url); | |
ele.setAttribute("frameborder", '0'); | |
ele.setAttribute("scrolling", 'no'); | |
ele.setAttribute("width", '480'); | |
ele.setAttribute("height", '270'); | |
ele.setAttribute("style", '-webkit-backface-visibility: hidden;-webkit-transform: scale(1);'); | |
ele.setAttribute("allowfullscreen", '1'); | |
entry.appendChild(ele); | |
} else { | |
ele = new Image; | |
if (/\.(png|gif|jpe?g)\?*.*$/i.test(href)) { | |
ele.src = href; | |
} else if (/imgur\.com/i.test(href) && !/\/a\/|\/gallery\//.test(href)) { | |
var id = /^.*(?:imgur\.com)\/(?:gallery\/|t\/(?:.*)\/)?(\w+)\/?.*$/.exec(href)[1]; | |
ele.src = "//i.imgur.com/" + id + ".jpg"; | |
} | |
if (ele.src) { | |
entry.appendChild(ele); | |
} | |
} | |
ele.style.display = 'block'; | |
ele.onload = function () { | |
var ho = this.height; | |
var wo = this.width; | |
var hn = 800 | |
, wn = 800; | |
if (ho <= 800 && wo <= 800) return; | |
if (ho > wo) { | |
wn = Math.round(hn * wo / ho); | |
} else if (wo > ho) { | |
hn = Math.round(ho * wn / wo); | |
} | |
this.height = hn; | |
this.width = wn; | |
ele.onerror = function () { | |
var s = this.src; | |
this.src = s.replace(/^https:\/\//i, 'http://'); | |
this.onerror = null; | |
} | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment