-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
153 lines (130 loc) · 5.22 KB
/
script.js
File metadata and controls
153 lines (130 loc) · 5.22 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
document.addEventListener("DOMContentLoaded", () => {
const genForm = document.getElementById("genForm");
const outputEl = document.getElementById("output");
const outputSection = document.getElementById("outputSection");
const loadingEl = document.getElementById("loading");
const downloadOptions = document.getElementById("downloadOptions");
const downloadPdfBtn = document.getElementById("downloadPdfBtn");
const downloadWordBtn = document.getElementById("downloadWordBtn");
const downloadTxtBtn = document.getElementById("downloadTxtBtn");
// Load previous result from local storage
const lastGenerated = localStorage.getItem("lastGenerated");
if (lastGenerated) {
outputEl.textContent = lastGenerated;
outputSection.style.display = "block";
downloadOptions.style.display = "block";
}
genForm.addEventListener("submit", async (event) => {
event.preventDefault();
const apiKey = "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";
const contentType = document.getElementById("contentType").value;
const gradeLevel = document.getElementById("gradeLevel").value;
const topic = document.getElementById("topic").value.trim();
const difficulty = document.getElementById("difficulty").value;
if (topic.length < 3) {
alert("Please enter a valid topic.");
return;
}
const prompt = `Write a ${difficulty.toLowerCase()} ${contentType.toLowerCase()} for Grade ${gradeLevel.replace(
"Grade ",
""
)} students on "${topic}". Use section titles with no markdown symbols. Include explanations, bullet points, and examples where helpful. Do not include user prompts, internal notes, disclaimers, or any references to AI. Only return the final student-facing content.`;
outputEl.textContent = "";
loadingEl.style.display = "block";
outputSection.style.display = "none";
downloadOptions.style.display = "none";
try {
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "mistralai/mistral-small-3.2-24b-instruct:free",
messages: [{ role: "user", content: prompt }]
})
});
if (!response.ok) {
throw new Error(`Error ${response.status}: ${await response.text()}`);
}
const data = await response.json();
let content = data.choices?.[0]?.message?.content || "⚠️ No content returned.";
// Clean out unwanted preamble or markdown symbols
content = content
.replace(/^.*?(?=Title:|Lesson:|Introduction|Photosynthesis)/is, "")
.replace(/^#+\s*/gm, "")
.replace(/User\sasks:.*$/gi, "")
.replace(/Prompt:.*$/gi, "")
.replace(/As\s(an\s)?AI.*$/gi, "")
.replace(/Disclaimer:.*$/gi, "")
.trim();
outputEl.textContent = content;
outputSection.style.display = "block";
downloadOptions.style.display = "block";
localStorage.setItem("lastGenerated", content);
} catch (err) {
outputEl.textContent = `⚠️ Error: ${err.message}`;
outputSection.style.display = "block";
downloadOptions.style.display = "none";
} finally {
loadingEl.style.display = "none";
}
});
// PDF Download Handler
downloadPdfBtn.addEventListener("click", () => {
const text = outputEl.textContent;
if (!text.trim()) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
// Split the text into lines that fit the PDF
const lines = doc.splitTextToSize(text, 180);
// Add content to PDF
doc.setFontSize(12);
doc.text(lines, 10, 10);
// Save the PDF
doc.save("generated_content.pdf");
});
// Word Download Handler
downloadWordBtn.addEventListener("click", () => {
const text = outputEl.textContent;
if (!text.trim()) return;
// Create HTML content for Word document
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Generated Content</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.5; }
h1, h2, h3 { color: #2a2a2a; }
</style>
</head>
<body>
${text.replace(/\n/g, "<br>")}
</body>
</html>
`;
// Create Blob and download
const blob = new Blob(['\ufeff', html], { type: 'application/msword' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'generated_content.doc';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Text Download Handler
downloadTxtBtn.addEventListener("click", () => {
const text = outputEl.textContent;
if (!text.trim()) return;
const blob = new Blob([text], { type: "text/plain" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "generated_content.txt";
a.click();
});
});