TikTok Background Playback

Keeps TikTok videos playing in the background.

  1. // ==UserScript==
  2. // @name TikTok Background Playback
  3. // @author UniverseDev
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.0
  6. // @license GPL-3.0-or-later
  7. // @description Keeps TikTok videos playing in the background.
  8. // @match https://www.tiktok.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=tiktok.com
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. let isOverrideActive = false;
  17. let isVideoPlaying = false;
  18. let currentVideoElement = null;
  19.  
  20. const overrideVisibility = () => {
  21. if (!isOverrideActive) {
  22. Object.defineProperties(document, {
  23. hidden: {
  24. configurable: true,
  25. get: () => false
  26. },
  27. visibilityState: {
  28. configurable: true,
  29. get: () => 'visible'
  30. }
  31. });
  32. document.dispatchEvent(new Event('visibilitychange'));
  33. isOverrideActive = true;
  34. console.log("TikTok Background Playback: Activated");
  35. }
  36. };
  37.  
  38. const restoreVisibility = () => {
  39. if (isOverrideActive) {
  40. delete document.hidden;
  41. delete document.visibilityState;
  42. isOverrideActive = false;
  43. console.log("TikTok Background Playback: Deactivated");
  44. }
  45. };
  46.  
  47. document.addEventListener('play', function(event) {
  48. if (event.target.tagName === 'VIDEO') {
  49. isVideoPlaying = true;
  50. currentVideoElement = event.target;
  51. console.log("TikTok Background Playback: Video started.");
  52. if (document.hidden) {
  53. overrideVisibility();
  54. }
  55. }
  56. }, true);
  57.  
  58. document.addEventListener('ended', function(event) {
  59. if (event.target.tagName === 'VIDEO' && event.target === currentVideoElement) {
  60. isVideoPlaying = false;
  61. currentVideoElement = null;
  62. if (!document.hidden) {
  63. restoreVisibility();
  64. }
  65. console.log("TikTok Background Playback: Video ended.");
  66. }
  67. });
  68.  
  69. document.addEventListener('visibilitychange', () => {
  70. if (document.hidden && isVideoPlaying) {
  71. overrideVisibility();
  72. } else if (!document.hidden && isOverrideActive) {
  73. restoreVisibility();
  74. }
  75. });
  76.  
  77. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址