-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
199 lines (182 loc) · 6.72 KB
/
script.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const apiUrl = "https://api.groq.com/openai/v1/chat/completions";
// var apiKey = "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 ";
var apiKey= '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'
// var apiKey = "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";
async function generateHashtags() {
const textInput = document.getElementById("textInput").value;
function wordsCount(textInput) {
let totalChars = textInput.length;
console.log("The total charachters in the paragraph is : "+totalChars);
wordsCountToast(totalChars);
}
wordsCount(textInput);
const messages = [
{
role: "system",
content:
"You are a SEO expert who have to generate hashtags and you have to extract all the relevant keywords from the paragraph and after that you have to generate the SEO friendly hashtags for the paragraph based on the keywords and current trends and at the end regenerate the paragraph using the extracted SEO friendly keywords and add the hashtags at the end of the paragraph. insert hashtags at the end of the paragaraph",
},
{
role: "user",
content: textInput,
},
];
try {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"response-format": "json_object",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({ messages, model: "mixtral-8x7b-32768" }),
});
console.log(response);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
const data = await response.json();
console.log(data);
resultLoadedToast();
const content = data["choices"][0].message.content;
// console.log(content)
const sections = content.split("\n\n");
console.log(sections);
// Extract keywords
const keywords = sections[0].split(": ")[1].split(", ");
// Extract hashtags
const hashtags = sections[1].split(": ")[1].split(", ");
// Extract regenerated text
const regeneratedText = sections.slice(2).join("\n\n");
console.log(regeneratedText);
// Display the extracted parts
document.getElementById("keywords").innerHTML =
"<strong>Keywords:</strong><br>" +
keywords.join(", ") +
`<br><img onclick="copiedKeywordsToast()" src="copy-icon.png" alt="" class="copyIcon">`;
document.getElementById("hashtags").innerHTML =
"<strong>SEO friendly hashtags:</strong><br>" +
hashtags.join(", ") +
`<br><img onclick="copiedHashtagToast ()" src="copy-icon.png" alt="" class="copyIcon">`;
document.getElementById("regeneratedText").innerHTML =
"<strong>Regenerated paragraph:</strong><br> " +
regeneratedText +
`<br><img onclick="copiedRegeneratedToast()" src="copy-icon.png" alt="" class="copyIcon">`;
const copyIcons = document.getElementsByClassName("copyIcon");
for (let i = 0; i < copyIcons.length; i++) {
copyIcons[i].addEventListener("click", function () {
let textToCopy = this.previousSibling.textContent.trim();
const textarea = document.createElement("textarea");
textarea.value = textToCopy;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
// alert("Text copied to clipboard!");
document.body.showToast();
});
}
// Scroll to the contentDiv smoothly
document
.querySelector(".contentBox")
.scrollIntoView({ behavior: "smooth" });
} catch (error) {
console.error("Error:", error);
}
}
function copiedKeywordsToast() {
Toastify({
text: "Copied Keywords to clipboard!",
duration: 9000,
destination: "https://github.com/apvarun/toastify-js",
newWindow: true,
close: true,
gravity: "top", // `top` or `bottom`
position: "right", // `left`, `center` or `right`
stopOnFocus: true, // Prevents dismissing of toast on hover
style: {
background: "linear-gradient(to right, #00b09b, #96c93d)",
},
onClick: function () {}, // Callback after click
}).showToast();
}
function copiedHashtagToast() {
Toastify({
text: " Copied Hashtags to clipboard!",
duration: 9000,
destination: "https://github.com/apvarun/toastify-js",
newWindow: true,
close: true,
gravity: "top", // `top` or `bottom`
position: "right", // `left`, `center` or `right`
stopOnFocus: true, // Prevents dismissing of toast on hover
style: {
background: "linear-gradient(to right, #00b09b, #96c93d)",
},
onClick: function () {}, // Callback after click
}).showToast();
}
function copiedRegeneratedToast() {
Toastify({
text: "Copied Regenerated Text to clipboard!",
duration: 9000,
destination: "https://github.com/apvarun/toastify-js",
newWindow: true,
close: true,
gravity: "top", // `top` or `bottom`
position: "right", // `left`, `center` or `right`
stopOnFocus: true, // Prevents dismissing of toast on hover
style: {
background: "linear-gradient(to right, #00b09b, #96c93d)",
},
onClick: function () {}, // Callback after click
}).showToast();
}
function loadingToast() {
Toastify({
text: "Wait plz!!! we are generating hashtags for you...",
duration: 3000,
destination: "https://github.com/apvarun/toastify-js",
newWindow: true,
close: true,
gravity: "top", // `top` or `bottom`
position: "left", // `left`, `center` or `right`
stopOnFocus: true, // Prevents dismissing of toast on hover
style: {
background: "linear-gradient(to right, #00b09b, #96c93d)",
},
onClick: function () {}, // Callback after click
}).showToast();
}
function wordsCountToast(totalWords) {
Toastify({
text: "The total words in your Paragraph are : "+totalWords,
duration: 5000,
destination: "https://github.com/apvarun/toastify-js",
newWindow: true,
close: true,
gravity: "top", // `top` or `bottom`
position: "right", // `left`, `center` or `right`
stopOnFocus: true, // Prevents dismissing of toast on hover
style: {
background: "linear-gradient(to right, #00b09b, #96c93d)",
},
onClick: function () {}, // Callback after click
}).showToast();
}
function resultLoadedToast() {
Toastify({
text: "Congratulations!!! Your result has been loaded",
duration: 5000,
destination: "https://github.com/apvarun/toastify-js",
newWindow: true,
close: true,
gravity: "top", // `top` or `bottom`
position: "right", // `left`, `center` or `right`
stopOnFocus: true, // Prevents dismissing of toast on hover
style: {
background: "linear-gradient(to right, #00b09b, #96c93d)",
},
onClick: function () {}, // Callback after click
}).showToast();
}