-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-gpt-hack.js
233 lines (196 loc) · 7.82 KB
/
html-gpt-hack.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Serve it locally:
Terminal 1:
$ python -m http.server 8080
Terminal 2:
$ ngrok http --domain=caiman-central-perch.ngrok-free.app 18080
Embed me into production Open edX using the following script snippet:
<script>var NELC_API_URL = xyz</script>
<script src="https://cdn.jsdelivr.net/gh/Zeit-Labs/HackGPTforHTML@v5/html-gpt-hack.js"></script>
Embed me into development Open edX using the following script snippet:
<script>var NELC_API_URL = xyz</script>
<script src="https://caiman-central-perch.ngrok-free.app/html-gpt-hack.js"></script>
*/
;(function () {
console.log('tinymce: Hello it the snippet!')
console.log('tinymce: API URL is: ', NELC_API_URL)
const apiCall = function (system, prompt, callback) {
console.time('tinymce: API call')
fetch(
`${NELC_API_URL}?` +
new URLSearchParams({
system: system,
message: prompt,
})
)
.then((response) => response.json())
.then((data) => {
console.info('tinymce: result', data)
console.timeEnd('tinymce: API call')
callback(data.result)
})
.catch((error) => {
console.timeEnd('tinymce: API call')
console.error('tinymce: Error:', error)
})
}
const getTextarea = () => {
const textarea = $('.modal-window textarea')
return textarea
}
function makeRandomId(length) {
const characters =
'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789'
return Array.from(crypto.getRandomValues(new Uint8Array(length)))
.map((byte) => characters[byte % characters.length])
.join('')
}
const getMCE = () => {
return tinymce.get(getTextarea().attr('id'))
}
const hasContentInMCE = () => {
const contentLength = getMCE()?.getContent()?.trim()?.length
return contentLength && contentLength > 20
}
const basePrompter = (window.basePrompter = function (
system,
prompt,
callback
) {
const mce = getMCE()
apiCall(system, prompt, (html) => {
console.info('tinymce: apiCall called back to basePrompter')
mce.setContent(html)
callback ? callback('done') : null
})
})
const prompter = (window.prompter = function (prompt, callback) {
const parentClass = `n-gpt-${makeRandomId(9)}`
const system = `
You are a TinyMCE (WYSIWYG Editor), HTML, CSS and JS master. and ready to help with writing some row html to be inserted to a TinyMCE.
You will also take security seriously and will never return any snippets that facilitates XSS or any other injection or security issues under any circumstances.
Retun only HTML, CSS, and Javascript without any additional texts before or after, as this will be inserted automatically to the editor.
Return the HTML in the following structure without head, body, html and other parent elements:
"""
<!-- in comments explain what the html is doing in natural language in details -->
<style> {{ STYLE GOES HERE }} </style>
<div class="${parentClass}"> {{ CONTENT GOES HERE }} </div>
<script> {{ JAVASCRIPT GOES HERE }} </script>
"""
IMPORTANT: All CSS rules should be scoped in ".${parentClass}" CSS class.
If the user provides an HTML between "============ START OF USER HTML ============" and "============ END OF USER HTML ==============", edit the html to match the provided prompt and update the comment to include the last changes also.
`
if (hasContentInMCE()) {
prompt = `
Given the HTML below:
============ START OF USER HTML ============
${getMCE().getContent()}
============ END OF USER HTML ==============
Here's what I want: ${prompt}
`
}
basePrompter(system, prompt, callback)
})
const getPlaceholder = () => {
return hasContentInMCE()
? 'عدّل على المحتوى باستخدام الذكاء الإصطناعي'
: 'أكتب لإنشاء محتوى بالذكاء الإصطناعي'
}
let inputer = () => {
return `
<div>
<style>
.gpt-nelc-input-wrapper {
margin-bottom: 1.5em;
}
.gpt-nelc-input-wrapper .setting-input {
border: 2px solid rgb(178, 178, 178, 0.5);
width: 98%;
margin-inline-start: -2%;
top: -9px;
position: relative;
height: 49px;
border-radius: 4px;
outline: none;
box-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
}
.gpt-nelc-input-wrapper .setting-input.gpt-loading {
animation: gpt-glow 1.5s ease-in-out infinite;
color: #868686;
}
/* Animation keyframes for glowing effect */
@keyframes gpt-glow {
0% {
border-color: rgb(178, 178, 178, 0.5);
}
50% {
border-color: rgba(135,135,135,0.5);
}
0% {
border-color: rgb(178, 178, 178, 0.5);
}
}
.gpt-nelc-input-wrapper .setting-send {
transform: scale(-1, 1);
position: relative;
top: -5px;
color: #0075b4;
right: 3px;
border: none;
background: none;
font-size: 1.5em;
}
.gpt-nelc-input-wrapper .setting-send .fa-spinner {
display: none;
}
.gpt-nelc-input-wrapper .setting-send.gpt-loading .fa-spinner {
display: inline-block;
}
.gpt-nelc-input-wrapper .setting-send.gpt-loading .fa-paper-plane {
display: none;
}
</style>
<div class="gpt-nelc-input-wrapper wrapper-comp-setting">
<form>
<input class="gpt-input input setting-input" placeholder="${getPlaceholder()}" type="text" value="">
<button class="gpt-input action setting-send active" type="button" data-tooltip="أرسل">
<span class="icon fa fa-paper-plane"></span>
<span class="icon fa fa-spinner fa-spin"></span>
</button>
</form>
</div>
</div>
`
}
setInterval(() => {
if (
$('.modal-actions').length &&
!$('.modal-actions .gpt-nelc-input-wrapper').length
) {
console.log('tinymce: exists ', !!$('.edit-xblock-modal').length)
$('.modal-actions').prepend(inputer)
console.log('tinymce: added prompt')
}
}, 200)
const submit = () => {
$('.gpt-nelc-input-wrapper .gpt-input')
.addClass('gpt-loading')
.prop('disabled', true)
prompter($('.gpt-nelc-input-wrapper .setting-input').val(), () => {
$('.gpt-nelc-input-wrapper .gpt-input')
.removeClass('gpt-loading')
.prop('disabled', false)
$('.gpt-nelc-input-wrapper .setting-input')
.val('')
.prop('placeholder', getPlaceholder())
})
}
$('body').on('click', '.gpt-nelc-input-wrapper button', (e) => {
e.preventDefault()
submit()
})
$('body').on('submit', '.gpt-nelc-input-wrapper form', (e) => {
e.preventDefault()
submit()
})
})()