Editing User:Toad64/Sandbox

Jump to navigation Jump to search
Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
Using Javascript, It is possible to modify the default audio element used to play audio. This code replaces the standard webkit media player with a stylized version. (Currently for chromium browsers only)
Using Javascript (and a set of ::root colors i had), It is possible to modify the default audio element used to play audio.


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
Line 6: Line 6:
// In your Common.js
// In your Common.js
$(document).ready(function() {
$(document).ready(function() {
    // Find every native audio player generated by the wiki
     $('audio').each(function() {
     $('audio').each(function() {
         const $this = $(this);
         // Create your custom component
         const src = $this.attr('src');
         const myPlayer = document.createElement('audio-player');
          
          
         // Log what we found to the console so we can see why it's undefined
         // Transfer the source
         console.log("Processing element:", this);
         myPlayer.setAttribute('track', $(this).attr('src'));
        console.log("Source found:", src);
       
 
         // Replace the native player with your custom one
         if (src) {
        $(this).replaceWith(myPlayer);
            const myPlayer = document.createElement('audio-player');
            myPlayer.setAttribute('track', src);
            $this.replaceWith(myPlayer);
        } else {
            console.warn("Skipping element because src is undefined:", this);
        }
     });
     });
});
});
Line 33: Line 28:


       this.innerHTML = `
       this.innerHTML = `
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
   
         <style>
         <style>
           .player-controls {
           .player-controls {
Line 40: Line 33:
             align-items: center;
             align-items: center;
             gap: 12px;
             gap: 12px;
           
             width: 100%;
             width: 100%;
             max-width: 400px;
             max-width: 400px;
padding: 10px 15px;
            background: var(--box-color, #222);
            padding: 10px 15px;
             border-radius: 50px;
             border-radius: 50px;
           
             color: white;
            background: var(--background-color-base-subtle);
             color: var(--color-base);
             font-family: sans-serif;
             font-family: sans-serif;
            box-sizing: border-box;
overflow: hidden;
           }
           }
         
           .playBtn {
           .playBtn {
            flex-shrink: 0;
         
             width: 30px;
             width: 30px;
             height: 30px;
             height: 30px;
            background: var(--green-color, #4caf50);
             border: none;
             border: none;
            color: white;
             border-radius: 50%;
             border-radius: 50%;
           
            align-items: center;
    justify-content: center;
           
            background: var(--background-color-interactive);
            color: var(--color-base);
             cursor: pointer;
             cursor: pointer;
              
             flex-shrink: 0;
           }
           }
         
          .playBtn:hover {
        background: var(--background-color-interactive-subtle);
          }
         
           .seek {
           .seek {
        flex: 1;
            -webkit-appearance: none;
        -webkit-appearance: none;
             flex: 1;
             appearance: none;
           
             height: 6px;
             height: 6px;
            background: #7e7e7e;
             border-radius: 5px;
             border-radius: 5px;
           
            background: var(--color-base);
             cursor: pointer;
             cursor: pointer;
           }
           }
         
           .time { font-size: 12px; opacity: 0.8; white-space: nowrap; }
          .seek::-webkit-slider-thumb {
        -webkit-appearance: none;
        appearance: none;
       
        width: 15px;
        height: 15px;
        border-radius: 50px;
       
        background: var(--link-color);
        cursor: pointer;
      }
     
      .seek::-moz-range-thumb {
    -webkit-appearance: none;
        appearance: none;
       
        width: 15px;
        height: 15px;
        border-radius: 50px;
       
        background: var(--link-color);
        cursor: pointer;
          }
         
           .time {  
        font-size: 12px;  
        opacity: 0.8;  
        white-space: nowrap;  
          }
         </style>
         </style>
          
          
         <div class="player-controls">
         <div class="player-controls">
           <audio class="audio"><source src="${src}" type="${mime}"></audio>
           <audio class="audio"><source src="${src}" type="${mime}"></audio>
           <button class="playBtn"><i class="fa-solid fa-play"></i></button>
           <button class="playBtn"></button>
           <input class="seek" type="range" value="0" min="0" max="100">
           <input class="seek" type="range" value="0" min="0" max="100">
           <span class="time">0:00 / 0:00</span>
           <span class="time">0:00 / 0:00</span>
Line 131: Line 76:


       playBtn.addEventListener("click", () => {
       playBtn.addEventListener("click", () => {
         if (audio.paused) { audio.play(); playBtn.innerHTML = '<i class="fa-solid fa-pause"></i>'; }
         if (audio.paused) { audio.play(); playBtn.textContent = ''; }
         else { audio.pause(); playBtn.innerHTML = '<i class="fa-solid fa-play"></i>'; }
         else { audio.pause(); playBtn.textContent = ''; }
       });
       });


Line 156: Line 101:
</syntaxhighlight>
</syntaxhighlight>


In this example, the Old Deadlock Title Screen:
This code creates a styleized media player (in this example the Old Deadlock Title Screen):
 
[[File:Music menu lp.mp3]]
[[File:Music menu lp.mp3]]
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)