-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.user.js
195 lines (168 loc) · 7.08 KB
/
index.user.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// ==UserScript==
// @name Picture In Picture
// @name:zh-CN HTML5画中画
// @namespace http://github.com/eternal-flame-AD/picture-in-picture/
// @version 0.2
// @downloadURL https://github.com/eternal-flame-AD/picture-in-picture/raw/master/index.user.js
// @homepage http://github.com/eternal-flame-AD/picture-in-picture/
// @description Provide picture in picture functionality to HTML5 videos on supported browsers
// @description:zh-CN 向兼容浏览器中的HTML5视频添加画中画按钮
// @compatible chrome 70+
// @author eternal-flame-AD
// @include *
// @grant GM_info
// @license Apache-2.0
// ==/UserScript==
(function() {
'use strict';
if (!("pictureInPictureEnabled" in document)) {
console.log("Your browser does not support picture in picture. Exiting...")
return
}
if (!document.pictureInPictureEnabled) {
console.log("Picture in picture is disabled. Exiting...")
return
}
window["PIP_URL_BASE"] = "https://cdn.jsdelivr.net/gh/eternal-flame-AD/picture-in-picture/";
function formatURL(url) {
return (!/^(https?:)?\/\//.test(url))?(window['PIP_URL_BASE'] + url):url;
}
function createStylesheet(url) {
url = formatURL(url);
let elt = document.createElement('link');
elt.rel = 'stylesheet';
elt.href = url;
document.documentElement.appendChild(elt);
};
const locateVideoElement = function _self(DOM=document.body) {
let video = DOM.querySelector("video")
if (video) return video
let iframes = DOM.querySelectorAll("iframe")
for (let iframe of iframes) {
video = _self(iframe.contentDocument.body)
if (video) return video
}
return null
}
let loaded = false;
const load = function(target) {
if (loaded) return
loaded = true;
createStylesheet('style.css');
class PIP {
constructor(target) {
this.target = target || locateVideoElement(document.body);
this.outside = false
let holder = document.createElement('div')
holder.className = 'pip-indicator-holder pip-off'
let img = document.createElement('img');
img.className = 'pip-indicator-logo'
img.src = formatURL("logo.svg")
holder.appendChild(img)
document.body.appendChild(holder)
this.off = this.off.bind(this)
this.on = this.on.bind(this)
this.updateTarget = this.updateTarget.bind(this)
holder.onclick = () => {
(this.outside?this.off:this.on)().catch(console.error)
}
this.elem = holder;
}
updateTarget() {
if (!this.target.isConnected) this.target = locateVideoElement(document.body);
let _this = this;
this.target.addEventListener('enterpictureinpicture', function _self() {
if (!_this.target.isConnected) {
_this.target.removeEventListener('enterpictureinpicture', _self)
return
}
_this.outside = true
_this.elem.classList.replace("pip-off","pip-on")
});
this.target.addEventListener('leavepictureinpicture', function _self() {
if (!_this.target.isConnected) {
_this.target.removeEventListener('leavepictureinpicture', _self)
return
}
_this.outside = false
_this.elem.classList.replace("pip-on","pip-off")
});
}
async on() {
this.updateTarget()
await this.target.requestPictureInPicture()
}
async off() {
await document.exitPictureInPicture()
}
}
//new PIP(target)
new PIP() // Fixed ad problem
}
{
let observers = []
let observe = function _self(DOM=document.body, _window=window, debug=false) {
const gotVideo = function(video) {
if (video.disablePictureInPicture) return;
console.log("Got video element! Loading...")
load(video)
observers.forEach(obs=>{
obs.disconnect()
})
}
{
// Existing video
let video = locateVideoElement(DOM)
if (video) gotVideo(video)
}
{
// Existing iframe
DOM.querySelectorAll("iframe").forEach(iframe=>{
iframe.contentWindow.onload = function() {
iframe.contentWindow["PIP_OBSERVING"] = true;
_self(iframe.contentDocument.body,iframe.contentWindow)
}
})
}
console.log("No video elements. Watching for changes...")
let callback = function(mutationList) {
mutationList.forEach((mutation)=>{
mutation.addedNodes.forEach((node)=>{
if (debug) console.log(node)
const search = function (node,tag) {
if (node.tagName && node.tagName.toUpperCase()==tag.toUpperCase()) {
return node
}
return node.querySelector && node.querySelector(tag)
}
{
// Dynamically added video
let video = search(node, "video")
if (video) gotVideo(video)
}
{
// Dynamically added iframe
let iframe = search(node, "iframe")
if (iframe) {
iframe.contentWindow.onload = function() {
if (iframe.contentWindow["PIP_OBSERVING"] == true) return;
iframe.contentWindow["PIP_OBSERVING"] = true;
_self(iframe.contentDocument.body,iframe.contentWindow)
}
}
}
})
})
}
let observer = new _window.MutationObserver(callback)
observer.observe(DOM,{
childList: true,
attributes: false,
subtree: true
})
observers.push(observer)
window.observers = observers;
}
observe(document.body)
}
})();