chansung commited on
Commit
6e56735
·
verified ·
1 Parent(s): 31e691e

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +47 -27
script.js CHANGED
@@ -1438,42 +1438,62 @@ document.addEventListener('DOMContentLoaded', () => {
1438
  showConfigModal(); // Show config modal if API key is missing
1439
  return;
1440
  }
1441
- if (typeof JSZip === 'undefined') {
1442
- alert('JSZip library is not loaded. Export cannot proceed.');
1443
- console.error("JSZip is not defined!");
1444
- return;
1445
- }
1446
 
1447
  const originalButtonContent = exportCodeButtonEl.innerHTML;
1448
  exportCodeButtonEl.disabled = true;
1449
- exportCodeButtonEl.innerHTML = '<svg class="animate-spin h-4 w-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg> Exporting...';
 
 
 
1450
 
1451
  try {
1452
- const splitCode = await fetchCodeSplitFromGemini(apiKey, currentCode);
1453
-
1454
- if (!splitCode || typeof splitCode.html_code === 'undefined' || typeof splitCode.css_code === 'undefined' || typeof splitCode.js_code === 'undefined') {
1455
- throw new Error('Invalid response structure from code splitting model.');
1456
- }
 
 
 
 
 
 
 
 
 
 
1457
 
1458
- const zip = new JSZip();
1459
- zip.file("index.html", splitCode.html_code);
1460
- zip.file("style.css", splitCode.css_code);
1461
- zip.file("script.js", splitCode.js_code);
1462
-
1463
- const zipBlob = await zip.generateAsync({ type: "blob" });
1464
-
1465
- const downloadLink = document.createElement('a');
1466
- downloadLink.href = URL.createObjectURL(zipBlob);
1467
- downloadLink.download = "exported_code.zip";
1468
- document.body.appendChild(downloadLink);
1469
- downloadLink.click();
1470
- document.body.removeChild(downloadLink);
1471
- URL.revokeObjectURL(downloadLink.href);
1472
 
1473
- exportCodeButtonEl.innerHTML = originalButtonContent;
 
 
1474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1475
  } catch (error) {
1476
- console.error("Export failed:", error);
1477
  alert(`Export failed: ${error.message}`);
1478
  exportCodeButtonEl.innerHTML = originalButtonContent;
1479
  } finally {
 
1438
  showConfigModal(); // Show config modal if API key is missing
1439
  return;
1440
  }
 
 
 
 
 
1441
 
1442
  const originalButtonContent = exportCodeButtonEl.innerHTML;
1443
  exportCodeButtonEl.disabled = true;
1444
+ exportCodeButtonEl.innerHTML = `<svg class="animate-spin h-4 w-4 inline-block mr-1" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
1445
+ <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
1446
+ <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
1447
+ </svg> Exporting...`;
1448
 
1449
  try {
1450
+ const response = await fetch(`${API_BASE_URL}/chat/completions`, {
1451
+ method: 'POST',
1452
+ headers: {
1453
+ 'Content-Type': 'application/json',
1454
+ 'Authorization': `Bearer ${apiKey}`
1455
+ },
1456
+ body: JSON.stringify({
1457
+ model: 'qwen/qwen3-235b-a22b-fp8',
1458
+ messages: [{
1459
+ role: "user",
1460
+ content: `Please analyze and optimize the following HTML code for export. Ensure it follows best practices and is production-ready:
1461
+
1462
+ \`\`\`html
1463
+ ${currentCode}
1464
+ \`\`\`
1465
 
1466
+ Please provide the optimized code with any necessary improvements for production use.`
1467
+ }],
1468
+ stream: false,
1469
+ max_tokens: MODEL_DETAILS['qwen/qwen3-235b-a22b-fp8'].defaultOutput
1470
+ })
1471
+ });
 
 
 
 
 
 
 
 
1472
 
1473
+ if (!response.ok) {
1474
+ throw new Error(`API Error: ${response.status} ${response.statusText}`);
1475
+ }
1476
 
1477
+ const data = await response.json();
1478
+ const optimizedCode = data.choices[0].message.content;
1479
+
1480
+ // Create a blob with the optimized code
1481
+ const blob = new Blob([optimizedCode], { type: 'text/html' });
1482
+ const url = window.URL.createObjectURL(blob);
1483
+ const a = document.createElement('a');
1484
+ a.href = url;
1485
+ a.download = 'optimized-code.html';
1486
+ document.body.appendChild(a);
1487
+ a.click();
1488
+ window.URL.revokeObjectURL(url);
1489
+ document.body.removeChild(a);
1490
+
1491
+ exportCodeButtonEl.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check inline-block mr-1"><polyline points="20 6 9 17 4 12"></polyline></svg> Exported!`;
1492
+ setTimeout(() => {
1493
+ exportCodeButtonEl.innerHTML = originalButtonContent;
1494
+ }, 2000);
1495
  } catch (error) {
1496
+ console.error('Export error:', error);
1497
  alert(`Export failed: ${error.message}`);
1498
  exportCodeButtonEl.innerHTML = originalButtonContent;
1499
  } finally {