// ==UserScript== // @name 小窗口视频(bili,mgtv) // @version 0.5 // @license MPL-2.0 // @namespace // @description 小窗口视频(bili,mgtv)。网页右下角会出现一个小按钮,点击之后视频会通过小窗口播放。基于chrome浏览器的画中画(Picture in Picture)。 // @author c4r // @require https://code.jquery.com/jquery-latest.js // @match https://www.bilibili.com/video/* // @match https://www.bilibili.com/bangumi/* // @match https://live.bilibili.com/* // @match https://www.mgtv.com/b/* // @match https://www.bilibili.com/watchlater/* // @match https://www.youtube.com/watch* // @grant none // @downloadURL none // ==/UserScript== (function () { 'use strict'; let videoUrl = undefined; let timeID = 0 let loadmetaRespon = function (timeID) { if (document.pictureInPictureElement && (!document.pictureInPictureElement.src || document.pictureInPictureElement.src != $('video').attr('src'))) { if (timeID) { clearTimeout(timeID) } console.log("picInpic : loadedmetadata") // document.exitPictureInPicture() document.getElementsByTagName('video')[0].requestPictureInPicture().catch(error => { // 视频无法进入画中画模式 console.log('picInpic error : ', error, document.pictureInPictureElement) // loadedmetadata有的时候触发的太快....等5秒的timeout影响观感 timeID = setTimeout(() => { if (document.pictureInPictureElement && (!document.pictureInPictureElement.src || document.pictureInPictureElement.src != $('video').attr('src'))) { // 首次尝试进入画中画 console.log("picInpic : setTimeout : pictureInPictureElement ") // document.exitPictureInPicture() $('video').get(0).requestPictureInPicture() // document.getElementsByTagName('video')[0].removeEventListener("loadedmetadata", loadmetaRespon(timeID)) } }, 2000); }); // document.getElementsByTagName('video')[0].removeEventListener("loadeddata", loadmetaRespon(timeID)) } } /** * */ let callbackVideo = function (mutationList, observer) { // console.log("coin change : ", mutationList) // 保证video已经有url if (document.pictureInPictureElement && $('video').length > 0 && $('video').attr('src') && $('video').attr('src').length > 0) { // 抓到url之后就不再处理了 if (videoUrl != $('video').attr('src')) { // 视频地址发生变更 videoUrl = $('video').attr('src') console.log("picInpic : video address changed ", $('video').attr('src')) // 如果pictureInPictureElement的src与当前video的src不一致 if (!document.pictureInPictureElement.src || document.pictureInPictureElement.src != $('video').attr('src')) { // console.log("picInpic : video address changed ", $('video').attr('src')) // 在画中画里 // console.log("picInpic : pictureInPictureElement ", document.pictureInPictureElement) // 防止loadedmetadata没有被触发 timeID = setTimeout(() => { if (document.pictureInPictureElement && (!document.pictureInPictureElement.src || document.pictureInPictureElement.src != $('video').attr('src'))) { // 首次尝试进入画中画 console.log("picInpic : setTimeout : pictureInPictureElement ") // document.exitPictureInPicture() $('video').get(0).requestPictureInPicture() // document.getElementsByTagName('video')[0].removeEventListener("loadedmetadata", loadmetaRespon(timeID)) } }, 5000); } // document.getElementsByTagName('video')[0].removeEventListener("loadeddata", loadmetaRespon(timeID)) document.getElementsByTagName('video')[0].addEventListener("loadedmetadata", loadmetaRespon(timeID), { once: true }) // $('video').on("timeupdate", () => { // if (document.pictureInPictureElement // && document.pictureInPictureElement.src != document.getElementsByTagName('video')[0].src) { // document.getElementsByTagName('video')[0].requestPictureInPicture().catch(error => { // // 视频无法进入画中画模式 // console.log('picInpic error : ', error, document.pictureInPictureElement) // }); // } // }) } } } let observerVideo = new MutationObserver(callbackVideo) document.body.insertAdjacentHTML('beforeend', '