-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
105 lines (99 loc) · 3.63 KB
/
popup.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
document.addEventListener("DOMContentLoaded", function () {
// Function to format time in HH:MM:SS
function formatTime(seconds) {
const h = Math.floor(seconds / 3600)
.toString()
.padStart(2, "0");
const m = Math.floor((seconds % 3600) / 60)
.toString()
.padStart(2, "0");
const s = Math.floor(seconds % 60)
.toString()
.padStart(2, "0");
return `${h}:${m}:${s}`;
}
// Function to update durations
function updateDurations(result) {
const totalElement = document.getElementById("total-duration");
const watchedElement = document.getElementById("watched-duration");
const remainingElement = document.getElementById("remaining-duration");
const estimatedDurations = {
1.25: {
total: document.getElementById("estimated-total-1-25"),
watched: document.getElementById("estimated-watched-1-25"),
remaining: document.getElementById("estimated-remaining-1-25"),
},
1.5: {
total: document.getElementById("estimated-total-1-5"),
watched: document.getElementById("estimated-watched-1-5"),
remaining: document.getElementById("estimated-remaining-1-5"),
},
1.75: {
total: document.getElementById("estimated-total-1-75"),
watched: document.getElementById("estimated-watched-1-75"),
remaining: document.getElementById("estimated-remaining-1-75"),
},
2: {
total: document.getElementById("estimated-total-2"),
watched: document.getElementById("estimated-watched-2"),
remaining: document.getElementById("estimated-remaining-2"),
},
};
if (result.totalDuration !== undefined) {
totalElement.textContent = `Total: ${formatTime(result.totalDuration)}`;
}
if (result.watchedDuration !== undefined) {
watchedElement.textContent = `Watched: ${formatTime(
result.watchedDuration
)}`;
}
if (result.remainingDuration !== undefined) {
remainingElement.textContent = `Remaining: ${formatTime(
result.remainingDuration
)}`;
// Calculate and display estimated durations for different speeds
for (const speed in estimatedDurations) {
const speedFloat = parseFloat(speed);
estimatedDurations[speed].total.textContent = ` ${formatTime(
result.totalDuration / speedFloat
)}`;
estimatedDurations[speed].watched.textContent = ` ${formatTime(
result.watchedDuration / speedFloat
)}`;
estimatedDurations[
speed
].remaining.textContent = ` ${formatTime(
result.remainingDuration / speedFloat
)}`;
}
}
}
// Check if the current tab's URL is a YouTube URL
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const activeTab = tabs[0];
const url = new URL(activeTab.url);
if (url.hostname === "www.youtube.com" || url.hostname === "youtube.com") {
// Retrieve values from Chrome storage and update durations
chrome.storage.local.get(
[
"totalDuration",
"watchedDuration",
"remainingDuration",
"playlistVisible",
],
function (result) {
if (result.playlistVisible) {
updateDurations(result);
} else {
document.body.innerHTML =
"<p>No playlist is currently open. Please open a YouTube playlist to see the durations.</p>";
}
}
);
} else {
// Display a message indicating that the user needs to be on YouTube
document.body.innerHTML =
"<p>Please open a YouTube playlist to see the durations.</p>";
}
});
});