Enable Picture In Picture For Every Sites (简单地启动画中画)

enable the picture in picture feature by holding down mouse for 1 second

目前為 2019-07-31 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Enable Picture In Picture For Every Sites (简单地启动画中画)
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  enable the picture in picture feature by holding down mouse for 1 second
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

let retry = true,
    timer = void(0),
    e_path = [],
    v_exist = false, // if a video exists in the path given
    target = void(0);

function enable_pip(element){
    if (!document.pictureInPictureEnabled) {
        alert('Your Browser Does Not Support Picture In Picture!');
    }
    element.disablePictureInPicture = false;
    element.requestPictureInPicture().then().catch(()=>{
        if(retry){
            retry = false;
            enable_pip(element);
        }else{
            console.log('unable to enable picture in picture\ncheck the browser');
        }
    });
}

document.onmousedown = (element)=>{
    timer = setTimeout(()=>{
        if(document.pictureInPictureElement){ // pause the video beforehand so that when mouseup, the video will play
            document.exitPictureInPicture();
            target.pause();
        }else{
            e_path = element.path;
            for(let i = 0;i<e_path.length;i++){ // find that video element
                if(e_path[i].nodeName === 'VIDEO'){
                    v_exist = true;
                    target = e_path[i];
                    enable_pip(target);
                    target.pause(); // same
                    console.log('PIP activated');
                }
            }
            if(!v_exist){
                console.log('no video exists in this path');
            }
        }
    },1000);
};

document.onmouseup = ()=>{ // if not 1 second, cancel the timer
    clearTimeout(timer);
};

QingJ © 2025

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