-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
61 lines (42 loc) · 1.45 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
let video;
let time;
let intervalID;
function checkVideo() {
video = document.querySelectorAll("video")[0];
if (video) {
clearInterval(intervalID); // stop the interval when the video element is found
const changeObserver = new MutationObserver(newLength);
changeObserver.observe(video, { attributes: true });
}
else {
console.error("Video element not found")
}
}
intervalID = setInterval(checkVideo, 100); //repeatedly call checkVideo
// Logic for calculating time in minutes and seconds
function calculateTime() {
let duration = video.duration; // gives the duration in seconds
let speed = video.playbackRate;
let reducedSeconds = Math.floor(duration / speed);
let minutes = reducedSeconds / 60;
let remainingFraction = minutes - Math.floor(minutes);
let seconds = Math.floor(remainingFraction * 60);
return { minutes, seconds };
}
// send the length to popup.js
function sendLength() {
let { minutes, seconds } = calculateTime(); // destructuring
time = `${Math.floor(minutes)} min ${seconds} sec`;
chrome.runtime.sendMessage({ time });
console.log(`${Math.floor(minutes)}mins ${seconds}sec`)
}
function newLength() {
sendLength();
}
// Listen for the extension button click
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "getVideoDuration") {
// Send the video duration when the button is clicked
chrome.runtime.sendMessage({ time });
}
});