|
1 | 1 | document.addEventListener('DOMContentLoaded', () => { |
2 | 2 | fetchProjects(); |
3 | 3 |
|
| 4 | + // Global API Key storage for the session |
| 5 | + let globalApiKey = null; |
| 6 | + |
| 7 | + function getApiKey(message) { |
| 8 | + if (globalApiKey) return globalApiKey; |
| 9 | + const apiKey = prompt(message || "Please enter your API key:"); |
| 10 | + if (apiKey) { |
| 11 | + globalApiKey = apiKey; |
| 12 | + } |
| 13 | + return apiKey; |
| 14 | + } |
| 15 | + |
4 | 16 | // --- Meta Pay and Stripe Integration --- |
5 | 17 |
|
6 | 18 | let stripe; |
@@ -31,7 +43,7 @@ document.addEventListener('DOMContentLoaded', () => { |
31 | 43 |
|
32 | 44 | async function fetchPaymentIntent() { |
33 | 45 | try { |
34 | | - const apiKey = prompt("Please enter your API key to make a purchase:"); |
| 46 | + const apiKey = getApiKey("Please enter your API key to make a purchase:"); |
35 | 47 | if (!apiKey) { |
36 | 48 | if(paymentStatus) paymentStatus.textContent = 'API key is required to make a purchase.'; |
37 | 49 | return null; |
@@ -213,7 +225,7 @@ document.addEventListener('DOMContentLoaded', () => { |
213 | 225 | const titleInput = document.getElementById('project-title-input'); |
214 | 226 | const descriptionInput = document.getElementById('project-description-input'); |
215 | 227 | const responseContainer = document.getElementById('create-project-response'); |
216 | | - const apiKey = prompt("Please enter your API key to create a project:"); |
| 228 | + const apiKey = getApiKey("Please enter your API key to create a project:"); |
217 | 229 |
|
218 | 230 | if (!apiKey) { |
219 | 231 | responseContainer.textContent = 'API key is required to create a project.'; |
@@ -255,7 +267,7 @@ document.addEventListener('DOMContentLoaded', () => { |
255 | 267 | documentSpecialistBtn.addEventListener('click', async () => { |
256 | 268 | const input = document.getElementById('document-specialist-input'); |
257 | 269 | const responseContainer = document.getElementById('document-specialist-response'); |
258 | | - const apiKey = prompt("Please enter your API key to use the Document Specialist:"); |
| 270 | + const apiKey = getApiKey("Please enter your API key to use the Document Specialist:"); |
259 | 271 |
|
260 | 272 | if (!apiKey) { |
261 | 273 | responseContainer.textContent = 'API key is required.'; |
@@ -293,7 +305,7 @@ document.addEventListener('DOMContentLoaded', () => { |
293 | 305 | promoteStartupBtn.addEventListener('click', async () => { |
294 | 306 | const descriptionInput = document.getElementById('startup-description-input'); |
295 | 307 | const responseContainer = document.getElementById('promote-startup-response'); |
296 | | - const apiKey = prompt("Please enter your API key to generate a promotion:"); |
| 308 | + const apiKey = getApiKey("Please enter your API key to generate a promotion:"); |
297 | 309 |
|
298 | 310 | if (!apiKey) { |
299 | 311 | responseContainer.textContent = 'API key is required to generate a promotion.'; |
@@ -331,7 +343,7 @@ document.addEventListener('DOMContentLoaded', () => { |
331 | 343 | sciencesEducatorBtn.addEventListener('click', async () => { |
332 | 344 | const input = document.getElementById('sciences-educator-input'); |
333 | 345 | const responseContainer = document.getElementById('sciences-educator-response'); |
334 | | - const apiKey = prompt("Please enter your API key to use the Sciences Educator:"); |
| 346 | + const apiKey = getApiKey("Please enter your API key to use the Sciences Educator:"); |
335 | 347 |
|
336 | 348 | if (!apiKey) { |
337 | 349 | responseContainer.textContent = 'API key is required.'; |
@@ -369,7 +381,7 @@ document.addEventListener('DOMContentLoaded', () => { |
369 | 381 | musicInstrumentalistBtn.addEventListener('click', async () => { |
370 | 382 | const input = document.getElementById('music-instrumentalist-input'); |
371 | 383 | const responseContainer = document.getElementById('music-instrumentalist-response'); |
372 | | - const apiKey = prompt("Please enter your API key to use the Music Instrumentalist:"); |
| 384 | + const apiKey = getApiKey("Please enter your API key to use the Music Instrumentalist:"); |
373 | 385 |
|
374 | 386 | if (!apiKey) { |
375 | 387 | responseContainer.textContent = 'API key is required.'; |
@@ -407,7 +419,7 @@ document.addEventListener('DOMContentLoaded', () => { |
407 | 419 | geometryAssistantBtn.addEventListener('click', async () => { |
408 | 420 | const input = document.getElementById('geometry-assistant-input'); |
409 | 421 | const responseContainer = document.getElementById('geometry-assistant-response'); |
410 | | - const apiKey = prompt("Please enter your API key to use the Geometry Assistant:"); |
| 422 | + const apiKey = getApiKey("Please enter your API key to use the Geometry Assistant:"); |
411 | 423 |
|
412 | 424 | if (!apiKey) { |
413 | 425 | responseContainer.textContent = 'API key is required.'; |
@@ -445,7 +457,7 @@ document.addEventListener('DOMContentLoaded', () => { |
445 | 457 | cartographyAssistantBtn.addEventListener('click', async () => { |
446 | 458 | const input = document.getElementById('cartography-assistant-input'); |
447 | 459 | const responseContainer = document.getElementById('cartography-assistant-response'); |
448 | | - const apiKey = prompt("Please enter your API key to use the Cartography Assistant:"); |
| 460 | + const apiKey = getApiKey("Please enter your API key to use the Cartography Assistant:"); |
449 | 461 |
|
450 | 462 | if (!apiKey) { |
451 | 463 | responseContainer.textContent = 'API key is required.'; |
@@ -483,7 +495,7 @@ document.addEventListener('DOMContentLoaded', () => { |
483 | 495 | businessPlanBtn.addEventListener('click', async () => { |
484 | 496 | const input = document.getElementById('business-plan-input'); |
485 | 497 | const responseContainer = document.getElementById('business-plan-response'); |
486 | | - const apiKey = prompt("Please enter your API key to use the Business Plan Creator:"); |
| 498 | + const apiKey = getApiKey("Please enter your API key to use the Business Plan Creator:"); |
487 | 499 |
|
488 | 500 | if (!apiKey) { |
489 | 501 | responseContainer.textContent = 'API key is required.'; |
@@ -521,7 +533,7 @@ document.addEventListener('DOMContentLoaded', () => { |
521 | 533 | investigationRoleBtn.addEventListener('click', async () => { |
522 | 534 | const input = document.getElementById('investigation-role-input'); |
523 | 535 | const responseContainer = document.getElementById('investigation-role-response'); |
524 | | - const apiKey = prompt("Please enter your API key to use the Investigation Role:"); |
| 536 | + const apiKey = getApiKey("Please enter your API key to use the Investigation Role:"); |
525 | 537 |
|
526 | 538 | if (!apiKey) { |
527 | 539 | responseContainer.textContent = 'API key is required.'; |
@@ -559,7 +571,7 @@ document.addEventListener('DOMContentLoaded', () => { |
559 | 571 | militaryAssistanceBtn.addEventListener('click', async () => { |
560 | 572 | const input = document.getElementById('military-assistance-input'); |
561 | 573 | const responseContainer = document.getElementById('military-assistance-response'); |
562 | | - const apiKey = prompt("Please enter your API key to use the Military Services Assistance:"); |
| 574 | + const apiKey = getApiKey("Please enter your API key to use the Military Services Assistance:"); |
563 | 575 |
|
564 | 576 | if (!apiKey) { |
565 | 577 | responseContainer.textContent = 'API key is required.'; |
@@ -597,7 +609,7 @@ document.addEventListener('DOMContentLoaded', () => { |
597 | 609 | podcastAssistanceBtn.addEventListener('click', async () => { |
598 | 610 | const input = document.getElementById('podcast-assistance-input'); |
599 | 611 | const responseContainer = document.getElementById('podcast-assistance-response'); |
600 | | - const apiKey = prompt("Please enter your API key to use the Podcast & Business Podcast Role:"); |
| 612 | + const apiKey = getApiKey("Please enter your API key to use the Podcast & Business Podcast Role:"); |
601 | 613 |
|
602 | 614 | if (!apiKey) { |
603 | 615 | responseContainer.textContent = 'API key is required.'; |
@@ -628,4 +640,44 @@ document.addEventListener('DOMContentLoaded', () => { |
628 | 640 | } |
629 | 641 | }); |
630 | 642 | } |
| 643 | + |
| 644 | + // --- Global Translator --- |
| 645 | + const translatorBtn = document.getElementById('translator-btn'); |
| 646 | + if (translatorBtn) { |
| 647 | + translatorBtn.addEventListener('click', async () => { |
| 648 | + const textInput = document.getElementById('translator-text-input'); |
| 649 | + const languageInput = document.getElementById('translator-language-input'); |
| 650 | + const responseContainer = document.getElementById('translator-response'); |
| 651 | + const apiKey = getApiKey("Please enter your API key to use the Global Translator:"); |
| 652 | + |
| 653 | + if (!apiKey) { |
| 654 | + responseContainer.textContent = 'API key is required.'; |
| 655 | + return; |
| 656 | + } |
| 657 | + |
| 658 | + try { |
| 659 | + const response = await fetch('/api/v1/translate', { |
| 660 | + method: 'POST', |
| 661 | + headers: { |
| 662 | + 'Content-Type': 'application/json', |
| 663 | + 'X-API-Key': apiKey |
| 664 | + }, |
| 665 | + body: JSON.stringify({ |
| 666 | + text: textInput.value, |
| 667 | + target_language: languageInput.value |
| 668 | + }) |
| 669 | + }); |
| 670 | + |
| 671 | + if (!response.ok) { |
| 672 | + const error = await response.json(); |
| 673 | + throw new Error(error.error || 'Failed to get a response from the translator'); |
| 674 | + } |
| 675 | + |
| 676 | + const result = await response.json(); |
| 677 | + responseContainer.textContent = result.message; |
| 678 | + } catch (error) { |
| 679 | + responseContainer.textContent = `Error: ${error.message}`; |
| 680 | + } |
| 681 | + }); |
| 682 | + } |
631 | 683 | }); |
0 commit comments