YouTube Music Smooth (Lazy Loader)

Lazy loader for Youtube Music, making it work smoothly. No lag :D

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Music Smooth (Lazy Loader)
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Lazy loader for Youtube Music, making it work smoothly. No lag :D
// @author       Emree.el on Instagram :)
// @match        https://music.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create YouTube Music item
    function createMusicItem(videoId) {
        return `<div class="placeholder" data-video-id="${videoId}"></div>`;
    }

    // Function to load YouTube Music items
    function loadMusicItems() {
        // Simulated list of YouTube video IDs
        const videoIds = [
            'VIDEO_ID_1',
            'VIDEO_ID_2',
            'VIDEO_ID_3',
            // Add more video IDs as needed
        ];

        // Create and append placeholder elements
        videoIds.forEach(videoId => {
            const item = createMusicItem(videoId);
            document.getElementById('contents').innerHTML += item;
        });

        // Create an intersection observer
        const observer = new IntersectionObserver(entries => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    // Load YouTube video when the placeholder is in view
                    const videoId = entry.target.getAttribute('data-video-id');
                    const iframe = document.createElement('iframe');
                    iframe.src = `https://www.youtube.com/embed/${videoId}`;
                    iframe.width = '560'; // Set iframe width
                    iframe.height = '315'; // Set iframe height
                    iframe.allowFullscreen = true; // Enable fullscreen
                    entry.target.appendChild(iframe);
                    // Stop observing once loaded
                    observer.unobserve(entry.target);
                }
            });
        });

        // Observe each placeholder element
        const placeholders = document.querySelectorAll('.placeholder');
        placeholders.forEach(placeholder => {
            observer.observe(placeholder);
        });
    }

    // Call the function to initially load music items
    loadMusicItems();
})();