Advertisement
Guest User

Windows Media Player skin for YouTube โ€“ revision as of 04 June 2022 23:13 (UTC)

a guest
Jun 4th, 2022
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Windows Media Player skin for YouTube (desktop and mobile)
  2.  
  3. function start_WMPskin() {
  4.     var WMPskin = {};
  5.  
  6.     function isDomain(domain) {
  7.         if (window.location.hostname.search(domain) >= 0) return true; else return false;
  8.     }
  9.  
  10.     function appendChildWithID(tagName,tagID,parent_element) {
  11.         // default parent element to document.body if unspecified
  12.         if (parent_element === undefined) parent_element = document.body;
  13.         parent_element.appendChild(document.createElement(tagName)); // add div
  14.         parent_element.lastElementChild.id=tagID; // give it ID
  15.     }
  16.  
  17.     function findMediaOnPage() {
  18.         if (document.getElementsByTagName("audio")[0]) {
  19.             WMPskin.media_exists = true;
  20.             return document.getElementsByTagName("audio")[0];
  21.         }
  22.         if (document.getElementsByTagName("video")[0]) {
  23.             WMPskin.media_exists = true;
  24.             return document.getElementsByTagName("video")[0];
  25.         } else {
  26.             WMPskin.media_exists = false;
  27.             console.debug("No media player found on page.");
  28.         }
  29.     }
  30.  
  31.  
  32.     var symbol_play = "โ–ถ︎";
  33.     var symbol_pause= "ll"; // if rounded arial font unavailable: "❚ ❚";
  34.  
  35.  
  36.     var WMplayer_replica_HTML = `
  37.     <div class="wmp_bar placeholder"></div>
  38.     <div class="wmp_bar progressbar"></div>
  39.  
  40.     <div class="wmp_left">
  41.       <button class="wmpbutton title" id="wmp_title">Title</button>
  42.       <button class="wmpbutton timer" id="wmp_timer">00:00</button>
  43.     </div>
  44.  
  45.     <div class="wmp_center_1">
  46.       <button class="wmpbutton shuf">๐Ÿ”€๏ธ&#xFE0E;</button>
  47.       <button class="wmpbutton rpt">๐Ÿ”„&#xFE0E;</button>
  48.       <button class="wmpbutton stop">โน&#xFE0E;</button>
  49.  
  50.         <div class="wmp_center_2">
  51.           <button class="wmpbutton rew" id="wmp_rew">โ—€โ—€</button>
  52.           <button class="wmpbutton play" id="wmp_play">โ–ถ๏ธŽ</button>
  53.           <button class="wmpbutton fwd" id="wmp_fwd">โ–ถ๏ธŽโ–ถ๏ธŽ</button>
  54.         </div>
  55.  
  56.       <button class="wmpbutton vol" id="wmp_speaker">๐Ÿ”Š&#xFE0E;</button>
  57.       <div class="wmpbutton vol_slider"></div>
  58.     </div>
  59.  
  60.  
  61.     <style>
  62.  
  63.     #wmplayer_replica button {
  64.         border:none;
  65.         background:none;
  66.         color:white;
  67.         cursor:pointer;
  68.     }
  69.  
  70.     #wmplayer_replica {
  71.         height:70px;
  72.         padding:10px;
  73.         color: white;
  74.         background-color: #223;
  75.         font-family:arial,'segoe ui';
  76.  
  77.         bottom:0;
  78.         width:100%;
  79.         text-align:center;
  80.         position:absolute;
  81.         display:flex;
  82.         background-image: linear-gradient(to bottom, #445 0%, #112 40%, #001 90%, #347 100%);
  83.     }
  84.  
  85.     .wmp_bar {
  86.         position:absolute;
  87.         top:-5px;
  88.         left:0px;
  89.         width:100%;
  90.         height:5px;
  91.         display:block;
  92.         background-color: #49F;
  93.         background-image: linear-gradient(to left, #AEF 0px, #49F 30px);
  94.         cursor:pointer;
  95.     }
  96.  
  97.     .wmp_bar.placeholder {
  98.         background-image:unset;
  99.         background-color:grey;
  100.         opacity:0.5;
  101.     }
  102.  
  103.     .wmp_bar.progressbar {
  104.         pointer-events: none;
  105.     }
  106.  
  107.     .wmp_left {
  108.         display:inline-block; position:relative; top:15px;
  109.         width:30%;
  110.     }
  111.  
  112.     .wmpbutton.title {
  113.         display:inline-block;
  114.         max-width:80%;
  115.         text-align:right;
  116.         margin-left:auto;
  117.         text-overflow: ellipsis;
  118.         overflow: hidden;  
  119.         white-space: nowrap;
  120.     }
  121.  
  122.     .wmpbutton.timer { margin-left:10px; }
  123.  
  124.     .wmp_center_1 button { display:inline-block; margin-right:5px;  }
  125.  
  126.     .wmpbutton { display:inline-block; font-size: 20px; }
  127.     #wmplayer_replica .wmpbutton.play {
  128.         display:inline-block;
  129.         font-size:30px;
  130.         margin:auto;
  131.         padding-bottom:5px;
  132.         padding-left:8px;
  133.         align-content: center;
  134.         background-color: #15C;
  135.         background-image: linear-gradient(to bottom, #abe 0%, #36c 49%, #129 50%, #8ef 100%);
  136.         border-radius: 100px;
  137.         width:50px; height:50px;
  138.         box-shadow: 0 0 5px white;
  139.         margin:0 40px;
  140.         transform:scale(1.3);
  141.         font-family:'arial rounded mt bold',arial,helvetica,roboto,sans-serif;
  142.         font-weight:bold;
  143.     }
  144.     .wmp_center_1 {
  145.         display:block;
  146.         margin:auto;
  147.         border-radius: 100px;
  148.         padding:5px 0;
  149.         vertical-align:middle;
  150.         box-shadow: 0 0 2px black;
  151.         width:600px;
  152.         background-image: linear-gradient(to bottom, #777 0%, #334 49%, #111 50%, #001 100%);
  153.         box-shadow: 0 0 2px white;
  154.     }
  155.  
  156.     .wmp_center_2 {
  157.         display:inline-block;
  158.         background-color: #345;
  159.         background-image: linear-gradient(to bottom, #abc 0%, #568 49%, #001 50%, #023 100%);
  160.         width:300px;
  161.         border-radius: 1000px;
  162.     }
  163.  
  164.     .wmpbutton.vol_slider {
  165.         width:100px;
  166.         height:5px;
  167.         background-color:#26F;
  168.         border-radius: 100px;
  169.  
  170.         position: relative;
  171.         top: -5px;
  172.     }
  173.  
  174.     #wmplayer_replica button:hover { filter: brightness(1.2); }
  175.     #wmplayer_replica button:active  { filter: brightness(0.6); }
  176.     </style>
  177.     ` ;
  178.  
  179.  
  180.     // domain fallback
  181.         WMPskin.video = findMediaOnPage();
  182.     if (isDomain("www.youtube.com")) {
  183.         WMPskin.domain_valid = true;
  184.         WMPskin.media_placeholder = document.getElementById("player-container-outer");
  185.     } else if (isDomain("m.youtube.com")) {
  186.         WMPskin.media_placeholder = document.getElementById("player-container-id");
  187.     } else {
  188.         WMPskin.domain_valid = false;
  189.         WMPskin.media_placeholder = WMPskin.video.parentElement; // generic fallback
  190.         console.warn("WMPskin: unsupported domain. Currently, it only works on YouTube.");
  191.     }
  192.  
  193.     // insert player interface
  194.     appendChildWithID("div","wmplayer_replica",WMPskin.media_placeholder);
  195.     document.getElementById("wmplayer_replica").innerHTML = WMplayer_replica_HTML;
  196.  
  197.     // system variables
  198.         WMPskin.div = document.getElementById("wmplayer_replica");
  199.         WMPskin.timer = document.getElementById("wmp_timer");
  200.         WMPskin.timer_mode = 0;
  201.         WMPskin.title = document.getElementById("wmp_title");
  202.         WMPskin.bar_placeholder = document.getElementsByClassName("wmp_bar")[0];
  203.         WMPskin.bar = document.getElementsByClassName("wmp_bar")[1];
  204.         WMPskin.play = document.getElementById("wmp_play");
  205.         WMPskin.rew = document.getElementById("wmp_rew");
  206.         WMPskin.fwd = document.getElementById("wmp_fwd");
  207.         WMPskin.speaker = document.getElementById("wmp_speaker");
  208.         WMPskin.domain_valid = true;
  209.  
  210.  
  211.     WMPskin.updateTimer = function(pause_override) {
  212.         if ( WMPskin.media_exists && ( ! WMPskin.video.paused || pause_override )  ) {
  213.             WMPskin.updatePlayButton();
  214.             WMPskin.bar.style.width = (WMPskin.video.currentTime / WMPskin.video.duration)*100+"%";
  215.             switch(WMPskin.timer_mode) {
  216.                 case 0: WMPskin.timer.innerHTML = mediaTimer(WMPskin.video.currentTime); break;
  217.                 case 1: WMPskin.timer.innerHTML = "-"+mediaTimer(WMPskin.video.duration-WMPskin.video.currentTime); break;
  218.                 case 2: WMPskin.timer.innerHTML = mediaTimer(WMPskin.video.currentTime)+" / "+ mediaTimer(WMPskin.video.duration); break;
  219.             }
  220.         }
  221.             WMPskin.title.innerHTML = document.title;
  222.         if ( isDomain("youtube.com") ) WMPskin.title.innerHTML = decodeURI(WMPskin.title.innerHTML.substring(0,WMPskin.title.innerHTML.search(/(-|โ€“)(?:.(?!(-|โ€“)))+$/)-1 ) ); // remove "- YouTube"
  223.         if ( ! WMPskin.media_exists && ! WMPskin.updateTimer_no_player_alerted ) {
  224.             console.log("Windows Media Player skin could not find a media element on page.");
  225.             WMPskin.updateTimer_no_player_alerted = true;
  226.         }
  227.     };
  228.  
  229.     var media_time = {};
  230.  
  231.     function mediaTimer_core(seconds) {
  232.  
  233.         if ( seconds >= 3600 ) {
  234.             // hours
  235.             media_time.HH = Math.floor( seconds/3600 );
  236.         }
  237.  
  238.         // minutes
  239.         media_time.MM = Math.floor( seconds/60%60 );
  240.         // leading zero
  241.         if ( seconds%3600 < 600 ) media_time.MM = "0" + media_time.MM;
  242.  
  243.         // seconds
  244.         media_time.SS = Math.floor( seconds%60 );
  245.         // leading zero
  246.         if ( seconds%60 < 10 ) media_time.SS = "0" + media_time.SS;
  247.  
  248.         if ( seconds >= 3600 ) {
  249.             return  media_time.HH+":"+media_time.MM+":"+media_time.SS;
  250.         } else { return media_time.MM+":"+media_time.SS; }
  251.     }
  252.  
  253.     function mediaTimer(seconds) {
  254.         if (seconds >= 0) return mediaTimer_core(seconds); // zero or positive
  255.         if (seconds < 0) // negative
  256.             {
  257.              seconds = seconds * (-1);
  258.              return "-"+mediaTimer_core(seconds);
  259.             }
  260.         if (seconds == undefined || isNaN(seconds) ) return "โ€“&thinsp;โ€“:โ€“&thinsp;โ€“";
  261.  
  262.     }
  263.     function togglePlay(media) {
  264.         if (media) {
  265.             media.paused ? media.play() : media.pause();
  266.         } else return false;
  267.     }
  268.  
  269.     WMPskin.updateInterval = setInterval(WMPskin.updateTimer, 200);
  270.  
  271.         WMPskin.clickSeekBar = function(m){
  272.             if (WMPskin.video) {
  273.                 var clickPos = m.clientX / WMPskin.bar_placeholder.offsetWidth;
  274.                 console.debug("WMPlayer skin: clickPos: " + clickPos);
  275.                 WMPskin.video.currentTime = WMPskin.video.duration * clickPos;
  276.                 WMPskin.updateTimer(true);
  277.             }
  278.         };
  279.  
  280.     WMPskin.bar_placeholder.addEventListener("mousedown", WMPskin.clickSeekBar );
  281.  
  282.     WMPskin.fwd.onclick = function() { WMPskin.video.currentTime+=30; WMPskin.updateTimer(true); };
  283.     WMPskin.rew.onclick = function() { WMPskin.video.currentTime-=10; WMPskin.updateTimer(true); };
  284.  
  285.     WMPskin.updatePlayButton = function() {
  286.         if (WMPskin.video.paused) {
  287.             WMPskin.play.innerHTML=symbol_play;
  288.         } else {
  289.             WMPskin.play.innerHTML=symbol_pause+"&nbsp;";
  290.         }
  291.     };
  292.  
  293.  
  294.     WMPskin.play.onclick = function() {
  295.         togglePlay(WMPskin.video);
  296.         WMPskin.updatePlayButton();
  297.     };
  298.     WMPskin.speaker.onclick = function() {
  299.         if (WMPskin.video.muted) {
  300.             WMPskin.video.muted = 0;
  301.             WMPskin.speaker.innerHTML="๐Ÿ”Š&#xFE0E;";
  302.         } else {
  303.             WMPskin.video.muted = 1;
  304.             WMPskin.speaker.innerHTML="๐Ÿ”‡&#xFE0E;";
  305.         }
  306.     };
  307.  
  308.     WMPskin.timer.onclick = function() {
  309.             switch(WMPskin.timer_mode) {
  310.                 case 0: WMPskin.timer_mode = 1; break;
  311.                 case 1: WMPskin.timer_mode = 2; break;
  312.                 case 2: WMPskin.timer_mode = 0; break;
  313.             }
  314.             WMPskin.updateTimer(true);
  315.     };
  316.  
  317.     // to avoid missing out on media element
  318.     WMPskin.findPlayerInterval = setInterval( function(){
  319.         WMPskin.video=findMediaOnPage();
  320.             WMPskin.updatePlayButton();
  321.     }, 2000);
  322.  
  323.  
  324.     // full screen optimizations
  325.  
  326.     window.addEventListener('resize', function() {
  327.         if (window.innerWidth > 1500) {
  328.             if ( ! document.getElementById("WMPskin_fullscreen_style") ) {
  329.                 appendChildWithID("style","WMPskin_fullscreen_style",WMPskin.div);
  330.             }
  331.             document.getElementById("WMPskin_fullscreen_style").innerHTML = ".wmp_center_1 { position:absolute; left:50%; margin-left:-250px;} .wmp_left { text-align: left; } #wmplayer_replica { min-height:80px;}";
  332.         }  
  333.  
  334.         if (window.innerWidth < 1500 && document.getElementById("WMPskin_fullscreen_style") ) {
  335.             document.getElementById("WMPskin_fullscreen_style").outerHTML="";
  336.         }
  337.  
  338.     } );
  339. }
  340.  
  341. window.addEventListener('load', start_WMPskin);
  342.  
  343. // patch
  344. var start_WMPskin_retry = setInterval(function(takedownWarningPolymer){
  345.     if(document.readyState == "complete" && ! document.getElementById("wmplayer_replica") )
  346.         start_WMPskin();
  347.         if (document.getElementById("wmplayer_replica") ) { clearInterval(start_WMPskin_retry); }
  348. }, 1000);
  349.  
  350. window.addEventListener('click', function(){ if ( ! document.getElementById("wmplayer_replica") ) start_WMPskin(); } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement