Browse Source

Replace links to youtube/vimeo with players

Ralfp 12 years ago
parent
commit
dda1a38d80
1 changed files with 42 additions and 2 deletions
  1. 42 2
      static/cranefly/js/cranefly.js

+ 42 - 2
static/cranefly/js/cranefly.js

@@ -54,7 +54,7 @@ $(function () {
 	$('.go-back').on('click', function (e) {
 	    history.go(-1)
 	})
-	
+
 	// Add labels to images
 	$('.markdown img').each(function() {
     $(this).addClass('img-rounded');
@@ -69,5 +69,45 @@ $(function () {
   $('.markdown img').one('error', function() {
   	$(this).after('<div class="md-img-error"><span>' + l_img_broken_msg + '</span></div>');
   	$(this).hide();
-	}); 
+	});
+	
+	// Automagically turn links into players
+	$('.markdown a').each(function() {
+		// Youtube link
+		var re = /watch\?v=([A-Za-z0-9]+)/;
+		if (re.test(this.href)) {
+			media_url = this.href.match(re);
+			$(this).replaceWith('<iframe width="480" height="360" src="http://www.youtube.com/embed/' + media_url[1] + '" frameborder="0" allowfullscreen></iframe>');
+			return;
+		}
+
+		// Youtube embed with start time
+		var re = /youtu.be\/([A-Za-z0-9]+)\?t=([A-Za-z0-9]+)/;
+		if (re.test(this.href)) {
+			media_url = this.href.match(re);
+			media_minutes = media_url[2].match(/([0-9]+)m/);
+			media_seconds = media_url[2].match(/([0-9]+)s/);
+			media_url[2] = 0;
+			if (media_minutes) { media_url[2] += (media_minutes[1] - 0) * 60; }
+			if (media_seconds) { media_url[2] += (media_seconds[1] - 0); }
+			$(this).replaceWith('<iframe width="480" height="360" src="http://www.youtube.com/embed/' + media_url[1] + '?start=' + media_url[2] + '" frameborder="0" allowfullscreen></iframe>');
+			return;
+		}
+		
+		// Youtube embed
+		var re = /youtu.be\/([A-Za-z0-9]+)/;
+		if (re.test(this.href)) {
+			media_url = this.href.match(re);
+			$(this).replaceWith('<iframe width="480" height="360" src="http://www.youtube.com/embed/' + media_url[1] + '" frameborder="0" allowfullscreen></iframe>');
+			return;
+		}
+
+		// Vimeo link
+		var re = /vimeo.com\/([0-9]+)/;
+		if (re.test(this.href)) {
+			media_url = this.href.match(re);
+			$(this).replaceWith('<iframe src="http://player.vimeo.com/video/' + media_url[1] + '?color=CF402E" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
+			return;
+		}
+	});
 })