-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
121 lines (105 loc) · 3.55 KB
/
content.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
document.addEventListener('keydown', handleKeyDown);
const allowedUrls = [
'https://www.youtube.com/watch*',
'https://www.netflix.com/watch/*',
'https://play.max.com/video/watch/*',
'https://podcasts.google.com/*',
'https://www.paramountplus.com/shows/video/*',
'https://www.paramountplus.com/movies/video/*',
'https://www.disneyplus.com/video/*',
'https://www.amazon.com/gp/video/detail/*',
'https://www.hulu.com/watch/*',
];
function isUrlAllowed(allowedUrls, urlToCheck) {
console.log(`urlToCheck: ${urlToCheck}`);
return allowedUrls.some(url => {
const regex = new RegExp("^" + url.replace("*", ".*") + "$");
return regex.test(urlToCheck);
});
}
function handleKeyDown(event) {
const currentUrl = window.location.href;
console.log(`currentUrl: ${currentUrl}`);
if (!isUrlAllowed(allowedUrls, currentUrl)) {
console.log('URL not in the allowlist for Media Controller');
return;
}
console.log('Allowed');
if ( // k, j, l, m, <, > are already defined in YouTube
currentUrl.includes('youtube.com')
&& ['k', 'j', 'l', 'm', '<', '>'].includes(event.key)
) {
return;
}
if ( // j & l cause issues on Netflix
currentUrl.includes('netflix.com')
&& ['j', 'l'].includes(event.key)
) {
return;
}
const mediaElements = Array.from(document.querySelectorAll('video, audio'));
mediaElements.forEach((media) => {
const volumeStep = 0.05;
const playbackRateStep = 0.1;
switch (event.key) {
case 'k':
if (media.paused) media.play();
else media.pause();
break;
case 'j':
media.currentTime = Math.max(media.currentTime - 10, 0);
break;
case 'l':
media.currentTime = Math.min(media.currentTime + 10, media.duration);
break;
case 'm':
media.muted = !media.muted;
break;
case 's':
newVolume = Math.max(media.volume - volumeStep, 0);
media.volume = newVolume;
showStatusPrompt(newVolume, 'Volume');
break;
case 'w':
newVolume = Math.min(media.volume + volumeStep, 1);
media.volume = newVolume;
showStatusPrompt(newVolume, 'Volume');
break;
case '<':
newPlaybackRate = Math.max(media.playbackRate - playbackRateStep, 0.5);
media.playbackRate = newPlaybackRate;
showStatusPrompt(newPlaybackRate, 'Speed');
break;
case '>':
newPlaybackRate = Math.min(media.playbackRate + 0.1, 2);
media.playbackRate = newPlaybackRate;
showStatusPrompt(newPlaybackRate, 'Speed');
break;
}
});
}
function showStatusPrompt(status, name) {
let statusPrompt = document.getElementById('status-prompt');
if (!statusPrompt) {
statusPrompt = document.createElement('div');
statusPrompt.id = 'status-prompt';
statusPrompt.style.position = 'fixed';
statusPrompt.style.top = '10%';
statusPrompt.style.left = '50%';
statusPrompt.style.transform = 'translate(-50%, -50%)';
statusPrompt.style.padding = '10px';
statusPrompt.style.borderRadius = '5px';
statusPrompt.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
statusPrompt.style.color = 'white';
statusPrompt.style.fontSize = '18px';
statusPrompt.style.fontWeight = 'regular';
statusPrompt.style.zIndex = '9999';
document.body.appendChild(statusPrompt);
}
statusPrompt.textContent = `${name}: ${(status * 100).toFixed(0)}%`;
statusPrompt.style.display = 'block';
clearTimeout(statusPrompt.timeout);
statusPrompt.timeout = setTimeout(() => {
statusPrompt.style.display = 'none';
}, 1000);
}