-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
96 lines (87 loc) · 3.48 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
// popup.js
document.addEventListener('DOMContentLoaded', function () {
const goButton = document.getElementById('goButton');
const userQueryInput = document.getElementById('userQuery');
const statusMessage = document.getElementById('statusMessage');
const resultsContainer = document.getElementById('resultsContainer');
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = Math.floor(seconds % 60);
if (hours > 0) {
return `${hours}:${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}
return `${minutes}:${String(remainingSeconds).padStart(2, '0')}`;
}
function createTimestampElement(timestamp) {
const div = document.createElement('div');
div.className = 'timestamp-result';
const button = document.createElement('button');
button.className = 'timestamp-button';
button.textContent = `Go to ${formatTime(timestamp.seconds)}`;
button.addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ type: 'NAVIGATE_VIDEO', timestamp: timestamp.seconds },
function(response) {
if (response && response.success) {
statusMessage.textContent = `Navigated to ${formatTime(timestamp.seconds)}`;
statusMessage.className = '';
} else {
statusMessage.textContent = 'Failed to navigate to timestamp';
statusMessage.className = 'error';
}
}
);
});
});
const summary = document.createElement('p');
summary.className = 'timestamp-summary';
summary.textContent = timestamp.summary;
div.appendChild(button);
div.appendChild(summary);
return div;
}
goButton.addEventListener('click', function () {
const userQuery = userQueryInput.value.trim();
if (userQuery) {
// Clear previous results
resultsContainer.innerHTML = '';
statusMessage.textContent = 'Searching...';
statusMessage.className = 'loading';
// Send the user query to the background script
chrome.runtime.sendMessage(
{ type: 'USER_QUERY', query: userQuery },
function (response) {
if (response && response.success) {
const timestamps = response.timestamps;
if (timestamps && timestamps.length > 0) {
resultsContainer.innerHTML = ''; // Clear loading state
timestamps.forEach(timestamp => {
resultsContainer.appendChild(createTimestampElement(timestamp));
});
statusMessage.textContent = `Found ${timestamps.length} matching timestamp${timestamps.length === 1 ? '' : 's'}`;
statusMessage.className = '';
} else {
statusMessage.textContent = 'No matching timestamps found';
statusMessage.className = 'error';
}
} else {
statusMessage.textContent = response?.error || 'An error occurred. Please try again.';
statusMessage.className = 'error';
}
}
);
} else {
statusMessage.textContent = 'Please enter a request.';
statusMessage.className = 'error';
}
});
// Add enter key support
userQueryInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
goButton.click();
}
});
});