Skip to content

Instantly share code, notes, and snippets.

@grobertson
Created June 10, 2015 15:59
Show Gist options
  • Save grobertson/9801c198da78a3e9c35e to your computer and use it in GitHub Desktop.
Save grobertson/9801c198da78a3e9c35e to your computer and use it in GitHub Desktop.
$('#id_video_id').live('paste', function() {
var onFail = function(found) {
alert('Unsupported video service or invalid URL');
console.log(found);
}
var element = this;
var parser = document.createElement('a');
setTimeout(function () {
parser.href = $(element).val();
var videoId = new String();
if (parser.hostname === "youtu.be") {
videoId = parser.pathname.replace('/', '');
$(element).val(videoId);
$("#id_service").val('Y');
}else if(parser.hostname === "youtube.com" || parser.hostname === "www.youtube.com" ) {
var matches = parser.search.match(/v\=([A-Za-z0-9\-]+)/);
if (matches[1]) {
videoId = matches[1];
$(element).val(videoId);
$("#id_service").val('Y');
} else {
onFail(parser.href);
}
} else {
onFail(parser.href);
}
}, 300);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment