Skip to content

Commit 45a4c34

Browse files
committed
Overlay video is no longer injected until a valid button is detected
1 parent bc6f208 commit 45a4c34

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

js/content.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,42 @@ const overlayHTML = `
1919
</div>
2020
`;
2121

22-
const overlayElement = document.createElement("div");
23-
overlayElement.innerHTML = overlayHTML;
24-
document.body.appendChild(overlayElement);
22+
let videoDiv;
23+
let videoOverlay;
24+
let video;
25+
let skip;
2526

26-
const videoDiv = document.getElementById("volumeDiv");
27-
const videoOverlay = document.getElementById("video-overlay");
28-
const video = document.getElementById("video");
29-
const skip = document.getElementById("skip-button");
27+
let injected = false;
28+
function injectVideo() {
29+
if (injected) return;
30+
31+
const overlayElement = document.createElement("div");
32+
overlayElement.innerHTML = overlayHTML;
33+
document.body.appendChild(overlayElement);
34+
35+
videoDiv = document.getElementById("volumeDiv");
36+
videoOverlay = document.getElementById("video-overlay");
37+
video = document.getElementById("video");
38+
skip = document.getElementById("skip-button");
39+
40+
video.addEventListener("ended", () => {
41+
videoOverlay.classList.remove("show-bevo");
42+
setPlaying(false);
43+
44+
video.muted = false;
45+
});
46+
47+
skip.addEventListener("click", () => {
48+
video.pause();
49+
video.currentTime = 0;
50+
videoOverlay.classList.remove("show-bevo");
51+
setPlaying(false);
52+
});
53+
54+
injected = true;
55+
56+
console.log("Injected video");
57+
}
3058

3159
var eventButtons = [];
3260

@@ -88,6 +116,7 @@ load("playing", null, function (value) {
88116
const type = value[2];
89117

90118
if (wasPlaying && Date.now() / 1000 - time < 4) {
119+
injectVideo();
91120
video.muted = true;
92121
displayBevo(type, true);
93122
} else if (wasPlaying) {
@@ -117,13 +146,6 @@ document.addEventListener("click", () => {
117146
video.muted = false;
118147
});
119148

120-
video.addEventListener("ended", () => {
121-
videoOverlay.classList.remove("show-bevo");
122-
setPlaying(false);
123-
124-
video.muted = false;
125-
});
126-
127149
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
128150
if (request == null) return;
129151

@@ -135,13 +157,6 @@ chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
135157
}
136158
});
137159

138-
skip.addEventListener("click", () => {
139-
video.pause();
140-
video.currentTime = 0;
141-
videoOverlay.classList.remove("show-bevo");
142-
setPlaying(false);
143-
});
144-
145160
/**
146161
* ATTACHING TO BUTTONS
147162
*/
@@ -303,7 +318,7 @@ async function displayBevo(type, skipAnalytics) {
303318
URL = themedVideoURL;
304319
}
305320

306-
console.log("Video is " + (isValid ? "valid" : "invalid"));
321+
console.log("Themed video " + (isValid ? "exists" : "doesn't exist"));
307322
}
308323

309324
video.src = URL;
@@ -336,7 +351,7 @@ function updateVolume(value) {
336351
value = value[1];
337352
volume = clamp(value, 0, 1);
338353

339-
video.volume = volume;
354+
if (video != null) video.volume = volume;
340355
}
341356

342357
function toggle(value) {
@@ -370,6 +385,8 @@ function isValidVideo(url) {
370385

371386
function initButton(button, type) {
372387
if (button != null && !eventButtons.includes(button)) {
388+
injectVideo();
389+
373390
eventButtons.push(button);
374391
button.addEventListener("click", () => {
375392
displayBevo(type);

0 commit comments

Comments
 (0)