tao5858 / komica embed youtube

// ==UserScript==
// @name         komica embed youtube
// @version      1.5.6
// @description  Embed youtube videos on komica
// @author       taoko
// @match      http*://*.komica1.org/*
// @match      http*://*.komica2.cc/*
// @grant        none
// @license MIT
// @updateURL https://openuserjs.org/meta/tao5858/komica_embed_youtube.meta.js
// @downloadURL https://openuserjs.org/install/tao5858/komica_embed_youtube.user.js
// ==/UserScript==

(function () {
  'use strict';
  document.head.insertAdjacentHTML("beforeend", "<style type='text/css'> .resizable { resize: both; overflow: hidden;  padding: 15px; border: medium none; } </style>");

  var as = document.getElementsByTagName('a');
  var ab = document.getElementsByTagName('span');
  var ac = document.getElementsByClassName('-youtube-thumb');
  var id1;
  var id2;
  var id3;
  for (var i = 0; i < as.length; i++) {
    var a = as[i];
    if (a.href.indexOf('youtube.com') != -1 || a.href.indexOf('youtu.be') != -1) {
      if (a.href.indexOf('list') != -1) {
        if (a.href.indexOf('playlist') != -1) {
          continue;
        }
        id2 = a.href.split('list=')[1];
        if (a.href.indexOf('youtu.be') != -1) {
          id3 = a.href.split('be/')[1].substr(0, 11) + '?rel=0';
        }
        else {
          id3 = a.href.split('v=')[1].substr(0, 11);
        }
        id1 = (id3 + '?list=' + id2);
      }
      else if (a.href.indexOf('watch') != -1) {
        id1 = a.href.split('v=')[1].substr(0, 11) + '?rel=0';
      }
      else if (a.href.indexOf('be/') != -1) {
        id1 = a.href.split('be/')[1].substr(0, 11) + '?rel=0';
      }
      else if (a.href.indexOf('embed/') != -1) {
        id1 = a.href.split('embed/')[1].substr(0, 11) + '?rel=0';
      }
      else if (a.href.indexOf('shorts/') != -1) {
        id1 = a.href.split('shorts/')[1].substr(0, 11) + '?rel=0';
      }
      else if (a.href.indexOf('live/') != -1) {
        id1 = a.href.split('live/')[1].substr(0, 11) + '?rel=0';
      }
      else {
        continue;
      }
      if (id1.length < 11) {
        continue;
      }
      //var parent = a.parentNode;
      var iframe = document.createElement('iframe');
      //parent.appendChild(iframe);
      a.parentNode.insertBefore(iframe, a);
      a.innerHTML = '';
      iframe.width = 640;
      iframe.height = 360;
      iframe.setAttribute('allowFullScreen', 'allowFullScreen');
      iframe.setAttribute('allow', 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture');
      iframe.frameBorder = '0';
      iframe.src = 'https://www.youtube-nocookie.com/embed/' + id1;
    }
    if (a.href.indexOf('dailymotion.com') != -1 || a.href.indexOf('dai.ly') != -1) {
      if (a.href.indexOf('video') != -1) {
        id1 = a.href.split('video/')[1];
      }
      else if (a.href.indexOf('ly/') != -1) {
        id1 = a.href.split('ly/')[1];
      }
      else {
        continue;
      }
      var miframe = document.createElement('iframe');
      a.parentNode.insertBefore(miframe, a);
      a.innerHTML = '';
      miframe.width = 640;
      miframe.height = 360;
      miframe.setAttribute('allowfullscreen', 'true');
      miframe.frameBorder = '0';
      miframe.src = '//www.dailymotion.com/embed/video/' + id1;
    }
    if (a.href.indexOf('twitch.tv') != -1) {
      var parent = a.parentNode;
      var tiframe = document.createElement('iframe');
      a.parentNode.insertBefore(tiframe, a);
      a.innerHTML = '';
      id2 = '&time=0h0m0s';
      if (a.href.indexOf('?') != -1) {
        if (a.href.indexOf('t=') != -1) {
          id2 = '&time=' + a.href.split('t=')[1];
        }
        id1 = a.href.split('?')[0];
      }
      else {
        id1 = a.href;
      }
      if (id1.indexOf('videos') != -1) {
        id1 = id1.split('videos/')[1];
        tiframe.src = '//player.twitch.tv/?video=v' + id1 + '&parent=' + window.location.hostname + '&autoplay=false' + id2;
      }
      else if (id1.indexOf('clip') != -1) {
        if (id1.indexOf('clips') != -1) {
          id1 = id1.split('tv/')[1];
        }
        else {
          id1 = id1.split('clip/')[1];
        }
        tiframe.src = '//clips.twitch.tv/embed?clip=' + id1 + '&parent=' + window.location.hostname + '&autoplay=false';
      }
      else {
        id1 = id1.split('tv/')[1];
        tiframe.src = '//player.twitch.tv/?channel=' + id1 + '&parent=' + window.location.hostname + '&autoplay=false';
      }
      tiframe.width = 640;
      tiframe.height = 360;
      tiframe.setAttribute('allowfullscreen', 'true');
      tiframe.setAttribute('scrolling', 'no');
      tiframe.frameBorder = '0';
    }
    if (a.href.indexOf('.webm') != -1) {
      if (as[i].href != as[i + 1].href) {
        var webm = document.createElement('video');
        if (as[i].href.indexOf('komica1.org') != -1) {
          id1 = a.href.split('src/')[1];
          id3 = as[i].href.split('/');
          id2 = id1.split('.webm')[0];
          webm.setAttribute('poster', '//' + id3[2] + '/' + id3[3] + '/thumb/' + id2 + 's.jpg');
          webm.setAttribute('preload', 'none');
        }
        else if (as[i].href.indexOf('komica2.net') != -1) {
          id1 = a.href.split('src/')[1];
          id3 = as[i].href.split('/');
          id2 = id1.split('.webm')[0];
          webm.setAttribute('poster', '//' + id3[2] + '/' + id3[3] + '/thumb/' + id2 + 's.jpg');
          webm.setAttribute('preload', 'none');
        }
        else if (as[i].href.indexOf('4chan.org') != -1 || as[i].href.indexOf('i.4cdn.org') != -1) {
          id2 = as[i].href.split('.webm')[0];
          webm.setAttribute('poster', id2 + 's.jpg');
          webm.setAttribute('preload', 'none');
        }
        else if (as[i].href.indexOf('webmshare.com') != -1) {
          id1 = a.href.split('com/')[1];
          id2 = id1.split('.webm')[0];
          webm.setAttribute('poster', '//s1.webmshare.com/t/' + id2 + '.jpg');
          webm.setAttribute('preload', 'none');
        }
        else {
          webm.setAttribute('preload', 'metadata');
        }
        a.parentNode.insertBefore(webm, a);
        a.innerHTML = '';
        webm.src = a.href;
        webm.width = 640;
        webm.height = 360;
        webm.setAttribute('controls', 'true');
        webm.setAttribute('allowfullscreen', 'allowFullScreen');
        webm.frameBorder = '0';
        webm.volume = 0.15;
      }
      else {
        continue;
      }
    }
    if (a.href.indexOf('vimeo.com') != -1) {
      var viframe = document.createElement('iframe');
      id1 = a.href.split('vimeo.com/')[1];
      //console.log(id);
      if (id1.length !== 0) {
        a.parentNode.insertBefore(viframe, a);
        a.innerHTML = '';
        viframe.src = '//player.vimeo.com/video/' + id1;
        viframe.width = 640;
        viframe.height = 360;
        viframe.setAttribute('allowFullScreen', 'allowFullScreen');
      }
    }
    if (a.href.indexOf('store.steampowered.com/app') != -1) {
      var siframe = document.createElement('iframe');
      id1 = a.href.split('/')[4];
      a.parentNode.insertBefore(siframe, a);
      siframe.src = '//store.steampowered.com/widget/' + id1;
      siframe.width = 640;
      siframe.height = 190;
      siframe.frameBorder = '0';
      a.innerHTML = '<br/>商店頁面';
    }
    if (a.href.indexOf('twitter.com') != -1 || a.href.indexOf('x.com') != -1 && a.href.indexOf('/status/') != -1) {
      var newDiv = document.createElement("div");
      a.parentNode.insertBefore(newDiv, a);
      newDiv.className = "resizable";
      newDiv.style.width = "550px";
      newDiv.style.height = "400px";
      var biframe = document.createElement('iframe');
      newDiv.appendChild(biframe);
      if ( a.href.indexOf('twitter.com') != -1){
          id1 = a.href.split('twitter.com/')[1];
      }else{
          id1 = a.href.split('x.com/')[1];
      }
      biframe.src = 'https://twitframe.com/show?url=https://twitter.com/' + id1;
      biframe.width = '100%';
      biframe.height = '100%';
      biframe.frameBorder = '0';
      a.innerHTML = '';
    }
    if (a.href.indexOf('nicovideo.jp/watch/') != -1) {
      if (a.href.indexOf('/watch/mg') != -1) {
        continue;
      }
      var niframe = document.createElement('iframe');
      id1 = a.href.split('watch/')[1];
      a.parentNode.insertBefore(niframe, a);
      if (a.href.indexOf('live') != -1){
            niframe.src = '//live.nicovideo.jp/embed/' + id1;
            niframe.width = 312;
            niframe.height = 176;
            niframe.setAttribute('allowFullScreen', 'true');
            niframe.style = "border:solid 1px #d0d0d0; background-color: #f6f6f6;";
            niframe.scrolling = "no"
        }else{
            niframe.src = '//embed.nicovideo.jp/watch/' + id1;
            niframe.width = 640;
            niframe.height = 360;
        }
      niframe.frameBorder = '0';
      a.innerHTML = '';
      //console.log(id);
    }
    if (a.href.indexOf('facebook.com') != -1 && a.href.indexOf('videos') != -1 || a.href.indexOf('watch/?v') != -1) {
      var fiframe = document.createElement('iframe');
      a.parentNode.insertBefore(fiframe, a);
      fiframe.src = 'https://www.facebook.com/plugins/video.php?height=360&href=' + a.href + '&show_text=false&width=640&t=0';
      fiframe.frameBorder = '0';
      fiframe.width = 640;
      fiframe.height = 360;
      a.innerHTML = '';
      // console.log(id);
    }
    
    if(a.href.indexOf('bilibili.com/video/') != -1 || a.href.indexOf('bilibili.com/s/video/') != -1){
      var ciframe = document.createElement('iframe');
      id1 = a.href.split('video/')[1].substr(0, 12);
      a.parentNode.insertBefore(ciframe, a);
      ciframe.src = 'https://player.bilibili.com/player.html?bvid=' +id1 + '&autoplay=0';
      ciframe.width = 640;
      ciframe.height = 360;
      ciframe.frameBorder = '0';
      ciframe.scrolling = "no";
      ciframe.setAttribute('allowFullScreen', 'true');
      ciframe.setAttribute('framespacing', '0');
      a.innerHTML = '<br>' + a.innerHTML;
    }
    
    //console.log(id);
    //console.log(a);
  }
  for (var i2 = 0; i2 < ac.length; i2++) {
    var a2 = ac[i2];
    a2.innerHTML = '';
  }
  for (var i3 = 0; i3 < ab.length; i3++) {
    var a3 = ab[i3].innerText;
    if (a3.indexOf('有回應') != -1) {
      i3++;
    }
    else {
      if (a3.indexOf('[展開]') != -1) {
        ab[i3].innerHTML = '';
      }
    }
  }
})();