Auto PiP on Tab Switch

Auto Picture-in-Picture when switching tabs (only if video is playing)

  1. // ==UserScript==
  2. // @name Auto PiP on Tab Switch
  3. // @name:ru Авто PiP при переключении вкладок
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.2
  6. // @description Auto Picture-in-Picture when switching tabs (only if video is playing)
  7. // @description:ru Автоматически включает режим "Картинка в картинке" (PiP), если вы ушли с вкладки и видео воспроизводится
  8. // @author FerNikoMF + ChatGPT Fix
  9. // @match *://*/*
  10. // @grant none
  11. // @license MIT
  12. // @icon https://i.imgur.com/0OXnhxm.png
  13. // ==/UserScript==
  14.  
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. const ONLY_WHEN_PLAYING = true; // 💡
  20. let videoElement = null;
  21. let isPiP = false;
  22.  
  23. function findVideo() {
  24. const videos = document.querySelectorAll('video');
  25. for (let video of videos) {
  26. if (video.readyState >= 2) {
  27. return video;
  28. }
  29. }
  30. return null;
  31. }
  32.  
  33. document.addEventListener("visibilitychange", async () => {
  34. if (document.hidden) {
  35. videoElement = findVideo();
  36. if (videoElement && !document.pictureInPictureElement) {
  37. const canEnable = !ONLY_WHEN_PLAYING || !videoElement.paused;
  38. if (canEnable) {
  39. try {
  40. await videoElement.requestPictureInPicture();
  41. isPiP = true;
  42. } catch (error) {
  43. console.warn("Не удалось включить PiP:", error.message);
  44. }
  45. }
  46. }
  47. } else {
  48. if (document.pictureInPictureElement) {
  49. try {
  50. await document.exitPictureInPicture();
  51. isPiP = false;
  52. } catch (e) {
  53. console.warn("Не удалось выключить PiP:", e.message);
  54. }
  55. }
  56. }
  57. });
  58. })();

QingJ © 2025

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