-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (54 loc) · 1.83 KB
/
index.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
// @flow
import React from 'react';
import ReactDOM from 'react-dom';
import Recorder from './Recorder';
import Root from './Root';
import polyfillAudioElement from './polyfills/polyfill-audio-element';
import polyfillVideoElement from './polyfills/polyfill-video-element';
import './state';
import './index.css';
const mediaElements = new Set();
function mountRecorder(mediaElement) {
const root = new Root(mediaElement);
const rootNode = root.render();
// $FlowFixMe
document.body.appendChild(rootNode);
ReactDOM.render(<Recorder mediaElement={mediaElement} />, rootNode);
window.addEventListener('mousemove', (e) => {
const rect = mediaElement.getBoundingClientRect();
const isBoundedX = e.pageX > rect.left && e.pageX < rect.left + rect.width;
const isBoundedY = e.pageY > rect.top && e.pageY < rect.top + rect.height;
const isBounded = isBoundedX && isBoundedY;
if (isBounded) {
root.show();
} else {
root.hide();
}
});
}
function setMediaElements() {
const audioElements = Array.from(document.getElementsByTagName('audio'));
const videoElements = Array.from(document.getElementsByTagName('video'));
const newMediaElements = [...audioElements, ...videoElements];
newMediaElements
.filter(mediaElement => !mediaElements.has(mediaElement))
.forEach(async (mediaElement) => {
mediaElements.add(mediaElement);
if (mediaElement instanceof HTMLAudioElement) {
await polyfillAudioElement(mediaElement);
}
if (mediaElement instanceof HTMLVideoElement) {
await polyfillVideoElement(mediaElement);
}
mountRecorder(mediaElement);
});
}
(function main() {
setMediaElements();
const mutObs = new MutationObserver(setMediaElements);
// $FlowFixMe
mutObs.observe(document.body, {
childList: true,
subtree: true,
});
}());