-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontent.js
165 lines (135 loc) · 4.74 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var timeMarkersGlobal = []; // Global variable to store time markers
var currentMarkerIndex = -1; // Current position in the time markers array
var videoElement = null;
console.log('Content script loaded.');
// Function to parse time markers from comment text
function parseTimeMarkers(commentText) {
const timeMarkerRegex = /(\d+:\d+)\s*(\S+)/g;
let match;
let timeMarkers = [];
while ((match = timeMarkerRegex.exec(commentText)) !== null) {
const timeParts = match[1].split(':');
const minutes = parseInt(timeParts[0], 10);
const seconds = parseInt(timeParts[1], 10);
const totalSeconds = (minutes * 60) + seconds;
timeMarkers.push(totalSeconds);
}
// print timeMarkers
console.log('timeMarkers: ', timeMarkers);
return timeMarkers;
}
// Function to attach click event listeners to comments
function addClickListenerToComments() {
var commentSelector = 'ytd-comment-renderer';
var comments = document.querySelectorAll(commentSelector);
comments.forEach(comment => {
comment.addEventListener('click', function (event) {
var commentText = comment.querySelector('#content-text').innerText;
console.log('Comment clicked:', commentText);
// Parse time markers and store in console log
timeMarkersGlobal = parseTimeMarkers(commentText);
currentMarkerIndex = -1; // Reset to start
console.log('Time markers:', timeMarkersGlobal);
});
});
}
function observeComments() {
const commentSection = document.querySelector('ytd-comments'); // Update this selector based on current YouTube layout
if (!commentSection) {
console.log('Waiting for comment section to load...');
setTimeout(observeComments, 1000); // Check again after 1 second
return;
}
console.log('Comment section found. Setting up observer.');
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes.length) {
addClickListenerToComments();
}
});
});
observer.observe(commentSection, { childList: true, subtree: true });
}
function seekToTime(seconds) {
if (!videoElement) {
videoElement = document.querySelector('video');
if (!videoElement) {
console.log('Video element not found.');
return;
}
}
videoElement.currentTime = seconds;
console.log(`Video jumped to ${seconds} seconds.`);
}
function navigateToPrevMarker() {
if (timeMarkersGlobal.length === 0 || currentMarkerIndex <= 0) {
console.log('No previous marker.');
return;
}
currentMarkerIndex--;
seekToTime(timeMarkersGlobal[currentMarkerIndex]);
}
function navigateToNextMarker() {
if (timeMarkersGlobal.length === 0 || currentMarkerIndex >= timeMarkersGlobal.length - 1) {
console.log('No next marker.');
return;
}
currentMarkerIndex++;
seekToTime(timeMarkersGlobal[currentMarkerIndex]);
}
function handleKeydown(event) {
if (event.altKey && event.key === 'q') {
navigateToPrevMarker();
} else if (event.altKey && event.key === 'w') {
navigateToNextMarker();
}
}
document.addEventListener('keydown', handleKeydown);
var lastUrl = location.href;
function checkUrlChange() {
var currentUrl = location.href;
if (currentUrl !== lastUrl) {
timeMarkersGlobal = [];
currentMarkerIndex = -1;
console.log('Page changed, markers reset.');
lastUrl = currentUrl;
observeComments();
}
}
function init() {
if (document.readyState === "complete" || document.readyState === "interactive") {
observeComments();
setInterval(checkUrlChange, 1000);
} else {
document.addEventListener('DOMContentLoaded', function () {
observeComments();
setInterval(checkUrlChange, 1000);
});
}
}
browser.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.action === "getTimestamps") {
sendResponse({
timestamps: timeMarkersGlobal,
currentMarkerIndex: currentMarkerIndex
});
}
}
);
browser.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.action === "getTimestamps") {
sendResponse({
timestamps: timeMarkersGlobal,
currentMarkerIndex: currentMarkerIndex
});
} else if (request.action === "resetTimestamps") {
timeMarkersGlobal = [];
currentMarkerIndex = -1;
sendResponse({ success: true });
console.log('Timestamps and index reset.');
}
}
);
init();