YouTube Music Loop Toggle

Adds a checkbox bottom right, once enabled, it'll loop without it stopping

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         YouTube Music Loop Toggle
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Adds a checkbox bottom right, once enabled, it'll loop without it stopping
// @author       Emree.el on instagraaammmmmm :D
// @match        https://music.youtube.com/*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create and style the checkbox
    const createCheckbox = () => {
        const checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.style.position = 'fixed';
        checkbox.style.bottom = '100px'; // Further adjusted position
        checkbox.style.right = '20px';
        checkbox.style.zIndex = '1000';
        checkbox.id = 'yt-loop-checkbox';
        return checkbox;
    };

    // Load the saved state from local storage
    const loadState = () => {
        const checkbox = document.getElementById('yt-loop-checkbox');
        const savedState = localStorage.getItem('ytLoopState');
        checkbox.checked = savedState === 'true';
        return checkbox.checked;
    };

    // Save the state to local storage
    const saveState = (state) => {
        localStorage.setItem('ytLoopState', state);
    };

    // Handle the looping logic
    const handleLoop = () => {
        const checkbox = document.getElementById('yt-loop-checkbox');
        const player = document.querySelector('video');

        if (player) {
            player.loop = checkbox.checked;
        }
    };

    // Initialize script
    const init = () => {
        const checkbox = createCheckbox();
        document.body.appendChild(checkbox);

        // Load and apply the saved state
        const isLooping = loadState();
        checkbox.checked = isLooping;
        handleLoop();

        // Add event listener to handle checkbox changes
        checkbox.addEventListener('change', (e) => {
            const isChecked = e.target.checked;
            saveState(isChecked);
            handleLoop();
        });

        // Observe changes to the video element to apply looping
        const observer = new MutationObserver(() => {
            handleLoop();
        });

        observer.observe(document.body, { childList: true, subtree: true });
    };

    // Wait for the page to fully load before initializing
    window.addEventListener('load', init);
})();