Easy Picture-in-Picture

動画上部にポップアウト ボタンを表示して、ピクチャー・イン・ピクチャーを簡単に実行できるようにします。

当前为 2019-01-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Easy Picture-in-Picture
// @namespace    easy-picture-in-picture.user.js
// @version      1.1
// @description  動画上部にポップアウト ボタンを表示して、ピクチャー・イン・ピクチャーを簡単に実行できるようにします。
// @author       nafumofu
// @match        *://*/*
// @grant        GM_addStyle
// @license      MIT License
// ==/UserScript==

class EasyPictureInPicture {
    constructor() {
        this.epipButton = this.createButton();
        document.body.addEventListener('mousemove', (event) => this.event(event), {passive: true});
        document.body.addEventListener('touchstart', (event) => this.event(event), {passive: true});
    }
    event(event) {
        if (!this.eventLocked) {
            this.eventLocked = !!setTimeout(() => {
                this.eventLocked = false;
            }, 50);
            
            var posX = event.screenX || event.changedTouches[0].screenX;
            var posY = event.screenY || event.changedTouches[0].screenY;
            var nodes = document.elementsFromPoint(posX, posY);
            for (let node of nodes) {
                if (node.tagName === 'VIDEO') {
                    this.showButton(node);
                    break;
                }
            }
        }
    }
    popout() {
        document.pictureInPictureElement ? document.exitPictureInPicture() : this.epipTarget.requestPictureInPicture();
    }
    showButton(target) {
        if (!target.disablePictureInPicture) {
            this.epipTarget = target;
            
            var style = this.epipButton.style;
            var compStyle = getComputedStyle(this.epipButton);
            var rect =this.epipTarget.getBoundingClientRect();
            var posY = window.scrollY + rect.top;
            var posX = window.scrollX + rect.left + (rect.width / 2 - parseInt(compStyle.width) / 2);
            
            style.setProperty('top', `${posY}px`, 'important');
            style.setProperty('left', `${posX}px`, 'important');
            style.setProperty('opacity', '1', 'important');
            
            clearTimeout(this.epipTimer);
            this.epipTimer = setTimeout(() => {
                style.setProperty('opacity', '0', 'important');
            }, 3000);
        }
    }
    createButton() {
        GM_addStyle(`
        #epip-button {
            all: unset !important;
            opacity: 0 !important;
            position: absolute !important;
            background: rgba(0,0,0,0.4) !important;
            color: #ffffff !important;
            font-size: 13px !important;
            text-align: center !important;
            width: 80px !important;
            height: 30px !important;
            border: solid 1px rgba(255,255,255,0.4) !important;
            border-top: none !important;
            border-radius: 0 0 8px 8px !important;
            z-index: 2147483647 !important;
            transition: opacity 0.3s !important;
        }
        `);
        
        var button = document.createElement('button');
        button.id = 'epip-button';
        button.textContent = 'Pop-out';
        button.addEventListener('click', () => this.popout());
        document.documentElement.append(button);
        return button;
    }
}

setTimeout(() => {
    if (document.pictureInPictureEnabled) {
        new EasyPictureInPicture();
    }
}, 1000);