(function () { console.log("HELLO FROM PLAYLIST!", Math.floor(Math.random() * 999)); function msgPlayEvt(type, target) { const payload = { type, url: window.location.href, timestamp: target.currentTime, duration: target.duration, title: document.title.replace(/ - YouTube$/, ""), }; return browser.runtime.sendMessage(payload); } const vid = document.querySelector("video"); if (vid) { vid.addEventListener("play", function (evt) { msgPlayEvt("play", evt.target); }); vid.addEventListener("playing", function (evt) { msgPlayEvt("playing", evt.target); }); vid.addEventListener("pause", function (evt) { msgPlayEvt("pause", evt.target); }); vid.addEventListener("ended", function (evt) { msgPlayEvt("ended", evt.target); }); } function playIt(tryAgain) { const video = document.querySelector("video"); const smallPlay = document.querySelector( 'button.ytp-play-button.ytp-button[data-title-no-tooltip="Play"]', ); const bigPlay = document.querySelector( "button.ytp-large-play-button.ytp-button", //"div.ytp-cued-thumbnail-overlay", ); if (video && video.paused && smallPlay && bigPlay) { bigPlay.click(); } if ((!video || (video && video.paused)) && tryAgain > 0) { setTimeout(playIt, 500, tryAgain - 1); } } // Listen for messages from the popup or background script browser.runtime.onMessage.addListener((message) => { if (message.type === "print") { console.log("MESSAGE", message); } if (message.type === "autoplay") { setTimeout(playIt, 100, 10); } }); })();