Autoplay & Loop on Youtube (PC/Mobile)

You can autoplay and loop on youtube in all device.

As of 2020-06-25. See the latest version.

// ==UserScript==
// @name         Autoplay & Loop on Youtube (PC/Mobile)
// @namespace    http://tampermonkey.net/
// @version      1.70
// @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();
}

const id = setInterval(() => {
    const video = document.getElementsByTagName("video")[0];
    if(video != undefined) {
        clearInterval(id);
        function set() {
            video.autoplay = IS_AUTOPLAY;
            video.loop = IS_LOOP;
        }
        set();
        video.addEventListener("loadstart", event => set());
    }
},10000);