当网页进入后台且视频占比大于30%、有声音时,自动开启PiP(画中画模式),避免重复通知
当前为
// ==UserScript==
// @name PIP Enhancer
// @namespace http://tampermonkey.net/
// @version 1.9
// @description 当网页进入后台且视频占比大于30%、有声音时,自动开启PiP(画中画模式),避免重复通知
// @author OB_BUFF
// @license GPL-3.0
// @match *://*/*
// @grant GM_notification
// ==/UserScript==
(function() {
'use strict';
let pipActive = false; // 记录当前是否在 PIP 模式,避免重复通知
// 语言包(支持多语言)
const messages = {
"zh": {
"enterPiP": "网页失去焦点,视频进入画中画模式",
"exitPiP": "网页回到前台,退出画中画模式"
},
"en": {
"enterPiP": "Page lost focus, video entered Picture-in-Picture mode",
"exitPiP": "Page is back in focus, exiting Picture-in-Picture mode"
},
"es": {
"enterPiP": "La página perdió el foco, el video entró en modo PiP",
"exitPiP": "La página volvió a enfocarse, saliendo del modo PiP"
}
};
// 获取用户浏览器语言
const userLang = navigator.language.startsWith("zh") ? "zh" :
navigator.language.startsWith("es") ? "es" : "en";
/**
* 判断视频是否满足条件
* 1. 正在播放
* 2. 有声音(音量 > 0 或未静音)
* 3. 占屏幕面积 > 30%
*/
function isEligibleVideo(video) {
let rect = video.getBoundingClientRect();
let screenWidth = window.innerWidth;
let screenHeight = window.innerHeight;
let videoArea = rect.width * rect.height;
let screenArea = screenWidth * screenHeight;
return (
!video.paused &&
video.volume > 0 && !video.muted &&
(videoArea / screenArea) > 0.3
);
}
/**
* 进入画中画模式
*/
async function enterPiP() {
if (pipActive) return; // 避免重复触发
let videos = document.querySelectorAll("video");
for (let video of videos) {
if (isEligibleVideo(video)) {
try {
await video.requestPictureInPicture();
pipActive = true;
GM_notification({
text: messages[userLang].enterPiP,
title: "视频监测",
timeout: 5000
});
} catch (error) {
console.error("无法进入 PiP 模式:", error);
}
break;
}
}
}
/**
* 退出画中画模式
*/
function exitPiP() {
if (!pipActive) return; // 避免重复触发
if (document.pictureInPictureElement) {
document.exitPictureInPicture();
GM_notification({
text: messages[userLang].exitPiP,
title: "视频监测",
timeout: 5000
});
}
pipActive = false;
}
/**
* 监听网页可见性变化
*/
document.addEventListener("visibilitychange", function() {
if (document.hidden) {
setTimeout(() => {
if (document.hidden) enterPiP();
}, 300); // 确保 visibility 状态已经稳定
} else {
exitPiP();
}
});
/**
* 监听窗口焦点变化
*/
window.addEventListener("blur", enterPiP);
window.addEventListener("focus", exitPiP);
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址