A
A
Adam Sulumov2017-01-15 19:24:24
YouTube
Adam Sulumov, 2017-01-15 19:24:24

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&amp;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>');}

As you can see, only 2 variables are used. Basically, the script searches and replaces, filters out unnecessary characters, and gets a clean id that it embeds into the player. So many actions have been done for youtube so that the script supports absolutely all types of links to videos: with timecode, from a playlist, etc.
The code itself does the job.
Make comments, point out my mistakes to me.
Tell me how to do the same, only better.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2017-01-15
@k12th

Don't skimp on variables, this is not assembler. In general, the dumbest thing is these pieces like

v1 = v2.something();
v2 = v1.somethingElse();

Debugging is a nightmare, modifying is like walking through a minefield.
document.write is not very good. blocks the page, and very destructively. In general, these places are not very neat, and I want to create a function that takes a URL, creates an iframe and inserts it in the right place.
Instead of a bunch of split and replace, you can use regular expressions, especially in the case of YouTube.
Do not use $ for variables, this is not PHP (in extreme cases, you can only if such a variable contains a jQuery object)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question