-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max Keyboard Shortcuts.user.js
189 lines (156 loc) · 5.2 KB
/
Max Keyboard Shortcuts.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
// ==UserScript==
// @name Max Keyboard Shortcuts
// @namespace https://github.com/rafalb8/HBOMaxKeyboard
// @version 0.24
// @description Adds keyboard shortcuts to Max player
// @author Rafalb8
// @author CHJ85
// @match https://play.max.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=max.com
// @license MIT
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Configuration
const SEEK_INTERVAL = 5;
let Video = null;
let SkipButton = null;
let Overlay = null;
// Observer
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
// Ensure it's an element node
if (node.nodeType !== Node.ELEMENT_NODE) {
return
}
switch (node.nodeName) {
case 'VIDEO':
createOverlay(node);
addVideoMethods(node);
Video = node;
break;
case 'DIV':
const root = node;
node = findNode(root, 'player-ux-skip-button');
if (node) {
SkipButton = node;
break;
}
node = findNode(root, 'player-ux-up-next-button');
if (node) {
SkipButton = node;
}
break;
}
});
});
});
// Start observer
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Start keydown listener
document.addEventListener('keydown', keybinds);
function keybinds(e) {
switch (e.key) {
// Seek forward
case 'l':
Video.seek(SEEK_INTERVAL);
break;
// Seek backward
case 'j':
Video.seek(-SEEK_INTERVAL);
break;
// Skip intro or move to next episode
case 's':
SkipButton.click();
break;
case 'f':
if (document.fullscreenElement) {
document.exitFullscreen();
}
return;
case 'd':
Overlay.toggle();
break;
default:
return;
}
e.stopImmediatePropagation();
}
// addVideoMethods Adds seek method to video element
function addVideoMethods(vid) {
vid.seek = function (val) {
const buffered = this.buffered;
const bufLen = buffered.length;
const ts = this.currentTime + val;
for (let i = 0; i < bufLen; i++) {
const start = buffered.start(i);
const end = buffered.end(i);
if (start <= ts && ts <= end) {
this.currentTime = ts;
return;
}
}
const end = buffered.end(bufLen - 1);
if (ts > end) {
this.currentTime = end;
return;
}
this.currentTime = ts;
};
}
function createOverlay(vid) {
if (vid.parentNode.children.length >= 3) {
return
}
// Create overlay
Overlay = document.createElement('div');
Overlay.style.position = 'absolute';
Overlay.style.top = '0';
Overlay.style.left = '0';
Overlay.style.right = '0';
Overlay.style.bottom = '0';
Overlay.style.justifyContent = 'center';
Overlay.style.alignItems = 'center';
Overlay.style.color = 'white';
Overlay.style.fontSize = '24px';
Overlay.style.display = 'none';
// Ensure overlay is above the video
Overlay.style.zIndex = '10';
// Semi-transparent background
Overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
// Add after video
vid.style.position = 'relative';
vid.parentNode.appendChild(Overlay);
const updateMeta = function () {
Overlay.innerText = `Resolution: ${Video.videoWidth}x${Video.videoHeight}`;
}
vid.addEventListener('resize', updateMeta);
vid.addEventListener('loadedmetadata', updateMeta);
Overlay.toggle = function () {
this.style.display = (this.style.display === 'none') ? 'flex' : 'none';
};
}
// Recursive function to find a node with a specific data-testid value
function findNode(node, testID) {
if (node == null || node.nodeType !== Node.ELEMENT_NODE) {
return null;
}
if (node.getAttribute('data-testid') === testID) {
return node;
}
const children = node.childNodes;
const childCount = children.length;
for (let i = 0; i < childCount; i++) {
const found = findNode(children[i], testID);
if (found) {
return found;
}
}
return null;
}
})();