From 14e417d72aeb7b2680e836428db75f83e832768b Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Fri, 12 Dec 2025 18:10:12 +0100 Subject: [PATCH] Should fix autoplay --- README.md | 2 +- ytmnd.js | 81 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 43adf8d..c475225 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ serving this scraper will download the gif and mp3 from a ytmnd and write a file embedding these things in addition to zoom text (if any). -The downloaded files cannot be loaded from a `file://` url. In order to view these files, put them online or run a local server. For example, `python -m http.server` from the directory and got to [http://localhost:8000/](http://localhost:8000/) +The downloaded files cannot be loaded from a `file://` url. In order to view these files, put them online or run a local server. For example, `python -m http.server` from the directory and got to [http://localhost:8000/](http://localhost:8000/). If you host them somewhere, remember to include `ytmnd.js` in the same directory. options ------- diff --git a/ytmnd.js b/ytmnd.js index ad26f42..ed80d77 100644 --- a/ytmnd.js +++ b/ytmnd.js @@ -1,42 +1,43 @@ (function () { - var AudioContext = window.AudioContext || window.webkitAudioContext; - var context = new AudioContext(); - var request = new XMLHttpRequest(); - var source; - var buffer; - - request.open("GET", url, true); - request.responseType = "arraybuffer"; - - request.onload = function () { - context.decodeAudioData( - request.response, - function (response) { - buffer = response; - - function playBuffer() { - source = context.createBufferSource(); - source.buffer = buffer; - source.connect(context.destination); - - source.onended = function () { - playBuffer(); - }; - - source.start(0); - } - - playBuffer(); - }, - function (error) { - console.error("Audio decoding failed:", error); - }, - ); - }; - - request.onerror = function () { - console.error("Failed to load audio file"); - }; - - request.send(); + var audio = new Audio(url); + audio.loop = true; + audio.muted = true; + audio + .play() + .then(function () { + console.log("Audio started (muted). Click/tap to unmute!"); + var unmuteMsg = document.createElement("div"); + unmuteMsg.textContent = "Click to unmute"; + unmuteMsg.style.cssText = + "position:fixed;top:10px;right:10px;background:rgba(0,0,0,0.8);color:#fff;padding:10px 20px;border-radius:5px;font-family:sans-serif;z-index:9999;cursor:pointer;"; + document.body.appendChild(unmuteMsg); + function unmute() { + audio.muted = false; + unmuteMsg.remove(); + console.log("Audio unmuted!"); + } + document.addEventListener("click", unmute, { once: true }); + document.addEventListener("keydown", unmute, { once: true }); + document.addEventListener("touchstart", unmute, { once: true }); + unmuteMsg.addEventListener("click", unmute, { once: true }); + }) + .catch(function (error) { + console.error("Autoplay failed even when muted:", error); + function playOnInteraction() { + audio.muted = false; + audio + .play() + .then(function () { + console.log("Audio started after user interaction"); + }) + .catch(function (err) { + console.error("Still couldn't play:", err); + }); + } + document.addEventListener("click", playOnInteraction, { once: true }); + document.addEventListener("keydown", playOnInteraction, { once: true }); + document.addEventListener("touchstart", playOnInteraction, { + once: true, + }); + }); })();