Autoplay & Loop on Youtube (PC/Mobile)

You can autoplay and loop on youtube in all device.

As of 2020-04-10. See the latest version.

// ==UserScript==
// @name         Autoplay & Loop on Youtube (PC/Mobile)
// @namespace    http://tampermonkey.net/
// @version      1.61
// @description  You can autoplay and loop on youtube in all device.
// @author       Solitary Walker
// @match        https://*.youtube.com/*
// @grant        none
// @license           CC0
// ==/UserScript==

'use strict';

/*You only change follow flags if you change */
const IS_AUTOPLAY = true;
const IS_LOOP = true;
/******************************/

let playButton = document.getElementsByClassName("ytp-icon-large-play-button-hover")[0];
if(playButton != undefined && IS_AUTOPLAY) {
    playButton.click();
}

let observer = new MutationObserver(mutations => {
    const video = document.getElementsByTagName("video")[0];
    if(video != undefined) {
        function set() {
            video.autoplay = IS_AUTOPLAY;
            video.loop = IS_LOOP;
        }
        set();
        video.addEventListener("loadstart", event => set());
        observer.disconnect();
    }
})

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