Video Player AutoConfig

Automatically enforces playback speed, preferred quality, fullscreen, and mute state on many streaming sites. Works across multiple hosts and mirrors without site‑specific configuration. Inspired by Ghoste's HiAnime Auto 1080p script.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Video Player AutoConfig
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  Automatically enforces playback speed, preferred quality, fullscreen, and mute state on many streaming sites. Works across multiple hosts and mirrors without site‑specific configuration. Inspired by Ghoste's HiAnime Auto 1080p script.
// @match        https://*/*
// @author       AliensStoleMyAntibodies
// @icon         data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjU2IiBoZWlnaHQ9IjI1NiIgdmlld0JveD0iMCAwIDI1NiAyNTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3Qgd2lkdGg9IjI1NiIgaGVpZ2h0PSIyNTYiIHJ4PSI0OCIgc3R5bGU9ImZpbGw6IzAwQUVFRiIvPjxwb2x5Z29uIHBvaW50cz0iMTA0LDgwIDE3NiwxMjggMTA0LDE3NiIgZmlsbD0iI2ZmZiIvPjx0ZXh0IHg9IjEyOCIgeT0iMjIwIiBmb250LXNpemU9IjQ4IiBmb250LWZhbWlseT0iQXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZiIgZm9udC13ZWlnaHQ9ImJvbGQiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGZpbGw9IiNmZmYiPkFVVE88L3RleHQ+PC9zdmc+
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
"use strict";

const settings = {
  pauseOnFocusLoss: false,
  // Set this to true if you want it to pause/unpause based on if you're looking at
  // Valid Values: true, false.
  autoFocus: false,
  // If this is true, automatically focuses on the player once it begins playback (for keyboard shortcuts)
  // Valid Values: true, false.
  autoUnmute: false,
  // If this is true, automatically unmutes the player if it starts muted.
  // Valid Values: true, false.
  autoFullscreen: false,
  // Set this to true if you want the video to automatically go fullscreen. Doesn't work on Youtube.
  // Valid Values: true, false.
  playbackRate: 1,
  // Sets the Playback Speed.
  // Valid Values: 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2. (higher might work)
  playbackQuality: "1080",
  // Sets the Video Quality.
  // Valid Values: 1080, 720, 360.
};

const hooked = new WeakSet();

let fullscreenApplied = false;

document.addEventListener("yt-navigate-finish", () => {
  setTimeout(() => detectPlayers(document), 500);
});

function isYouTube() {
  return location.hostname.includes("youtube.com") ||
         location.hostname.includes("youtu.be");
}

function hookYouTube(onPlayer) {

  function tryAttach() {

    if (!window.YT || !YT.Player) return;

    const player = document.getElementById("movie_player");

    if (!player || player.__autoHooked) return;

    player.__autoHooked = true;

    onPlayer(player);

    // detect new videos (SPA navigation)
    const original = player.loadVideoById;

    if (original) {
      player.loadVideoById = function () {
        const result = original.apply(this, arguments);
        setTimeout(() => onPlayer(player), 300);
        return result;
      };
    }
  }

  const interval = setInterval(tryAttach, 500);

  setTimeout(() => clearInterval(interval), 15000);
}

hookYouTube((player) => {

  const adapter = createAdapter(player);

  applyBehavior(adapter);

});

function createAdapter(source) {

  // JW Player
  if (source && typeof source.getState === "function") {
    return {
      play: () => source.play(),
      pause: () => source.pause(),
      isPlaying: () => source.getState() === "playing",
      setRate: r => source.setPlaybackRate?.(r),
      setMute: m => source.setMute?.(m),
      fullscreen: () => source.setFullscreen?.(true),
      setQuality: () => {},
      onReady: fn => source.on("firstFrame", fn),
      onQualityChange: fn => source.on("levels", fn),
    };
  }

  // Video.js
  if (source && source.ready && source.tech_) {
    return {
      play: () => source.play(),
      pause: () => source.pause(),
      isPlaying: () => !source.paused(),
      setRate: r => source.playbackRate(r),
      setMute: m => source.muted(m),
      fullscreen: () => source.requestFullscreen(),
      setQuality: () => {},
      onReady: fn => source.ready(fn),
      onQualityChange: () => {},
    };
  }

  // Plyr
  if (source && source.play && source.fullscreen && source.elements) {
    return {
      play: () => source.play(),
      pause: () => source.pause(),
      isPlaying: () => source.playing,
      setRate: r => source.speed = r,
      setMute: m => source.muted = m,
      fullscreen: () => source.fullscreen.enter(),
      setQuality: q => source.quality = parseInt(q),
      onReady: fn => source.on("ready", fn),
      onQualityChange: fn => source.on("qualitychange", fn),
    };
  }

    // Native video
    if (source instanceof HTMLVideoElement) {

        // Prevent conflict with YouTube
        if (source.closest("#movie_player"))
            return null;

        return {
            play: () => source.play().catch(()=>{}),
            pause: () => source.pause(),
            isPlaying: () => !source.paused,
            setRate: r => source.playbackRate = r,
            setMute: m => source.muted = m,
            fullscreen: () => source.requestFullscreen?.(),
            setQuality: () => {},
            onReady: fn => {
                if (source.readyState >= 2) fn();
                else source.addEventListener("playing", fn, {once:true});
            },
            onQualityChange: () => {},
        };
    }
        // YouTube
        if (source && source.getPlayerState && source.setPlaybackRate) {

            return {

                play: () => source.playVideo(),

                pause: () => source.pauseVideo(),

                isPlaying: () => source.getPlayerState() === 1,

                setRate: r => {
                    try { source.setPlaybackRate(r); } catch {}
                },

                setMute: m => {
                    if (m) source.mute();
                    else source.unMute();
                },

                fullscreen: () => {

                    const player = document.querySelector(".html5-video-player");

                    if (!player?.classList.contains("ytp-fullscreen")) {

                        document
                            .querySelector(".ytp-fullscreen-button")
                            ?.click();
                    }
                },

                setQuality: q => {

                    try {

                        const levels = source.getAvailableQualityLevels();

                        const match = levels.find(l => l.includes(q));

                        if (match)
                            source.setPlaybackQualityRange(match, match);

                    } catch {}

                },

                onReady: fn => {

                    const check = () => {

                        const state = source.getPlayerState?.();

                        if (state !== -1) fn();
                        else setTimeout(check, 500);

                    };

                    check();
                },

                onQualityChange: () => {},
            };
        }

  return null;
}

function applyBehavior(adapter) {

  adapter.onReady(() => {

    adapter.setRate(settings.playbackRate);

    if (settings.autoUnmute)
      adapter.setMute(false);

    if (settings.autoFullscreen && !fullscreenApplied && !isYouTube()) {

        const enterFS = () => {
            if (fullscreenApplied) return;

            fullscreenApplied = true;

            setTimeout(() => {
                if (!document.fullscreenElement)
                    adapter.fullscreen();
            }, 150);
        };

        adapter.onReady(() => {
            enterFS();
        });
    }

    adapter.setQuality(settings.playbackQuality);
  });

  adapter.onQualityChange(() => {
    adapter.setQuality(settings.playbackQuality);
  });

  if (settings.pauseOnFocusLoss) {

    let wasPlaying = false;

    document.addEventListener("visibilitychange", () => {

      if (document.visibilityState === "hidden") {
        wasPlaying = adapter.isPlaying();
        adapter.pause();
      } else if (wasPlaying) {
        adapter.play();
      }

    });

  }
}

function tryHook(source) {

  if (!source || hooked.has(source))
    return;

  const adapter = createAdapter(source);

  if (!adapter)
    return;

  hooked.add(source);

  console.log("Video AutoConfig hooked", source);

  applyBehavior(adapter);
}

function detectPlayers(node) {

  if (!node)
    return;

  // Native video
  if (node.tagName === "VIDEO")
    tryHook(node);

  // scan subtree
  node.querySelectorAll?.("video").forEach(v => tryHook(v));

  // JW Player
  if (window.jwplayer) {
    try {
      const players = jwplayer.getPlayers?.() || [jwplayer()];
      players.forEach(p => tryHook(p));
    } catch {}
  }

  // Video.js
  if (window.videojs?.players) {
    Object.values(videojs.players).forEach(p => tryHook(p));
  }

  // Plyr
  document.querySelectorAll(".plyr").forEach(el => {
    if (el.plyr)
      tryHook(el.plyr);
  });
  // YouTube player
  const yt = document.getElementById("movie_player");
  if (yt) tryHook(yt);

}

const observer = new MutationObserver(mutations => {
  mutations.forEach(m => {
    m.addedNodes.forEach(node => detectPlayers(node));
  });
});

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

// initial scan
detectPlayers(document);

})();