Gadget

Apple Rilis iPhone Premium Lebih Dulu untuk Atasi Krisis Memori Global 2026

It seems like the content you’ve provided is a snippet of JavaScript, particularly code related to lazy loading a YouTube iframe. Here’s a rewritten version that maintains the original purpose while improving the readability and clarity:

// Function to create a lazy-loaded YouTube iframe
function lazyLoadYoutubeIframe() {
    var iframe = document.createElement("iframe");
    var videoSrc = this.parentNode.dataset.src + "?autoplay=1";

    // Append any additional query parameters if they exist
    if (this.parentNode.dataset.query.length > 0) {
        videoSrc += "&" + this.parentNode.dataset.query;
    }

    // Set attributes for the iframe
    iframe.setAttribute("src", videoSrc);
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("allowfullscreen", "1");
    iframe.setAttribute("allow", "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture");

    // Replace the parent node with the iframe
    this.parentNode.parentNode.replaceChild(iframe, this.parentNode);
}

// Function to lazy load the thumbnail for YouTube videos
function lazyLoadThumb(videoId, altText, isLazyLoad) {
    var thumbnailHtml = `${altText}`;
    return thumbnailHtml;
}

// Wait for the DOM to fully load
document.addEventListener("DOMContentLoaded", function() {
    var players = document.getElementsByClassName("rll-youtube-player");

    for (var i = 0; i < players.length; i++) {
        var player = players[i];
        var videoId = player.dataset.id;
        var altText = player.dataset.alt || "";
        var lazyLoad = player.dataset.lazy === "true";

        // Create a wrapper element for the thumbnail
        var thumbnail = document.createElement("div");
        thumbnail.setAttribute("data-id", videoId);
        thumbnail.setAttribute("data-query", player.dataset.query);
        thumbnail.setAttribute("data-src", player.dataset.src);
        thumbnail.innerHTML = lazyLoadThumb(videoId, altText, lazyLoad);

        player.appendChild(thumbnail);

        // Add click event to play the video
        var playButton = thumbnail.querySelector(".play");
        playButton.onclick = lazyLoadYoutubeIframe;
    }
});

This revised version improves readability, organizes the code into functions, and retains the core functionality of lazy loading a YouTube iframe with appropriate attributes.

➡️ Baca Juga: 12 Fitur Rahasia Android 15 Developer Mode 90% Gak Pernah Pake

➡️ Baca Juga: Tragedi Surat Siswa SD Gantung Diri di Ngada NTT: Pesan Terakhir untuk Mama

Related Articles

Back to top button