-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-widget.js
More file actions
645 lines (563 loc) · 21.2 KB
/
chat-widget.js
File metadata and controls
645 lines (563 loc) · 21.2 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// Chat Widget Script
(function() {
// Create and inject styles
const styles = `
.n8n-chat-widget {
--chat--color-primary: var(--n8n-chat-primary-color, #854fff);
--chat--color-secondary: var(--n8n-chat-secondary-color, #6b3fd4);
--chat--color-background: var(--n8n-chat-background-color, #ffffff);
--chat--color-font: var(--n8n-chat-font-color, #333333);
font-family: Helvetica, Arial, sans-serif;
}
.n8n-chat-widget .chat-container {
position: fixed;
bottom: 32px;
right: 20px;
z-index: 1000;
display: none;
width: 380px;
height: 600px;
background: var(--chat--color-background);
border-radius: 12px;
box-shadow: 0 8px 32px rgba(133, 79, 255, 0.15);
border: 1px solid rgba(133, 79, 255, 0.2);
overflow: hidden;
font-family: inherit;
}
.n8n-chat-widget .chat-container.position-left {
right: auto;
left: 20px;
}
.n8n-chat-widget .chat-container.open {
display: flex;
flex-direction: column;
}
.n8n-chat-widget .brand-header {
padding: 16px;
display: flex;
align-items: center;
gap: 12px;
border-bottom: 1px solid rgba(133, 79, 255, 0.1);
position: relative;
}
.n8n-chat-widget .close-button {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
background: none;
border: none;
color: var(--chat--color-font);
cursor: pointer;
padding: 4px;
display: flex;
align-items: center;
justify-content: center;
transition: color 0.2s;
font-size: 20px;
opacity: 0.6;
}
.n8n-chat-widget .close-button:hover {
opacity: 1;
}
.n8n-chat-widget .brand-header img {
width: 32px;
height: 32px;
}
.n8n-chat-widget .brand-header span {
font-size: 18px;
font-weight: 500;
color: var(--chat--color-font);
}
.n8n-chat-widget .new-conversation {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
text-align: center;
width: 100%;
max-width: 300px;
}
.n8n-chat-widget .welcome-text {
font-size: 24px;
font-weight: 600;
color: var(--chat--color-font);
margin-bottom: 24px;
line-height: 1.3;
}
.n8n-chat-widget .new-chat-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
width: 100%;
padding: 16px 24px;
background: linear-gradient(135deg, var(--chat--color-primary) 0%, var(--chat--color-secondary) 100%);
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: transform 0.3s;
font-weight: 500;
font-family: inherit;
margin-bottom: 12px;
}
.n8n-chat-widget .new-chat-btn:hover {
transform: scale(1.02);
}
.n8n-chat-widget .message-icon {
width: 20px;
height: 20px;
}
.n8n-chat-widget .response-text {
font-size: 14px;
color: var(--chat--color-font);
opacity: 0.7;
margin: 0;
}
.n8n-chat-widget .chat-interface {
display: none;
flex-direction: column;
height: 100%;
}
.n8n-chat-widget .chat-interface.active {
display: flex;
}
.n8n-chat-widget .chat-messages {
flex: 1;
overflow-y: auto;
padding: 20px;
background: var(--chat--color-background);
display: flex;
flex-direction: column;
}
.n8n-chat-widget .chat-message {
padding: 12px 16px;
margin: 8px 0;
border-radius: 12px;
max-width: 80%;
word-wrap: break-word;
font-size: 14px;
line-height: 1.5;
}
.n8n-chat-widget .chat-message.user {
background: linear-gradient(135deg, var(--chat--color-primary) 0%, var(--chat--color-secondary) 100%);
color: white;
align-self: flex-end;
box-shadow: 0 4px 12px rgba(133, 79, 255, 0.2);
border: none;
}
.n8n-chat-widget .chat-message.bot {
background: var(--chat--color-background);
border: 1px solid rgba(133, 79, 255, 0.2);
color: var(--chat--color-font);
align-self: flex-start;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.n8n-chat-widget .chat-input {
padding: 16px;
background: var(--chat--color-background);
border-top: 1px solid rgba(133, 79, 255, 0.1);
display: flex;
gap: 8px;
}
.n8n-chat-widget .chat-input textarea {
flex: 1;
padding: 12px;
border: 1px solid rgba(133, 79, 255, 0.2);
border-radius: 8px;
background: var(--chat--color-background);
color: var(--chat--color-font);
resize: none;
font-family: inherit;
font-size: 14px;
}
.n8n-chat-widget .chat-input textarea::placeholder {
color: var(--chat--color-font);
opacity: 0.6;
}
.n8n-chat-widget .chat-input button {
background: linear-gradient(135deg, var(--chat--color-primary) 0%, var(--chat--color-secondary) 100%);
color: white;
border: none;
border-radius: 8px;
padding: 0 20px;
cursor: pointer;
transition: transform 0.2s;
font-family: inherit;
font-weight: 500;
}
.n8n-chat-widget .chat-input button:hover {
transform: scale(1.05);
}
.n8n-chat-widget .chat-toggle {
position: fixed;
bottom: 32px;
right: 20px;
width: 60px;
height: 60px;
border-radius: 30px;
background: linear-gradient(135deg, var(--chat--color-primary) 0%, var(--chat--color-secondary) 100%);
color: white;
border: none;
cursor: pointer;
box-shadow: 0 4px 12px rgba(133, 79, 255, 0.3);
z-index: 999;
transition: transform 0.3s;
display: flex;
align-items: center;
justify-content: center;
}
.n8n-chat-widget .chat-toggle.position-left {
right: auto;
left: 20px;
}
.n8n-chat-widget .chat-toggle:hover {
transform: scale(1.05);
}
.n8n-chat-widget .chat-toggle svg {
width: 24px;
height: 24px;
fill: currentColor;
}
.n8n-chat-widget .chat-footer {
padding: 8px;
text-align: center;
background: var(--chat--color-background);
border-top: 1px solid rgba(133, 79, 255, 0.1);
}
.n8n-chat-widget .chat-footer a {
color: var(--chat--color-primary);
text-decoration: none;
font-size: 12px;
opacity: 0.8;
transition: opacity 0.2s;
font-family: inherit;
}
.n8n-chat-widget .chat-footer a:hover {
opacity: 1;
}
/* Einheitliche Typografie in den Chat-Bubbles */
.n8n-chat-widget .chat-message,
.n8n-chat-widget .chat-message p,
.n8n-chat-widget .chat-message li,
.n8n-chat-widget .chat-message td,
.n8n-chat-widget .chat-message pre,
.n8n-chat-widget .chat-message code {
font-size: 14px !important;
line-height: 1.5;
}
/* Falls Markdown Überschriften reinkommen (marked) */
.n8n-chat-widget .chat-message h1,
.n8n-chat-widget .chat-message h2,
.n8n-chat-widget .chat-message h3,
.n8n-chat-widget .chat-message h4,
.n8n-chat-widget .chat-message h5,
.n8n-chat-widget .chat-message h6 {
font-size: 14px !important;
line-height: 1.5;
margin: 0.5em 0 0.4em;
}
/* Option: Listeneinzüge moderat halten */
.n8n-chat-widget .chat-message ul,
.n8n-chat-widget .chat-message ol {
margin: 0.4em 0 0.4em 1.1em;
}
`;
// Inject styles
const styleSheet = document.createElement('style');
styleSheet.textContent = styles;
document.head.appendChild(styleSheet);
// Default configuration
const defaultConfig = {
webhook: {
url: '',
route: ''
},
branding: {
logo: '',
name: '',
welcomeText: '',
responseTimeText: '',
poweredBy: {
text: 'AI automation service provided by ETH Library',
link: 'https://library.ethz.ch/'
}
},
style: {
primaryColor: '',
secondaryColor: '',
position: 'right',
backgroundColor: '#ffffff',
fontColor: '#333333'
}
};
// Merge user config with defaults
const config = window.ChatWidgetConfig ?
{
webhook: { ...defaultConfig.webhook, ...window.ChatWidgetConfig.webhook },
branding: { ...defaultConfig.branding, ...window.ChatWidgetConfig.branding },
style: { ...defaultConfig.style, ...window.ChatWidgetConfig.style }
} : defaultConfig;
// Prevent multiple initializations
if (window.N8NChatWidgetInitialized) return;
window.N8NChatWidgetInitialized = true;
let currentSessionId = '';
// Create widget container
const widgetContainer = document.createElement('div');
widgetContainer.className = 'n8n-chat-widget';
// Set CSS variables for colors
widgetContainer.style.setProperty('--n8n-chat-primary-color', config.style.primaryColor);
widgetContainer.style.setProperty('--n8n-chat-secondary-color', config.style.secondaryColor);
widgetContainer.style.setProperty('--n8n-chat-background-color', config.style.backgroundColor);
widgetContainer.style.setProperty('--n8n-chat-font-color', config.style.fontColor);
const chatContainer = document.createElement('div');
chatContainer.className = `chat-container${config.style.position === 'left' ? ' position-left' : ''}`;
const newConversationHTML = `
<div class="brand-header">
<img src="${config.branding.logo}" alt="${config.branding.name}">
<span>${config.branding.name}</span>
<button class="close-button">×</button>
</div>
<div class="new-conversation">
<h2 class="welcome-text">${config.branding.welcomeText}</h2>
<button class="new-chat-btn">
<svg class="message-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="currentColor" d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H5.2L4 17.2V4h16v12z"/>
</svg>
Chat starten
</button>
<p class="response-text">${config.branding.responseTimeText}</p>
</div>
`;
const chatInterfaceHTML = `
<div class="chat-interface">
<div class="brand-header">
<img src="${config.branding.logo}" alt="${config.branding.name}">
<span>${config.branding.name}</span>
<button class="close-button">×</button>
</div>
<div class="chat-messages"></div>
<div class="chat-input">
<textarea placeholder="Frage hier eingeben..." rows="1"></textarea>
<button type="submit">Senden</button>
</div>
<div class="chat-footer">
<a href="${config.branding.poweredBy.link}" target="_blank">${config.branding.poweredBy.text}</a>
</div>
</div>
`;
chatContainer.innerHTML = newConversationHTML + chatInterfaceHTML;
const toggleButton = document.createElement('button');
toggleButton.className = `chat-toggle${config.style.position === 'left' ? ' position-left' : ''}`;
toggleButton.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M12 2C6.477 2 2 6.477 2 12c0 1.821.487 3.53 1.338 5L2.5 21.5l4.5-.838A9.955 9.955 0 0012 22c5.523 0 10-4.477 10-10S17.523 2 12 2zm0 18c-1.476 0-2.886-.313-4.156-.878l-3.156.586.586-3.156A7.962 7.962 0 014 12c0-4.411 3.589-8 8-8s8 3.589 8 8-3.589 8-8 8z"/>
</svg>`;
widgetContainer.appendChild(chatContainer);
widgetContainer.appendChild(toggleButton);
document.body.appendChild(widgetContainer);
const newChatBtn = chatContainer.querySelector('.new-chat-btn');
const chatInterface = chatContainer.querySelector('.chat-interface');
const messagesContainer = chatContainer.querySelector('.chat-messages');
const textarea = chatContainer.querySelector('textarea');
const sendButton = chatContainer.querySelector('button[type="submit"]');
function generateUUID() {
return crypto.randomUUID();
}
async function startNewConversation() {
currentSessionId = generateUUID();
const data = [{
action: "loadPreviousSession",
sessionId: currentSessionId,
route: config.webhook.route,
metadata: {
userId: ""
}
}];
try {
const response = await fetch(config.webhook.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
const responseData = await response.json();
chatContainer.querySelector('.brand-header').style.display = 'none';
chatContainer.querySelector('.new-conversation').style.display = 'none';
chatInterface.classList.add('active');
const botMessageDiv = document.createElement('div');
botMessageDiv.className = 'chat-message bot';
botMessageDiv.innerHTML = marked.parse(Array.isArray(responseData) ? responseData[0].output : responseData.output);
messagesContainer.appendChild(botMessageDiv);
// Füge target="_blank" zu allen Links hinzu
botMessageDiv.querySelectorAll('a').forEach(a => {
a.setAttribute('target', '_blank');
a.setAttribute('rel', 'noopener noreferrer');
});
messagesContainer.appendChild(botMessageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
} catch (error) {
console.error('Error:', error);
}
}
async function sendMessage(message) {
const messageData = {
action: "sendMessage",
sessionId: currentSessionId,
route: config.webhook.route,
chatInput: message,
metadata: {
userId: ""
}
};
const userMessageDiv = document.createElement('div');
userMessageDiv.className = 'chat-message user';
userMessageDiv.textContent = message;
messagesContainer.appendChild(userMessageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
try {
const response = await fetch(config.webhook.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(messageData)
});
const data = await response.json();
const botMessageDiv = document.createElement('div');
botMessageDiv.className = 'chat-message bot';
botMessageDiv.innerHTML = marked.parse(Array.isArray(data) ? data[0].output : data.output);
messagesContainer.appendChild(botMessageDiv);
// Füge target="_blank" zu allen Links hinzu
botMessageDiv.querySelectorAll('a').forEach(a => {
a.setAttribute('target', '_blank');
a.setAttribute('rel', 'noopener noreferrer');
});
messagesContainer.scrollTop = messagesContainer.scrollHeight;
} catch (error) {
console.error('Error:', error);
}
}
newChatBtn.addEventListener('click', startNewConversation);
sendButton.addEventListener('click', () => {
const message = textarea.value.trim();
if (message) {
sendMessage(message);
textarea.value = '';
}
});
textarea.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
const message = textarea.value.trim();
if (message) {
sendMessage(message);
textarea.value = '';
}
}
});
toggleButton.addEventListener('click', () => {
chatContainer.classList.toggle('open');
});
// Add close button handlers
const closeButtons = chatContainer.querySelectorAll('.close-button');
closeButtons.forEach(button => {
button.addEventListener('click', () => {
chatContainer.classList.remove('open');
});
});
// Neue Funktion: Ladeanimation anzeigen
function showTypingIndicator() {
if (messagesContainer.querySelector('.chat-message.bot.typing')) return;
const typingDiv = document.createElement('div');
typingDiv.className = 'chat-message bot typing';
typingDiv.innerHTML = `
<div class="dot-loader">
<span></span><span></span><span></span>
</div>
`;
messagesContainer.appendChild(typingDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
// Neue Funktion: Ladeanimation entfernen
function removeTypingIndicator() {
const typingDiv = messagesContainer.querySelector('.chat-message.bot.typing');
if (typingDiv) typingDiv.remove();
}
async function sendMessage(message) {
const messageData = {
action: "sendMessage",
sessionId: currentSessionId,
route: config.webhook.route,
chatInput: message,
metadata: {
userId: ""
}
};
const userMessageDiv = document.createElement('div');
userMessageDiv.className = 'chat-message user';
userMessageDiv.textContent = message;
messagesContainer.appendChild(userMessageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
// Zeige Ladeanimation an
showTypingIndicator();
try {
const response = await fetch(config.webhook.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(messageData)
});
const data = await response.json();
// Ladeanimation entfernen
removeTypingIndicator();
const botMessageDiv = document.createElement('div');
botMessageDiv.className = 'chat-message bot';
botMessageDiv.innerHTML = marked.parse(Array.isArray(data) ? data[0].output : data.output);
messagesContainer.appendChild(botMessageDiv);
botMessageDiv.querySelectorAll('a').forEach(a => {
a.setAttribute('target', '_blank');
a.setAttribute('rel', 'noopener noreferrer');
});
messagesContainer.scrollTop = messagesContainer.scrollHeight;
} catch (error) {
console.error('Error:', error);
removeTypingIndicator();
}
}
// ... bestehende Events, neue Zeile bleibt unberührt
// Zusätzlicher Stil für Ladeanimation (kann in styleSheet oder CSS-Datei ergänzt werden)
const loaderStyle = `
.dot-loader {
display: flex;
gap: 6px;
padding: 10px;
padding-left: 16px;
}
.dot-loader span {
width: 8px;
height: 8px;
background-color: var(--chat--color-secondary);
border-radius: 50%;
animation: dot-blink 1.2s infinite ease-in-out both;
}
.dot-loader span:nth-child(2) {
animation-delay: 0.2s;
}
.dot-loader span:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes dot-blink {
0%, 80%, 100% { transform: scale(0); }
40% { transform: scale(1); }
}
`;
const loaderStyleSheet = document.createElement('style');
loaderStyleSheet.textContent = loaderStyle;
document.head.appendChild(loaderStyleSheet);
})();