// Get all YouTube iframes on the page
const youtubeIframes = document.querySelectorAll('iframe[src*="youtube.com/embed"]');
// Loop through each iframe and modify its src attribute
youtubeIframes.forEach(iframe => {
let src = iframe.src;
// Check if autoplay is already present, if not, add it
if (!src.includes('autoplay=1')) {
src += (src.includes('?') ? '&' : '?') + 'autoplay=1';
}
// Check if mute is present, if not, add it (highly recommended for autoplay)
if (!src.includes('mute=1')) {
src += (src.includes('?') ? '&' : '?') + 'mute=1';
}
// Update the iframe's src
iframe.src = src;
// Add or ensure the "allow" attribute includes autoplay
if (!iframe.hasAttribute('allow') || !iframe.getAttribute('allow').includes('autoplay')) {
iframe.setAttribute('allow', 'autoplay; ' + (iframe.hasAttribute('allow') ? iframe.getAttribute('allow') : ''));
}
});