DitherSky / Clean YouTube Loopy

// ==UserScript==
// @name           Clean YouTube Loopy
// @version        2016.09.12
// @description    Displays a link below YouTube videos to enable/disable auto replay.
// @include        http*.youtube.com/watch?*v=*
// @include        http*.youtube.com/watch?*videos=*
// @include        http*.youtube.com/watch#!*videos=*
// @include        http*.youtube.com/watch#!*v=*
// @include        http*.youtube.com/watch?*NR=*
// @exclude        http*.youtube.com*&list=*
// @author         Dither (fixes), Rowen, QuaraMan (embed code)
// @run-at         document-start
// ==/UserScript==

(function () {

var ytPlayer,
    ytLoop = false,
// Buttony stuff
    lpConOff = "LoopyOff",
    lpConOn = "LoopyOn";

var base = document.createElement("span");
var loopy = document.createElement("button");
loopy.id = "eLoopy";
loopy.onclick = function onClickHandler() {
    var content = document.getElementById("loopyContent"),
        button = document.getElementById("loopyButton");
    if (ytLoop) {
        //if (typeof ytPlayList != "undefined") setCookie("LoopyPL", null);
        button.setAttribute("data-tooltip-text", "Enable video looping");
        button.setAttribute("data-button-toggle", "true");
        content.setAttribute("class", lpConOff);
        ytLoop = false;
    } else {
        //if (typeof ytPlayList != "undefined") setCookie("LoopyPL", ytPlayList);
        button.setAttribute("data-tooltip-text", "Disable video looping");
        button.setAttribute("data-button-toggle", "false");
        content.setAttribute("class", lpConOn);
        ytLoop = true;
    }
};
loopy.setAttribute("class", "yt-uix-button yt-uix-tooltip no-focus-outline");
loopy.setAttribute("role", "button");
loopy.setAttribute("data-button-toggle", "true");
loopy.setAttribute("type", "button");
loopy.setAttribute("data-tooltip-text", "Enable video looping")
loopy.id = "loopyButton";

var a = document.createElement("span");
a.id = "loopyContent"
a.setAttribute("class", lpConOff);
a.innerHTML = '<svg width="30" height="22" viewBox="0 0 1792 1792"><path d="M1216 1504q0 13-9.5 22.5t-22.5 9.5h-960q-8 0-13.5-2t-9-7-5.5-8-3-11.5-1-11.5v-600h-192q-26 0-45-19t-19-45q0-24 15-41l320-384q19-22 49-22t49 22l320 384q15 17 15 41 0 26-19 45t-45 19h-192v384h576q16 0 25 11l160 192q7 11 7 21zm640-416q0 24-15 41l-320 384q-20 23-49 23t-49-23l-320-384q-15-17-15-41 0-26 19-45t45-19h192v-384h-576q-16 0-25-12l-160-192q-7-9-7-20 0-13 9.5-22.5t22.5-9.5h960q8 0 13.5 2t9 7 5.5 8 3 11.5 1 11.5v600h192q26 0 45 19t19 45z"  fill="#727373"/></svg><span class="yt-uix-button-valign"/>';

loopy.appendChild(a);
base.appendChild(loopy);

onPlayerStateChange = function (newState) {
    if (!!ytLoop && newState == "0") setTimeout(function() { ytPlayer.playVideo(); }, 100);
}

var attempts = 0, timeout = null, mainLoopy = function() {
    clearTimeout(timeout);

    var menu_node = document.querySelector('[id^="watch"][id$="sentiment-actions"]');
    if (!menu_node && ++attempts < 10) {
        timeout = setTimeout(mainLoopy, 1000);
        return;
    }

    attempts = 100;

    var head = document.querySelector("head"),
        style = document.createElement("style");
    style.setAttribute("type", "text/css");
    style.textContent = '#loopyButton {border: 0 none !important;} #loopyButton:focus {box-shadow: none !important;} .yt-uix-button-panel:hover #loopyButton {border: 1px solid; border-color: #C6C6C6;} .LoopyOff { opacity: 0.3 }';
    head.appendChild(style);

    try {
        menu_node.appendChild(base);
    } catch(e) {}

    loopy = null;
    var attempts_player = 0,
        retry = setInterval(function() {
            ytPlayer = document.getElementById("movie_player");
            if (!!ytPlayer) {
                ytPlayer.addEventListener("onStateChange", "onPlayerStateChange");
                clearInterval(retry);
            } else if (attempts_player++ > 4) {
                clearInterval(retry);
            }
        }, 1500);
};

document.addEventListener('DOMContentLoaded', mainLoopy, false);
document.addEventListener('spfdone', mainLoopy, false);

})();