Explorar el Código

YouTube_regex

Iryna Bolbat hace 2 años
padre
commit
22de3294dc
Se han modificado 1 ficheros con 15 adiciones y 1 borrados
  1. 15 1
      work in class/socketchat/index.html

+ 15 - 1
work in class/socketchat/index.html

@@ -18,11 +18,25 @@
                 let p = document.createElement('p');
                 p.innerHTML = `${msg.nick} : ${msg.message}`; 
                 document.body.appendChild(p);
+                console.log(p);
+                getyoutubeRegex(msg.message);
             });
 
+            function getyoutubeRegex(adressVideo){
+                const youtubeRegex = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/;
+                const matchStr  = adressVideo.match(youtubeRegex);
+                if(matchStr){
+                    const iframeNew = document.createElement("iframe");
+                    iframeNew.setAttribute("src", `https://www.youtube.com/embed/${matchStr[1]}`);
+                    iframeNew.style.width = "300px";
+                    iframeNew.style.height = "250px";
+                    document.body.appendChild(iframeNew);
+                }
+            }
+
             btn.onclick = () => {
                 socket.emit('msg', {nick: nick.value, message: message.value});
-            } ;
+            } ; 
         </script>
     </body>
 </html>