-
Notifications
You must be signed in to change notification settings - Fork 3
/
youtube-popup-window.user.js
179 lines (138 loc) · 5.34 KB
/
youtube-popup-window.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
// ==UserScript==
// @name YouTube Popup Window
// @name:zh-TW YouTube Popup Window
// @name:ja YouTube Popup Window
// @namespace http://tampermonkey.net/
// @version 0.2.2
// @description Enhances YouTube with a popup window feature.
// @description:zh-TW 透過彈出視窗功能增強YouTube。
// @description:ja YouTubeをポップアップウィンドウ機能で強化します。
// @author CY Fung
// @license MIT
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/[email protected]
// @grant GM_registerMenuCommand
// @allFrames true
// ==/UserScript==
(async function () {
'use strict';
const shortcutKey = 'ctrlcmd-alt-keya';
const winName = 'x4tGg';
const styleName = 'rCbM3';
const observablePromise = (proc, timeoutPromise) => {
let promise = null;
return {
obtain() {
if (!promise) {
promise = new Promise(resolve => {
let mo = null;
const f = () => {
let t = proc();
if (t) {
mo.disconnect();
mo.takeRecords();
mo = null;
resolve(t);
}
}
mo = new MutationObserver(f);
mo.observe(document, { subtree: true, childList: true })
f();
timeoutPromise && timeoutPromise.then(() => {
resolve(null)
});
});
}
return promise
}
}
}
function getVideo() {
return document.querySelector('.video-stream.html5-main-video');
}
function registerKeyboard(o) {
const { openPopup } = o;
const { KeyboardService } = VM.shortcut;
const service = new KeyboardService();
service.setContext('activeOnInput', false);
async function updateActiveOnInput() {
const elm = document.activeElement;
service.setContext('activeOnInput', elm instanceof HTMLInputElement || elm instanceof HTMLTextAreaElement);
}
document.addEventListener('focus', (e) => {
updateActiveOnInput();
}, true);
document.addEventListener('blur', (e) => {
updateActiveOnInput();
}, true);
service.register(shortcutKey, openPopup, {
condition: '!activeOnInput',
});
service.enable();
}
if (window.name === winName && window === top) {
if (!document.head) await observablePromise(() => document.head).obtain();
let style = document.createElement('style');
style.id = styleName;
style.textContent = `
*[class][id].style-scope.ytd-watch-flexy {
min-width: unset !important;
min-height: unset !important;
}
`
document.head.appendChild(style);
} else if (window !== top && top.name === winName) {
if (!document.head) await observablePromise(() => document.head).obtain();
let style = document.createElement('style');
style.id = styleName;
style.textContent = `
* {
min-width: unset !important;
min-height: unset !important;
}
`
document.head.appendChild(style);
} else if (window === top) {
function openPopup() {
const currentUrl = window.location.href;
const ytdAppElm = document.querySelector('ytd-app');
if (!ytdAppElm) return;
const rect = ytdAppElm.getBoundingClientRect();
const w = rect.width;
const h = rect.height;
const popupOptions = `toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=${w},height=${h}`;
let video = getVideo();
if (video) {
video.pause();
}
const win = window.open(currentUrl, '', popupOptions);
win.name = winName;
const b = document.querySelector('#x4tGg');
if (b) b.remove();
}
GM_registerMenuCommand('Open Popup Window', function () {
if (document.querySelector('#x4tGg')) return;
const div = document.body.appendChild(document.createElement('div'));
div.id = 'x4tGg';
div.textContent = 'Click to Open Popup'
Object.assign(div.style, {
'position': 'fixed',
'left': '50vw',
'top': '50vh',
'padding': '28px',
'backgroundColor': 'rgb(56, 94, 131)',
'color': '#fff',
'borderRadius': '16px',
'fontSize': '18pt',
'zIndex': '9999',
'transform': 'translate(-50%, -50%)'
})
div.onclick = function () {
openPopup();
}
});
registerKeyboard({ openPopup });
}
// Your code here...
})();