m.YouTube.com allow background play

Allows m.YouTube.com background play, especially useful for iPhone users

Stan na 05-02-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         m.YouTube.com allow background play
// @namespace    m-youtube-com-allow-background-play
// @version      1.0
// @description  Allows m.YouTube.com background play, especially useful for iPhone users
// @author       hlorand.hu
// @match        https://m.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @license      https://creativecommons.org/licenses/by-nc-sa/4.0/
// ==/UserScript==
 
// Original code: https://addons.mozilla.org/en-US/android/addon/video-background-play-fix/
 
(function() {
    //'use strict';
 
    const IS_YOUTUBE = window.location.hostname.search(/(?:^|.+\.)youtube\.com/) > -1 ||
                   window.location.hostname.search(/(?:^|.+\.)youtube-nocookie\.com/) > -1;
    const IS_MOBILE_YOUTUBE = window.location.hostname == 'm.youtube.com';
    const IS_DESKTOP_YOUTUBE = IS_YOUTUBE && !IS_MOBILE_YOUTUBE;
    const IS_VIMEO = window.location.hostname.search(/(?:^|.+\.)vimeo\.com/) > -1;

    const IS_ANDROID = window.navigator.userAgent.indexOf('Android') > -1;

    // Page Visibility API
    if (IS_ANDROID || !IS_DESKTOP_YOUTUBE) {
      Object.defineProperties(document.wrappedJSObject,
        { 'hidden': {value: false}, 'visibilityState': {value: 'visible'} });
    }

    window.addEventListener(
      'visibilitychange', evt => evt.stopImmediatePropagation(), true);

    // Fullscreen API
    if (IS_VIMEO) {
      window.addEventListener(
        'fullscreenchange', evt => evt.stopImmediatePropagation(), true);
    }

    // User activity tracking
    if (IS_YOUTUBE) {
      loop(pressKey, 60 * 1000, 10 * 1000); // every minute +/- 5 seconds
    }

    function pressKey() {
      const keyCodes = [18];
      let key = keyCodes[getRandomInt(0, keyCodes.length)];
      sendKeyEvent("keydown", key);
      sendKeyEvent("keyup", key);
    }

    function sendKeyEvent (aEvent, aKey) {
      document.dispatchEvent(new KeyboardEvent(aEvent, {
        bubbles: true,
        cancelable: true,
        keyCode: aKey,
        which: aKey,
      }));
    }

    function loop(aCallback, aDelay, aJitter) {
      let jitter = getRandomInt(-aJitter/2, aJitter/2);
      let delay = Math.max(aDelay + jitter, 0);

      window.setTimeout(() => {
                          aCallback();
                          loop(aCallback, aDelay, aJitter);
                        }, delay);
    }

    function getRandomInt(aMin, aMax) {
      let min = Math.ceil(aMin);
      let max = Math.floor(aMax);
      return Math.floor(Math.random() * (max - min)) + min;
    }

 
})();