Answer the question
In order to leave comments, you need to log in
Is it possible to make the code work more optimized?
Hello.
In general, I made such a code that converts a link to a youtube, vimeo and coub video into a player from these video hostings (if the latter can be called that).
I myself have been studying js for only 2 weeks, so I don’t know how to perform the following in a better way.
var $v1 = 'ССЫЛКА НА РОЛИК',
$v2;
// Для youtube
if($v1.indexOf('youtube.com') + 1 || $v1.indexOf('youtu.be') + 1) {
// Ищем и заменяем, чтобы проще обработать
$v2 = $v1.replace('.com/embed/', '.com/watch?v=');
$v1 = $v2.replace('.be/', '.com/watch?v=');
//Отсеиваем ненужные части, чтобы получить чистый id
$v2 = $v1.split(".com/watch?v=")[1];
$v1 = $v2.split("&index")[0];
$v2 = $v1.replace('&', '?');
//Получаем чистый id, вставляем его непосредственно в плеер
document.write('<iframe width="685" height="386" src="https://www.youtube.com/embed/' + $v2 + '" frameborder="0" allowfullscreen></iframe>');
}
// Для Coub
else if ($v1.indexOf('http://coub.com/') + 1) {
// Производим замену
$v2 = $v1.replace('/view/', '/embed/');
// Вставляем id в плеер
document.write('<iframe src="' + $v2 + '?muted=false&autostart=false&originalSize=false&hideTopBar=false&noSiteButtons=false&startWithHD=false" allowfullscreen="true" frameborder="0" width="685" height="386"></iframe>');
}
// Для vimeo
else if ($v1.indexOf('vimeo.com') + 1) {
// Производим замену
$v2 = $v1.replace('vimeo.com/', 'player.vimeo.com/video/');
// Вставляем id в плеер
document.write('<iframe src="' + $v2 +'" width="685" height="386" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>');
}
//В случае, если не удалось обработать
else {document.write('<div style="color:red">Ошибка: не могу воспроизвести.</div>');}
Answer the question
In order to leave comments, you need to log in
Don't skimp on variables, this is not assembler. In general, the dumbest thing is these pieces like
v1 = v2.something();
v2 = v1.somethingElse();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question