-
Notifications
You must be signed in to change notification settings - Fork 23
/
background.js
74 lines (61 loc) · 1.97 KB
/
background.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
const DELAY_IN_MS = 3000;
const VIDEO_LENGTH_IN_MS = 20000;
const MAX_Z_INDEX = 2147483647;
const SHOULD_CENAFY = Math.floor(Math.random() * 100) === 69;
let hasLearnedWhoTheChampIs = false;
let timeoutId;
function getCenafyVideo() {
const video = document.createElement("video");
video.src = chrome.runtime.getURL("cena.mp4");
Object.assign(video.style, {
position: "fixed",
background: "black",
zIndex: MAX_Z_INDEX,
height: "100vh",
width: "100vw",
inset: 0,
});
return video;
}
function cenafy() {
if (hasLearnedWhoTheChampIs) {
return;
}
// This acts as a basic debounce so that if the user is doing
// something click intensive, we don't show them who the champ is
// until they've taken a brief break
if (timeoutId != null) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
// Don't show the video if the tab isn't active. We'll show
// them who the champ is next time
if (document.hidden) {
return;
}
const body = document.body;
const previousPointerEvents = body.style.pointerEvents;
body.style.pointerEvents = "none";
const previousBackgroundColor = body.style.backgroundColor;
body.style.backgroundColor = "black";
const video = getCenafyVideo();
body.appendChild(video);
// Prevent future clicks from spawning additional videos while
// this is playing. In theory, pointer-events: none should guard
// against this but child nodes could have pointer-events set
// explicitly
window.removeEventListener("mouseup", cenafy);
video.addEventListener("ended", () => {
body.style.backgroundColor = previousBackgroundColor;
body.style.pointerEvents = previousPointerEvents;
body.removeChild(video);
hasLearnedWhoTheChampIs = true;
});
video.play();
}, DELAY_IN_MS);
}
if (SHOULD_CENAFY) {
// Add this to mouse-up instead of on load so we can auto-play
// the video with sound
window.addEventListener("mouseup", cenafy);
}