File size: 4,569 Bytes
4eeafca
3115bd2
4eeafca
 
 
3ecf899
b251fc3
 
3115bd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eeafca
 
 
 
 
 
 
 
 
96eca1b
3115bd2
4eeafca
 
3115bd2
4eeafca
 
3115bd2
a2fcfed
4eeafca
 
3115bd2
4eeafca
 
3115bd2
a2fcfed
3ecf899
4eeafca
3ecf899
4eeafca
 
 
 
 
 
 
 
 
 
 
3ecf899
4eeafca
 
 
a2fcfed
4eeafca
3ecf899
4eeafca
 
96eca1b
4eeafca
 
3115bd2
 
 
4eeafca
3115bd2
4eeafca
 
96eca1b
3115bd2
 
4eeafca
 
 
 
96eca1b
b251fc3
 
 
 
 
4eeafca
 
b251fc3
 
 
 
 
 
4eeafca
b251fc3
 
 
 
 
 
 
 
4eeafca
 
3ecf899
4eeafca
3115bd2
b251fc3
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
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);