YouTube™ YTMusic™ Nonstop

Prevent auto-pause, keep YouTube & YouTube Music nonstop

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube™ YTMusic™ Nonstop
// @namespace    https://github.com/nvbangg
// @version      1.0
// @description  Prevent auto-pause, keep YouTube & YouTube Music nonstop
// @author       nvbangg (https://github.com/nvbangg)
// @copyright    Copyright (c) 2025 Nguyễn Văn Bằng (nvbangg, github.com/nvbangg)
// @homepage     https://github.com/nvbangg/YouTube_YTMusic_Nonstop
// @icon         https://raw.githubusercontent.com/nvbangg/YouTube_YTMusic_Nonstop/main/src/icon.png
// @match        *://*.youtube.com/*
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";
  // Force page to always appear visible
  Object.defineProperties(document, {
    hidden: { value: false },
    visibilityState: { value: "visible" },
  });

  // Block visibilitychange events
  addEventListener(
    "visibilitychange",
    (e) => e.stopImmediatePropagation(),
    true
  );

  // Keep _lact (last activity) fresh
  const keepActive = () => {
    if ("_lact" in window)
      setInterval(() => (window._lact = Date.now()), 300000);
  };
  (function wait() {
    "_lact" in window ? keepActive() : setTimeout(wait, 1000);
  })();
})();