embeddedaichatux 1.4.1 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/EmbeddedChat.js +57 -5
  2. package/package.json +1 -1
package/EmbeddedChat.js CHANGED
@@ -11,10 +11,11 @@
11
11
  this.previewParam = this.isPreview ? "?isPreview=true" : "";
12
12
  this.serverUrl = this.options.serverUrl || 'https://embedgpt.chat/';
13
13
  this.height = options.height || 600; // Default height
14
- this.width = options.width || 300; // Default width
14
+ this.width = options.width || 320; // Default width
15
15
  this.enableAnimation = options.enableAnimation !== false; // Default to true if not provided
16
16
  this.minimizedHeight = `${this.height * 0.3}px`; // 30% of the full height
17
17
  this.mouseInsideChat = false;
18
+ this.hasRefreshed = false; // Flag to prevent endless loop
18
19
 
19
20
  // Determine transition styles
20
21
  const transitionStyle = this.enableAnimation ? 'transition: height 0.3s ease, opacity 0.3s ease;' : '';
@@ -29,24 +30,56 @@
29
30
  this.positionStyle = `width:100%; height:${this.height}px; ${transitionStyle}`;
30
31
  this.containerDiv.style.height = `${this.height}px`;
31
32
  }
33
+
34
+ this.conversationId = this.getStoredConversationId();
35
+
32
36
  this.sessionInfo = options.sessionInfo || null;
33
37
  this.init();
34
38
  }
35
39
 
40
+ isSafari() {
41
+ var ua = navigator.userAgent.toLowerCase();
42
+ return ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1;
43
+ }
44
+
45
+ getStoredConversationId() {
46
+ try {
47
+ return localStorage.getItem(`conversationId_${this.chatId}`);
48
+ } catch (error) {
49
+ console.error('Error accessing localStorage:', error);
50
+ return null;
51
+ }
52
+ }
53
+
54
+ setStoredConversationId(conversationId) {
55
+ try {
56
+ localStorage.setItem(`conversationId_${this.chatId}`, conversationId);
57
+ } catch (error) {
58
+ console.error('Error accessing localStorage:', error);
59
+ }
60
+ }
61
+
36
62
  init() {
63
+ this.updateIframes();
64
+ this.addEventListeners();
65
+ }
66
+
67
+ updateIframes() {
37
68
  const cleanedServerUrl = this.serverUrl.endsWith('/') ? this.serverUrl.slice(0, -1) : this.serverUrl;
38
69
  const baseIframeUrl = `${cleanedServerUrl}/ChatUX/${this.chatId}`;
39
70
  const minimizedPositionStyle = `position: fixed; right: 0; bottom: 0; width: ${this.width}px; height: ${this.minimizedHeight}; z-index: 100000; ${this.enableAnimation ? 'transition: height 0.3s ease, opacity 0.3s ease;' : ''}`;
40
71
 
41
72
  const iframeHtml = `
42
- <iframe id="embedded-chat" style="${this.positionStyle}; display: none; border:none; overflow:hidden;" frameborder="0" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="${this.buildIframeUrl(baseIframeUrl, { isPreview: this.isPreview, mode: this.mode, locale: this.locale })}"></iframe>
43
- <iframe id="embedded-chat-minimized" style="${minimizedPositionStyle}; display: none; border:none; overflow:hidden;" frameborder="0" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="${this.buildIframeUrl(baseIframeUrl, { isPreview: this.isPreview, isMinimized: true, locale: this.locale })}"></iframe>
73
+ <iframe id="embedded-chat" style="${this.positionStyle}; display: none; border:none; overflow:hidden;" frameborder="0" sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-top-navigation-by-user-activation" src="${this.buildIframeUrl(baseIframeUrl, { isPreview: this.isPreview, mode: this.mode, locale: this.locale, conversationId: this.conversationId })}"></iframe>
74
+ <iframe id="embedded-chat-minimized" style="${minimizedPositionStyle}; display: none; border:none; overflow:hidden;" frameborder="0" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="${this.buildIframeUrl(baseIframeUrl, { isPreview: this.isPreview, isMinimized: true, locale: this.locale, conversationId: this.conversationId })}"></iframe>
44
75
  `;
45
76
 
46
77
  this.containerDiv.innerHTML = iframeHtml;
47
78
  this.iframe = this.containerDiv.querySelector("#embedded-chat");
48
79
  this.minimizedIframe = this.containerDiv.querySelector("#embedded-chat-minimized");
80
+ }
49
81
 
82
+ addEventListeners() {
50
83
  // Add event listeners to track mouse position
51
84
  this.iframe.addEventListener("mouseenter", () => { this.mouseInsideChat = true; });
52
85
  this.iframe.addEventListener("mouseleave", () => { this.mouseInsideChat = false; });
@@ -83,6 +116,10 @@
83
116
  if (this.sessionInfo !== null) {
84
117
  this.setSessionInfo(this.sessionInfo);
85
118
  }
119
+ if (e.data.message === "show" && e.data.conversationId) {
120
+ this.conversationId = e.data.conversationId;
121
+ this.setStoredConversationId(this.conversationId);
122
+ }
86
123
  }
87
124
  } else if (typeof e.data === "string") {
88
125
  if (e.data === "minimize") {
@@ -109,9 +146,20 @@
109
146
 
110
147
  showMaximized() {
111
148
  this.minimizedIframe.style.display = "none";
149
+
112
150
  this.minimizedIframe.style.height = `${this.height}px`;
113
151
  this.minimizedIframe.style.opacity = ''; // Reset opacity to unset
152
+
114
153
  this.iframe.style.display = "block";
154
+
155
+ if (this.isSafari() && !this.hasRefreshed) {
156
+ // Force refresh by resetting the iframe's src
157
+ const currentSrc = this.iframe.src;
158
+ this.iframe.src = ''; // Temporarily clear the src
159
+ this.iframe.src = currentSrc; // Reset to the original src to trigger a reload
160
+ this.hasRefreshed = true; // Prevent endless loop
161
+ }
162
+
115
163
  this.iframe.style.height = `${this.height}px`;
116
164
  this.iframe.style.opacity = '1';
117
165
  }
@@ -157,7 +205,7 @@
157
205
  }
158
206
 
159
207
  buildIframeUrl(baseIframeUrl, params = {}) {
160
- const urlParams = new URLSearchParams(params);
208
+ const urlParams = new URLSearchParams();
161
209
 
162
210
  if (params.isPreview) {
163
211
  urlParams.set('isPreview', 'true');
@@ -171,8 +219,12 @@
171
219
  urlParams.set('locale', params.locale);
172
220
  }
173
221
 
222
+ if (params.conversationId) {
223
+ urlParams.set('conversationId', params.conversationId);
224
+ }
225
+
174
226
  for (const [key, value] of Object.entries(params)) {
175
- if (key !== 'mode' && key !== 'isPreview') {
227
+ if (key !== 'mode' && key !== 'isPreview' && key !== 'conversationId' && value !== null && value !== undefined) {
176
228
  urlParams.set(key, value);
177
229
  }
178
230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "embeddedaichatux",
3
- "version": "1.4.1",
3
+ "version": "1.5.1",
4
4
  "description": "A lightweight and customizable embedded AI chat UI component that seamlessly integrates into web applications, offering minimized and expanded views, with iframe-based content rendering.",
5
5
  "main": "EmbeddedChat.js",
6
6
  "scripts": {