_ChatGPT5_Free_ / script.js
Nuzwa's picture
Update script.js
b251fc3 verified
const ideaInput = document.getElementById('idea-input');
const longInput = document.getElementById('long-input');
const startBtn = document.getElementById('start-chat-btn');
const output = document.getElementById('chat-output');
const copyBtn = document.getElementById('copy-and-open-btn');
const GPT_URL = 'https://chat.openai.com/'; // Replace with your Custom GPT URL if needed.
const state = {
tone: 'Friendly',
format: 'Paragraph',
length: 'Short',
language: 'English'
};
function handleToggle(e){
const btn = e.target.closest('.toggle');
if(!btn) return;
const row = btn.parentElement;
const group = row.dataset.group;
row.querySelectorAll('.toggle').forEach(b=>{
b.classList.remove('selected');
b.setAttribute('aria-pressed','false');
});
btn.classList.add('selected');
btn.setAttribute('aria-pressed','true');
state[group] = btn.dataset.value;
}
document.querySelectorAll('.btn-row').forEach(r=>r.addEventListener('click', handleToggle));
function typewriter(text, target, speed = 18) {
target.textContent = '';
let i = 0;
const interval = setInterval(() => {
target.textContent += text.charAt(i);
i++;
if (i >= text.length) clearInterval(interval);
}, speed);
}
function formatResponse({ idea, details, tone, format, length, language }) {
const base = {
English: {
intro: `Idea: ${idea || '—'}\nDetails: ${details || '—'}\nTone: ${tone} • Format: ${format} • Length: ${length}\n\n`,
paragraph: `Here’s a ${tone.toLowerCase()} take on your idea. `,
bulletsIntro: `Key points:\n`,
emailIntro: `Subject: ${idea || 'Your Idea'}\n\nHi there,\n\n`,
outro: `\n\n— generated by coachproai`
},
Urdu: {
intro: `آئیڈیا: ${idea || '—'}\nتفصیل: ${details || '—'}\nلہجہ: ${tone} • فارمیٹ: ${format} • طوالت: ${length}\n\n`,
paragraph: `یہ آپ کے آئیڈیا پر ایک ${tone} انداز ہے۔ `,
bulletsIntro: `اہم نکات:\n`,
emailIntro: `موضوع: ${idea || 'آپ کا آئیڈیا'}\n\nالسلام علیکم،\n\n`,
outro: `\n\n— coachproai`
}
}[language];
const detailsByLength = {
Short: language === 'English'
? 'A crisp, single-line value proposition.'
: 'مختصر اور مؤثر ایک جملے کی قدر پیش کش۔',
Medium: language === 'English'
? 'A concise summary with one or two supporting points.'
: 'مختصر خلاصہ ساتھ ایک دو اہم نکات۔',
Detailed: language === 'English'
? 'A detailed explanation with benefits, audience fit, and a gentle CTA.'
: 'تفصیلی وضاحت جس میں فوائد، ہدف صارف اور ہلکی سی کال ٹو ایکشن شامل ہو۔'
}[length];
const contentByFormat = {
'Paragraph': `${base.paragraph}${detailsByLength}`,
'Bullet Points': `${base.bulletsIntro}- ${detailsByLength}\n- Audience relevance\n- Clear next step`,
'Email': `${base.emailIntro}${detailsByLength}\n\nBest regards,\ncoachproai`
}[format];
return `${base.intro}${contentByFormat}${base.outro}`;
}
function handleGenerate() {
const idea = ideaInput.value.trim();
const details = longInput.value.trim();
if (!idea && !details) {
ideaInput.focus();
output.textContent = 'Please enter an idea or details to get started.';
return;
}
const { tone, format, length, language } = state;
const text = formatResponse({ idea, details, tone, format, length, language });
typewriter(text, output);
copyBtn.classList.remove('hidden');
copyBtn.setAttribute('aria-live', 'polite');
}
async function copyAndOpenGPT() {
// Open GPT immediately (best chance to avoid popup blockers)
const opened = window.open(GPT_URL, '_blank');
// Then copy response text (if present)
const text = output.textContent.trim();
try {
if (text) {
await navigator.clipboard.writeText(text);
copyBtn.textContent = '✅ Copied! Opening GPT...';
} else {
copyBtn.textContent = '↗ Opening GPT...';
}
} catch {
copyBtn.textContent = '⚠️ Copy failed • Opening GPT';
} finally {
setTimeout(() => (copyBtn.textContent = '📋 Copy & Paste Now'), 1600);
}
// Optional feedback if popup blocked
if (!opened) {
// Silent fallback; user can allow pop-ups from the browser UI.
}
}
startBtn.addEventListener('click', handleGenerate);
ideaInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') handleGenerate(); });
copyBtn.addEventListener('click', copyAndOpenGPT);