-
Notifications
You must be signed in to change notification settings - Fork 3
/
sidepanel.js
191 lines (164 loc) · 6.03 KB
/
sidepanel.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
let apiKey = ''; // NEVER store api keys in source code. This is why we use the storage API and prompt the user to enter their API key in the side panel.
function showApiKeyForm() {
document.getElementById('apiKeyForm').style.display = 'block';
document.getElementById('status').textContent = 'Please enter your Runway API Key to continue.';
}
function hideApiKeyForm() {
document.getElementById('apiKeyForm').style.display = 'none';
}
document.getElementById('saveApiKey').addEventListener('click', () => {
const inputApiKey = document.getElementById('apiKey').value.trim();
if (inputApiKey) {
chrome.storage.local.set({ apiKey: inputApiKey }, () => {
apiKey = inputApiKey;
hideApiKeyForm();
document.getElementById('status').textContent = 'API Key saved. You can now generate videos.';
});
}
});
// Check for stored API key
chrome.storage.local.get("apiKey", (result) => {
if (result.apiKey) {
apiKey = result.apiKey;
hideApiKeyForm();
} else {
showApiKeyForm();
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "generateVideo") {
if (apiKey) {
generateVideo(message.imageUrl);
} else {
showApiKeyForm();
}
}
});
function createImageAndLoader(imageUrl) {
const container = document.createElement('div');
container.className = 'image-container';
const imageContainer = document.createElement('div');
imageContainer.className = 'image-container';
const loader = document.createElement('div');
loader.className = 'loader';
const loaderText = document.createElement('div');
loaderText.textContent = 'Generating video...';
const newImage = document.createElement('img');
newImage.src = imageUrl;
newImage.style.width = '100%';
newImage.style.height = 'auto';
imageContainer.appendChild(loader);
imageContainer.appendChild(newImage);
container.appendChild(imageContainer);
container.appendChild(loaderText);
return container;
}
async function generateVideo(imageUrl) {
if (!apiKey) {
showApiKeyForm();
return;
}
const statusElement = document.getElementById('status');
statusElement.textContent = '';
const newImage = createImageAndLoader(imageUrl);
document.getElementById('videoContainer').insertBefore(newImage, document.getElementById('videoContainer').firstChild);
try {
const taskId = await startVideoGeneration(imageUrl);
newImage.id = `image-${taskId}`;
const videoUrl = await pollForCompletion(taskId);
document.getElementById(`image-${taskId}`).remove();
renderVideo(videoUrl);
saveVideoData(imageUrl, videoUrl);
} catch (error) {
newImage.remove();
statusElement.textContent = `Error: ${error.message}`;
}
}
async function startVideoGeneration(imageUrl) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: "startVideoGeneration", imageUrl, apiKey }, (response) => {
if (response.success) {
resolve(response.taskId);
} else {
reject(new Error(response.error));
}
});
});
}
async function pollForCompletion(taskId) {
const statusElement = document.getElementById('status');
while (true) {
try {
const videoUrl = await new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: "pollForCompletion", taskId, apiKey }, (response) => {
if (response.success) {
resolve(response.videoUrl);
} else if (response.error === 'still_processing') {
reject(new Error('still_processing'));
} else {
reject(new Error(response.error));
}
});
});
return videoUrl;
} catch (error) {
if (error.message === 'still_processing') {
statusElement.textContent = '';
await new Promise(resolve => setTimeout(resolve, 2000));
} else {
throw error;
}
}
}
}
function renderVideo(videoUrl) {
const statusElement = document.getElementById('status');
statusElement.textContent = '';
const videoContainer = document.getElementById('videoContainer');
const videoWrapper = document.createElement('div');
videoWrapper.className = 'video-wrapper';
videoWrapper.innerHTML = `
<video controls autoplay muted loop src="${videoUrl}"></video>
`;
videoContainer.insertBefore(videoWrapper, videoContainer.firstChild);
const removeButton = document.createElement('button');
removeButton.className = 'remove-button';
removeButton.setAttribute('data-url', videoUrl);
removeButton.innerHTML = `<svg width="19" height="19" viewBox="0 0 19 19" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M17.97 17.26L10.19 9.49L17.97 1.71L17.26 1L9.49 8.78L1.71 1L1 1.71L8.78 9.49L1 17.26L1.71 17.97L9.49 10.19L17.26 17.97L17.97 17.26Z" fill="#0C0C0C"/>
</svg>
`;
videoWrapper.appendChild(removeButton);
// Add event listener to the new remove button
videoWrapper.querySelector('.remove-button').addEventListener('click', removeVideo);
}
function saveVideoData(imageUrl, videoUrl) {
chrome.storage.local.get('generatedVideos', (result) => {
const videos = result.generatedVideos || [];
videos.push({ imageUrl, videoUrl, timestamp: Date.now() });
chrome.storage.local.set({ generatedVideos: videos });
});
}
function removeVideo(event) {
const videoUrl = event.target.getAttribute('data-url');
const videoWrapper = event.target.closest('.video-wrapper');
chrome.storage.local.get('generatedVideos', (result) => {
let videos = result.generatedVideos || [];
videos = videos.filter(video => video.videoUrl !== videoUrl);
chrome.storage.local.set({ generatedVideos: videos }, () => {
videoWrapper.remove();
});
});
}
// Load previously generated videos
function loadSavedVideos() {
chrome.storage.local.get('generatedVideos', (result) => {
const videos = result.generatedVideos || [];
const videoContainer = document.getElementById('videoContainer');
videoContainer.innerHTML = ''; // Clear existing videos
videos.reverse().forEach(video => {
renderVideo(video.videoUrl);
});
});
}
loadSavedVideos()