| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- (function () {
- console.log("HELLO FROM PLAYLIST!", Math.floor(Math.random() * 999));
- function msgPlayEvt(type, target) {
- const payload = {
- type,
- url: PlaylistUtils.cleanVideoUrl(window.location.href),
- timestamp: target.currentTime,
- duration: target.duration,
- title: document.title.replace(/ - YouTube$/, ""),
- };
- return browser.runtime.sendMessage(payload);
- }
- // YouTube is a SPA: advancing to the next video in a queue swaps in a new
- // <video> element without a full page load, so a listener attached to one
- // specific element (grabbed once at injection time) stops receiving events.
- // Listening on `document` in the capture phase catches every video, past or
- // future, while `#movie_player` scopes this to the standard watch-page
- // player only (excludes home/search hover-preview thumbnails, Shorts, etc).
- ["play", "playing", "pause", "ended"].forEach(function (type) {
- document.addEventListener(
- type,
- function (evt) {
- if (
- evt.target.tagName === "VIDEO" &&
- evt.target.closest("#movie_player")
- ) {
- msgPlayEvt(type, evt.target);
- }
- },
- true,
- );
- });
- 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",
- );
- 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);
- }
- });
- })();
|