-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathpopup.js
343 lines (304 loc) · 11.7 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
document.addEventListener('DOMContentLoaded', function () {
const tryOnButton = document.getElementById('tryOn');
const resultDiv = document.getElementById('result');
const loader = document.getElementById('loader');
const loadingMessage = document.getElementById('loadingMessage');
const personImageInput = document.getElementById('personImage');
const cachedImagesDiv = document.getElementById('cachedImages');
let selectedImageUrl = null;
// Load and display cached images
loadCachedImages();
// Load and display last result
loadLastResult();
// Create and append the upload new image button
const uploadNewImage = document.createElement('label');
uploadNewImage.id = 'uploadNewImage';
uploadNewImage.textContent = '+';
uploadNewImage.setAttribute('for', 'personImage');
cachedImagesDiv.appendChild(uploadNewImage);
personImageInput.addEventListener('change', function () {
if (this.files.length > 0) {
const file = this.files[0];
const reader = new FileReader();
reader.onload = function (e) {
uploadNewImage.innerHTML = `
<img src="${e.target.result}" style="width: 100%; height: 100%; object-fit: cover; position: absolute; top: 0; left: 0;">
<span style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 24px; color: white; text-shadow: 0 0 3px rgba(0,0,0,0.5);">+</span>
`;
};
reader.readAsDataURL(file);
tryOnButton.disabled = false;
selectedImageUrl = null;
// Deselect any previously selected cached image
document
.querySelectorAll('.cached-image')
.forEach((img) => img.classList.remove('selected'));
} else {
resetUploadButton();
tryOnButton.disabled = !selectedImageUrl;
}
});
tryOnButton.addEventListener('click', function () {
if (selectedImageUrl) {
startVirtualTryOn(selectedImageUrl);
} else if (personImageInput.files.length > 0) {
const personImageFile = personImageInput.files[0];
uploadImageToCloudinary(personImageFile)
.then((personImageUrl) => {
const newCachedImage = cacheImage(personImageUrl);
selectCachedImage(newCachedImage, personImageUrl);
startVirtualTryOn(personImageUrl);
resetUploadButton();
})
.catch((error) => {
showError('Error: ' + error.message);
console.error('Error uploading image to Cloudinary:', error);
});
} else {
alert('Please select an image or upload a new one.');
}
});
function resetUploadButton() {
uploadNewImage.innerHTML = '<span style="font-size: 24px;">+</span>';
}
function startVirtualTryOn(personImageUrl) {
loader.style.display = 'block';
loadingMessage.style.display = 'block';
resultDiv.textContent = '';
tryOnButton.disabled = true;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentPageUrl = tabs[0].url; // Get the current page URL
const openAIApiKey = localStorage.getItem('openAIApiKey');
chrome.scripting.executeScript(
{
target: { tabId: tabs[0].id },
files: ['content.js'],
},
() => {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: 'getProductImage', openAIApiKey: openAIApiKey },
function (response) {
if (response && response.productImageUrl) {
performVirtualTryOn(
personImageUrl,
response.productImageUrl,
currentPageUrl
);
} else {
showError("Couldn't find product image.");
}
}
);
}
);
});
}
function loadCachedImages() {
const cachedUrls = JSON.parse(
localStorage.getItem('cachedImageUrls') || '[]'
);
cachedUrls.forEach((url) => {
const imgContainer = document.createElement('div');
imgContainer.classList.add('image-container');
const img = document.createElement('img');
img.src = url;
img.classList.add('cached-image');
img.addEventListener('click', () => selectCachedImage(img, url));
const deleteBtn = document.createElement('button');
deleteBtn.textContent = '×';
deleteBtn.classList.add('delete-btn');
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation();
deleteCachedImage(url, imgContainer);
});
imgContainer.appendChild(img);
imgContainer.appendChild(deleteBtn);
cachedImagesDiv.appendChild(imgContainer);
});
}
function deleteCachedImage(url, imgContainer) {
// Remove from localStorage
const cachedUrls = JSON.parse(
localStorage.getItem('cachedImageUrls') || '[]'
);
const updatedUrls = cachedUrls.filter((cachedUrl) => cachedUrl !== url);
localStorage.setItem('cachedImageUrls', JSON.stringify(updatedUrls));
// Remove from DOM
cachedImagesDiv.removeChild(imgContainer);
// Reset selection if the deleted image was selected
if (selectedImageUrl === url) {
selectedImageUrl = null;
tryOnButton.disabled = true;
}
}
function selectCachedImage(imgElement, url) {
document
.querySelectorAll('.cached-image')
.forEach((img) => img.classList.remove('selected'));
imgElement.classList.add('selected');
selectedImageUrl = url;
tryOnButton.disabled = false;
personImageInput.value = '';
resetUploadButton();
}
function cacheImage(url) {
const cachedUrls = JSON.parse(
localStorage.getItem('cachedImageUrls') || '[]'
);
if (!cachedUrls.includes(url)) {
cachedUrls.unshift(url);
if (cachedUrls.length > 5) cachedUrls.pop(); // Keep only the last 5 images
localStorage.setItem('cachedImageUrls', JSON.stringify(cachedUrls));
const imgContainer = document.createElement('div');
imgContainer.classList.add('image-container');
const img = document.createElement('img');
img.src = url;
img.classList.add('cached-image');
img.addEventListener('click', () => selectCachedImage(img, url));
const deleteBtn = document.createElement('button');
deleteBtn.textContent = '×';
deleteBtn.classList.add('delete-btn');
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation();
deleteCachedImage(url, imgContainer);
});
imgContainer.appendChild(img);
imgContainer.appendChild(deleteBtn);
cachedImagesDiv.insertBefore(imgContainer, uploadNewImage);
if (cachedImagesDiv.querySelectorAll('.image-container').length > 5) {
cachedImagesDiv.removeChild(
cachedImagesDiv.children[cachedImagesDiv.children.length - 2]
);
}
return img; // Return the newly created image element
}
return null;
}
function showError(message) {
loader.style.display = 'none';
loadingMessage.style.display = 'none';
resultDiv.innerHTML = message;
tryOnButton.disabled = false;
}
function performVirtualTryOn(
personImageUrl,
productImageUrl,
currentPageUrl
) {
const payload = {
data: [{ path: personImageUrl }, { path: productImageUrl }, 0, true],
};
fetch('https://kwai-kolors-kolors-virtual-try-on.hf.space/call/tryon', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
.then((response) => {
if (!response.ok) {
throw new Error('Error: ' + response.status);
}
response.json().then((data) => {
const eventId = data.event_id;
listenForResult(eventId, productImageUrl, currentPageUrl);
});
})
.catch((error) => {
showError(
'Could not do virtual try-on because kolors is busy,<br/> Please try again or use <a href="https://huggingface.co/spaces/Kwai-Kolors/Kolors-Virtual-Try-On" target="_blank">Huggingface space</a> directly.'
);
console.error('Error in virtual try-on process:', error);
});
}
function listenForResult(eventId, productImageUrl, currentPageUrl) {
fetch(
`https://kwai-kolors-kolors-virtual-try-on.hf.space/call/tryon/${eventId}`
)
.then((response) => {
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let buffer = '';
reader.read().then(function processText({ done, value }) {
if (done) {
return;
}
buffer += decoder.decode(value, { stream: true });
let lines = buffer.split('\n');
for (let i = 0; i < lines.length - 1; i++) {
if (lines[i].startsWith('event:')) {
const event = lines[i].split('event: ')[1];
console.log('event', event);
console.log('lines[i + 1]', lines[i + 1]);
const data = JSON.parse(lines[i + 1].split('data: ')[1]);
console.log('data', data);
if (event === 'complete') {
if (data && data[0]) {
const resultUrl = data[0].url;
if (chrome.storage && chrome.storage.local) {
// Cache the result only if chrome.storage is available
const cacheData = {};
cacheData[currentPageUrl] = resultUrl; // Use current page URL as key
chrome.storage.local.set(cacheData, function () {
console.log('Result cached for', currentPageUrl);
});
}
displayResult(resultUrl);
} else {
showError(
'Could not do virtual try-on because kolors is busy,<br/> Please try again or use <a href="https://huggingface.co/spaces/Kwai-Kolors/Kolors-Virtual-Try-On" target="_blank">Huggingface space</a> directly.'
);
}
return;
} else if (event === 'error') {
showError(
'Could not do virtual try-on because kolors is busy,<br/> Please try again or use <a href="https://huggingface.co/spaces/Kwai-Kolors/Kolors-Virtual-Try-On" target="_blank">Huggingface space</a> directly.'
);
return;
}
}
}
buffer = lines[lines.length - 1];
reader.read().then(processText);
});
})
.catch((error) => {
showError('Error: ' + error.message);
console.error('Error in virtual try-on process:', error);
});
}
function loadLastResult() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentPageUrl = tabs[0].url; // Get the current page URL
chrome.storage.local.get([currentPageUrl], function (result) {
if (result[currentPageUrl]) {
displayResult(result[currentPageUrl]);
}
});
});
}
function displayResult(resultUrl) {
loader.style.display = 'none';
loadingMessage.style.display = 'none';
const img = document.createElement('img');
img.src = resultUrl;
img.style.maxWidth = '100%';
resultDiv.innerHTML = '';
resultDiv.appendChild(img);
}
const settingsButton = document.getElementById('settingsButton');
settingsButton.addEventListener('click', toggleSettings);
checkAndShowSettings();
function checkAndShowSettings() {
const openAIApiKey = localStorage.getItem('openAIApiKey');
const cloudName = localStorage.getItem('cloudName');
const uploadPreset = localStorage.getItem('uploadPreset');
if (!openAIApiKey || !cloudName || !uploadPreset) {
showSettings();
}
}
// chrome.storage.local.clear(function () {
// console.log('Storage cleared');
// });
});