2018-09-23-111028_1920x1080_scrot.png

画中画功能还是很好用的,可以上班的时候摸鱼看电影。所以写个脚本给开启画中画加个快捷键方便使用。加入油猴之后在视频页按 alt+p 就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// ==UserScript==
// @name 开启画中画
// @namespace https://cnwangjie.com
// @version 0.1
// @description 开启画中画
// @author cnwangjie
// @match https://*.bilibili.com/*
// @match https://*.youku.com/*
// @match https://*.douyu.com/*
// @grant none
// ==/UserScript==

(function () {
const getCurrentPlayingVideo = () => {
const videos = [].slice.call(document.querySelectorAll('video'))
const [video] = videos.filter(v => !v.paused)
return video
}
const requestPictureInPictureForCurrentPlayingVideo = () => {
const video = getCurrentPlayingVideo()
if (!video) return
video.requestPictureInPicture()
const handler = () => {
document.exitPictureInPicture()
video.removeEventListener(handler)
}
video.addEventListener('pause', handler)
}
document.removeEventListener('keydown', window. 开启画中画键盘事件处理器)
window. 开启画中画键盘事件处理器 = document.addEventListener('keydown', e => {
if (e.altKey && e.keyCode === 80) requestPictureInPictureForCurrentPlayingVideo()
})
})()