// ==UserScript==
// @name SMWC YouTube Embed (w/thumbnails)
// @namespace SMWCentral
// @description Replaces *all* YouTube video links on SMWC with thumbnails that turn into embedded videos when clicked
// @include http://www.smwcentral.net/?p=viewthread&t=*
// @include http://www.smwcentral.net/?p=post&do=thread&f=*
// @include http://www.smwcentral.net/?p=post&do=reply&t=*
// @include http://www.smwcentral.net/?p=pm
// @include http://www.smwcentral.net/?p=pm&id=*
// @include http://www.smwcentral.net/?p=postsbyuser&*
// @icon http://bin.smwcentral.net/u/7478/SMWC_YouTube_Embed_%2528wthumbnails%2529.png
// @downloadURL http://bin.smwcentral.net/u/7478/SMWC_YouTube_Embed_%2528wthumbnails%2529.user.js
// @version 1.02
// @author ShadowFan-X
// @grant none
// ==/UserScript==
function getYTLinkId(l) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = l.match(regExp);
if (match && match[7].length == 11){
return match[7];
} else {
return null;
}
}
function getYTLinkTime(ytl)
{
var start;
var end;
var queryStart = ytl.indexOf('?') + 1;
var queryEnd = ytl.indexOf('#');
var query = ytl.substring(queryStart, queryEnd > -1 ? queryEnd : undefined);
if (queryStart && (queryEnd < 1 || queryStart < queryEnd)) {
var rgx = /(?:&|^)([^&=]+)=([^&]*)/g;
var match
while (match = rgx.exec(query)) {
if (match[1] === 'start') start = match[2];
else if (match[1] === 'end') end = match[2];
}
}
if (++queryEnd && queryEnd < ytl.length) {
query = ytl.substring(queryEnd);
if (query) {
var rgx = /^t=(?:((?:\d*\.)?\d+)h)?(?:((?:\d*\.)\d+)m)?(?:((?:\d*\.)\d+)s)?/;
var match = query.match(rgx);
if (match) {
start = ((+match[1] * 3600 || 0) + (+match[2] * 60 || 0) + (+match[3] || 0)) | 0;
}
}
}
return { start: start, end: end };
}
function embedYouTubeLink(xxx, o) {
// don't put embeds on the sidebar
if ($(o).parents('.postsidebar').length) return;
var id = getYTLinkId(o.href);
if (!id) {
if (console && typeof(console.warn) === 'function') {
console.warn('The URL "' + o.href + '" is not a YouTube URL.');
}
return;
}
o.title = 'textContent' in o ? o.textContent : 'innerText' in o ? o.innerText : o.innerHTML;
o.vid = id;
while (o.firstChild) o.removeChild(o.firstChild);
o.className = "yt-thumb";
o.style.backgroundImage = "url(//i1.ytimg.com/vi/" + id + "/0.jpg)";
o.innerHTML = '';
o.onclick = replaceWithIFrame;
}
function replaceWithIFrame(e) {
e.preventDefault();
var o = this;
var p = document.createElement('iframe');
var para = getYTLinkTime(o.href);
if (para) {
var s = para.start, e = para.end;
para = '';
if (s) para += '&start=' + s;
if (e) para += '&end=' + e;
} else para = '';
p.setAttribute('src', '//www.youtube.com/embed/' + o.vid + '?autoplay=1' + para);
p.setAttribute('allowfullscreen', 1);
p.style.width = "480px";
p.style.height = "360px";
p.style.border = "none";
o.parentNode.replaceChild(p, o);
return false;
}
(function(css) {
var sh = document.createElement('style');
sh.type = "text/css";
$('head').append(sh);
if (sh.styleSheet) {
sh.styleSheet.cssText = css;
} else {
sh.innerHTML = css;
}
}('.yt-thumb{display:inline-block;outline:0;position:relative;width:480px;height:360px}.yt-btn{position:absolute;width:96px;height:68px;top:146px;left:192px}.yt-btn.red{transition:opacity .2s linear,visibility .2s linear;visibility:hidden;opacity:0}.yt-thumb:hover>.yt-btn.red{visibility:visible;opacity:1}'));
$('a[href^="//www.youtube.com/"], a[href^="http://www.youtube.com/"], a[href^="https://www.youtube.com/"], a[href^="//youtu.be/"], a[href^="http://youtu.be/"], a[href^="https://youtu.be/"]').each(embedYouTubeLink);