/
contentScript.js
67 lines (60 loc) · 2.33 KB
/
contentScript.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
let markersShown = false; // State to track whether markers are shown or hidden
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.command === "markVideo") {
hideMarkers();
showMarkers(request.timestamps, "#57DD73");
showMarkers(request.extended_timestamps, "#1EEAFF");
// Toggle the state
markersShown = true;
console.log("done showing")
} else if (request.command === "removeMarkers") {
hideMarkers();
markersShown = false;
}
else if (request.command === "showSimilar") {
hideMarkers();
showMarkers(request.identical_phoneme_timestamps, "#FF9600");
showMarkers(request.similar_phoneme_timestamps, "#D81EFF");
console.log("here");
markersShown = true;
}
else if (request.command === "showAll") {
hideMarkers();
showMarkers(request.timestamps, "#57DD73");
showMarkers(request.extended_timestamps, "#1EEAFF");
showMarkers(request.identical_phoneme_timestamps, "#FF9600");
showMarkers(request.similar_phoneme_timestamps, "#D81EFF");
console.log("HERE");
markersShown = true;
}
});
function hideMarkers() {
// If markers are currently shown, hide them
let markers = document.querySelectorAll('.marker');
markers.forEach(function(marker) {
marker.style.display = 'none';
});
}
function showMarkers(timestamps, color) {
// If markers are currently hidden, show them
let videoElement = document.querySelector("video");
let progressBar = document.querySelector('.ytp-progress-bar');
let videoDuration = videoElement.duration;
if (!Array.isArray(timestamps) || !timestamps.length) {
console.log("no times")
return
}
timestamps.forEach(function(timestamp) {
let position = (timestamp / videoDuration)* 100;
// Create the marker
let marker = document.createElement('div');
marker.className = 'marker'; // Add a class to the marker for easy selection
marker.style.position = 'absolute';
marker.style.height = '100%';
marker.style.width = '2px';
marker.style.backgroundColor = color;
marker.style.left = `${position}%`;
// Append the marker to the progress bar
progressBar.appendChild(marker);
});
}