Nuzwa commited on
Commit
4eeafca
·
verified ·
1 Parent(s): 930276a

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +82 -86
script.js CHANGED
@@ -1,96 +1,92 @@
1
- document.addEventListener('DOMContentLoaded', () => {
2
- // Select all necessary HTML elements
3
- const startChatBtn = document.getElementById('start-chat-btn');
4
- const ideaInput = document.getElementById('idea-input');
5
- const chatOutput = document.getElementById('chat-output');
6
-
7
- const toneSelect = document.getElementById('tone-select');
8
- const formatSelect = document.getElementById('format-select');
9
- const lengthSelect = document.getElementById('length-select');
10
- const languageSelect = document.getElementById('language-select');
11
 
12
- // Select the new button
13
- const copyAndOpenBtn = document.getElementById('copy-and-open-btn');
 
 
 
 
 
 
 
14
 
15
- let currentInterval = null;
16
-
17
- // Typewriter function
18
- function typeWriter(element, text, speed = 40) {
19
- if (currentInterval) {
20
- clearInterval(currentInterval);
21
- }
22
-
23
- let i = 0;
24
- element.innerHTML = '';
25
- currentInterval = setInterval(() => {
26
- if (i < text.length) {
27
- element.innerHTML += text.charAt(i);
28
- i++;
29
- } else {
30
- clearInterval(currentInterval);
31
- currentInterval = null;
32
- // Show the "Copy & Open" button once typing is complete
33
- copyAndOpenBtn.classList.remove('hidden');
34
- }
35
- }, speed);
36
  }
 
37
 
38
- // Event listener for the "Start Chat" button
39
- startChatBtn.addEventListener('click', () => {
40
- // Hide the "Copy & Open" button when a new chat starts
41
- copyAndOpenBtn.classList.add('hidden');
42
-
43
- const idea = ideaInput.value.trim();
44
- if (idea === '') {
45
- alert('Please enter your idea first.');
46
- return;
47
- }
48
-
49
- const selectedTone = toneSelect.value;
50
- const selectedFormat = formatSelect.value;
51
- const selectedLength = lengthSelect.value;
52
- const selectedLanguage = languageSelect.value;
53
-
54
- const fullResponse = `Your Idea: "${idea}"
55
 
56
- Selected Options:
57
- - Tone: ${selectedTone}
58
- - Format: ${selectedFormat}
59
- - Length: ${selectedLength}
60
- - Language: ${selectedLanguage}
61
 
62
- ---
63
- Generating content based on this idea...`;
64
-
65
- typeWriter(chatOutput, fullResponse, 30);
66
- });
67
 
68
- // Event listener for the new "Copy & Open" button
69
- copyAndOpenBtn.addEventListener('click', () => {
70
- const textToCopy = chatOutput.innerText;
 
 
 
 
 
 
 
 
71
 
72
- // --- IMPORTANT ---
73
- // REPLACE THE LINK BELOW WITH YOUR ACTUAL CUSTOM GPT LINK
74
- const customGptUrl = 'https://chat.openai.com/g/YOUR-CUSTOM-GPT-ID-HERE';
75
- // --- IMPORTANT ---
 
76
 
77
- // Copy text to clipboard
78
- navigator.clipboard.writeText(textToCopy).then(() => {
79
- // Give user feedback
80
- const originalText = copyAndOpenBtn.innerHTML;
81
- copyAndOpenBtn.innerHTML = '✅ Copied!';
82
-
83
- // Open the link in a new tab
84
- window.open(customGptUrl, '_blank');
85
-
86
- // Revert button text after 2 seconds
87
- setTimeout(() => {
88
- copyAndOpenBtn.innerHTML = originalText;
89
- }, 2000);
 
90
 
91
- }).catch(err => {
92
- console.error('Failed to copy text: ', err);
93
- alert('Could not copy the text. Please do it manually.');
94
- });
95
- });
96
- });
 
1
+ const ideaInput = document.getElementById('idea-input');
2
+ const startBtn = document.getElementById('start-chat-btn');
3
+ const output = document.getElementById('chat-output');
4
+ const copyBtn = document.getElementById('copy-and-open-btn');
 
 
 
 
 
 
5
 
6
+ function typewriter(text, target, speed = 18) {
7
+ target.textContent = '';
8
+ let i = 0;
9
+ const interval = setInterval(() => {
10
+ target.textContent += text.charAt(i);
11
+ i++;
12
+ if (i >= text.length) clearInterval(interval);
13
+ }, speed);
14
+ }
15
 
16
+ function formatResponse({ idea, tone, format, length, language }) {
17
+ const base = {
18
+ English: {
19
+ intro: `Idea: ${idea}\nTone: ${tone} Format: ${format} • Length: ${length}\n\n`,
20
+ paragraph: `Here’s a ${tone.toLowerCase()} take on your idea. `,
21
+ bulletsIntro: `Key points:\n`,
22
+ emailIntro: `Subject: ${idea}\n\nHi there,\n\n`,
23
+ outro: `\n\n— Generated by Brand AI`
24
+ },
25
+ Urdu: {
26
+ intro: `آئیڈیا: ${idea}\nلہجہ: ${tone} • فارمیٹ: ${format} • طوالت: ${length}\n\n`,
27
+ paragraph: `یہ آپ کے آئیڈیا پر ایک ${tone} انداز ہے۔ `,
28
+ bulletsIntro: `اہم نکات:\n`,
29
+ emailIntro: `موضوع: ${idea}\n\nالسلام علیکم،\n\n`,
30
+ outro: `\n\n— برانڈ اے آئی کی جانب سے`
 
 
 
 
 
 
31
  }
32
+ }[language];
33
 
34
+ const detailsByLength = {
35
+ Short: language === 'English'
36
+ ? 'A crisp, single-line value proposition.'
37
+ : 'مختصر اور مؤثر ایک جملے کی قدر پیش کش۔',
38
+ Medium: language === 'English'
39
+ ? 'A concise summary with one or two supporting points.'
40
+ : 'مختصر خلاصہ ساتھ ایک دو اہم نکات۔',
41
+ Detailed: language === 'English'
42
+ ? 'A detailed explanation with benefits, audience fit, and a gentle CTA.'
43
+ : 'تفصیلی وضاحت جس میں فوائد، ہدف صارف اور ہلکی سی کال ٹو ایکشن شامل ہو۔'
44
+ }[length];
 
 
 
 
 
 
45
 
46
+ const contentByFormat = {
47
+ 'Paragraph': `${base.paragraph}${detailsByLength}`,
48
+ 'Bullet Points': `${base.bulletsIntro}- ${detailsByLength}\n- Audience relevance\n- Clear next step`,
49
+ 'Email': `${base.emailIntro}${detailsByLength}\n\nBest regards,\nYour Brand`
50
+ }[format];
51
 
52
+ return `${base.intro}${contentByFormat}${base.outro}`;
53
+ }
 
 
 
54
 
55
+ function handleGenerate() {
56
+ const idea = ideaInput.value.trim();
57
+ if (!idea) {
58
+ ideaInput.focus();
59
+ output.textContent = 'Please enter an idea to get started.';
60
+ return;
61
+ }
62
+ const tone = document.getElementById('tone-select').value;
63
+ const format = document.getElementById('format-select').value;
64
+ const length = document.getElementById('length-select').value;
65
+ const language = document.getElementById('language-select').value;
66
 
67
+ const text = formatResponse({ idea, tone, format, length, language });
68
+ typewriter(text, output);
69
+ copyBtn.classList.remove('hidden');
70
+ copyBtn.setAttribute('aria-live', 'polite');
71
+ }
72
 
73
+ async function copyToClipboard() {
74
+ const text = output.textContent.trim();
75
+ if (!text) return;
76
+ try {
77
+ await navigator.clipboard.writeText(text);
78
+ copyBtn.textContent = '✅ Copied! Open in Custom GPT';
79
+ setTimeout(() => (copyBtn.textContent = '📋 Copy & Open in Custom GPT'), 1500);
80
+ // Optional: open a placeholder URL for your Custom GPT
81
+ // window.open('https://chat.openai.com/', '_blank');
82
+ } catch {
83
+ copyBtn.textContent = '⚠️ Copy failed';
84
+ setTimeout(() => (copyBtn.textContent = '📋 Copy & Open in Custom GPT'), 1500);
85
+ }
86
+ }
87
 
88
+ startBtn.addEventListener('click', handleGenerate);
89
+ ideaInput.addEventListener('keydown', (e) => {
90
+ if (e.key === 'Enter') handleGenerate();
91
+ });
92
+ copyBtn.addEventListener('click', copyToClipboard);