File size: 1,831 Bytes
c922e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// استهداف عناصر HTML
const promptInput = document.getElementById("prompt");
const generateButton = document.getElementById("generate");
const outputDiv = document.getElementById("output");

// مفتاح API الخاص بـ Gemini (استبدله بمفتاحك)
const GEMINI_API_KEY = "AIzaSyBlkMn-zVlQyOERE4cLjnI-d24eMf5slGo";
const GEMINI_API_URL = "https://api.gemini.com/v1/your-endpoint"; // عدّل الرابط إذا كان مختلفًا

// عند الضغط على الزر "Generate"
generateButton.addEventListener("click", async () => {
  const prompt = promptInput.value;

  if (!prompt) {
    outputDiv.textContent = "Please enter a topic or prompt!";
    return;
  }

  // تنظيف منطقة الإخراج
  outputDiv.textContent = "Generating your article...";

  try {
    // إرسال طلب إلى Gemini API
    const response = await fetch(GEMINI_API_URL, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${GEMINI_API_KEY}`, // مفتاح المصادقة
      },
      body: JSON.stringify({
        prompt: prompt,
        max_tokens: 500, // عدد الكلمات أو الرموز المطلوبة
      }),
    });

    // التحقق من حالة الاستجابة
    if (!response.ok) {
      throw new Error(`Error: ${response.statusText}`);
    }

    // معالجة النتيجة
    const data = await response.json();
    const article = data.choices[0].text; // تعديل بناءً على شكل استجابة Gemini

    // عرض المقالة في منطقة الإخراج
    outputDiv.textContent = article;
  } catch (error) {
    // معالجة الأخطاء
    outputDiv.textContent = `An error occurred: ${error.message}`;
  }
});