releasebird-javascript-sdk 1.0.13 → 1.0.15

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.
@@ -32,55 +32,67 @@ export default class RbirdSessionManager {
32
32
  init(apiKey) {
33
33
  this.apiKey = apiKey;
34
34
  this.identify = this.getState()?.identify;
35
- const http = new XMLHttpRequest();
36
- http.open("GET", `${API}/ewidget`);
37
- http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
38
- http.setRequestHeader("apiKey", apiKey);
39
- http.onerror = function () {
40
- //handle error
41
- };
42
- const self = this;
43
- http.onreadystatechange = function (e) {
44
- if (http.readyState === XMLHttpRequest.DONE) {
45
- if (http.status === 200 || http.status === 201) {
46
- try {
47
- self.widgetSettings = JSON.parse(http.responseText);
48
- Rbird.setStyles(
49
- self.widgetSettings.backgroundColor,
50
- self.widgetSettings.textColor,
51
- self.widgetSettings.launcherColor,
52
- self.widgetSettings.launcherPosition,
53
- self.widgetSettings.spaceLeftRight,
54
- self.widgetSettings.spaceBottom
55
- );
56
- RbirdWebsiteWidget.getInstance().renderWebsiteWidget();
57
- RbirdWebsiteWidget.getInstance().styleWidget();
58
- } catch (e) {
35
+
36
+ return new Promise((resolve, reject) => {
37
+ const http = new XMLHttpRequest();
38
+ http.open("GET", `${API}/ewidget`);
39
+ http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
40
+ http.setRequestHeader("apiKey", apiKey);
41
+
42
+ http.onerror = function () {
43
+ reject(new Error("Network error"));
44
+ };
45
+
46
+ const self = this;
47
+ http.onreadystatechange = async function (e) {
48
+ if (http.readyState === XMLHttpRequest.DONE) {
49
+ if (http.status === 200 || http.status === 201) {
50
+ try {
51
+ self.widgetSettings = JSON.parse(http.responseText);
52
+ Rbird.setStyles(
53
+ self.widgetSettings.backgroundColor,
54
+ self.widgetSettings.textColor,
55
+ self.widgetSettings.launcherColor,
56
+ self.widgetSettings.launcherPosition,
57
+ self.widgetSettings.spaceLeftRight,
58
+ self.widgetSettings.spaceBottom
59
+ );
60
+ await RbirdWebsiteWidget.getInstance().renderWebsiteWidget();
61
+ RbirdWebsiteWidget.getInstance().styleWidget();
62
+ resolve(self.widgetSettings); // Erfolgreich -> Promise resolved
63
+ } catch (e) {
64
+ reject(new Error("Error parsing widget settings"));
65
+ }
66
+ } else {
67
+ reject(new Error(`Request failed with status ${http.status}`));
59
68
  }
60
69
  }
61
- //reject();
62
- }
63
- };
64
- http.send();
65
- let anonymous = window.localStorage.getItem("Rbird_I");
66
- if (!anonymous) {
67
- const i = this.generateRandomString();
68
- window.localStorage.setItem('Rbird_I', i);
69
- this.anonymousIdentifier = i;
70
- } else {
71
- this.anonymousIdentifier = anonymous;
72
- }
73
- this.anonymousIdentifier = this.anonymousIdentifier.replace("\"", "");
74
- setInterval(() => {
75
- let state = this.getState();
76
- if (state?.identify && state?.identify?.properties['external_user_id']) {
77
- const http = new XMLHttpRequest();
78
- http.open("POST", `${API}/ewidget/ping`);
79
- http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
80
- http.setRequestHeader("apiKey", apiKey);
81
- http.send(JSON.stringify(state.identify));
70
+ };
71
+
72
+ http.send();
73
+
74
+ let anonymous = window.localStorage.getItem("Rbird_I");
75
+ if (!anonymous) {
76
+ const i = this.generateRandomString();
77
+ window.localStorage.setItem('Rbird_I', i);
78
+ this.anonymousIdentifier = i;
79
+ } else {
80
+ this.anonymousIdentifier = anonymous;
82
81
  }
83
- }, 30000);
82
+ this.anonymousIdentifier = this.anonymousIdentifier.replace("\"", "");
83
+
84
+ // Intervall bleibt unverändert
85
+ setInterval(() => {
86
+ let state = this.getState();
87
+ if (state?.identify && state?.identify?.properties['external_user_id']) {
88
+ const http = new XMLHttpRequest();
89
+ http.open("POST", `${API}/ewidget/ping`);
90
+ http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
91
+ http.setRequestHeader("apiKey", apiKey);
92
+ http.send(JSON.stringify(state.identify));
93
+ }
94
+ }, 30000);
95
+ });
84
96
  }
85
97
 
86
98
  getState() {
@@ -32,6 +32,8 @@ export default class RbirdWebsiteWidget {
32
32
 
33
33
  hideWidgetButton;
34
34
 
35
+ noButton;
36
+
35
37
  static getInstance() {
36
38
  if (!this.instance) {
37
39
  this.instance = new RbirdWebsiteWidget();
@@ -45,33 +47,50 @@ export default class RbirdWebsiteWidget {
45
47
  }
46
48
 
47
49
  renderWebsiteWidget() {
48
- if (this.websiteWidgetVisible) {
49
- return;
50
- }
51
- this.websiteWidgetVisible = true;
50
+ return new Promise((resolve, reject) => {
51
+ // Überprüfen, ob das Widget bereits sichtbar ist
52
+ if (this.websiteWidgetVisible) {
53
+ resolve(); // Wenn das Widget bereits sichtbar ist, wird das Promise sofort aufgelöst
54
+ return;
55
+ }
52
56
 
53
- let elem = document.createElement("div");
54
- elem.onclick = () => {
55
- this.openWebsiteWidget();
56
- };
57
- document.body.appendChild(elem);
58
- this.websiteWidget = elem;
59
-
60
- if (RbirdSessionManager.getInstance().widgetSettings?.allowClose) {
61
- this.hideWidgetButton = document.createElement("div");
62
- this.hideWidgetButton.className = "hideWidgetButton";
63
- this.hideWidgetButton.innerHTML = hideWidgetImage;
64
- this.hideWidgetButton.onclick = () => this.handleHideWidgetButton();
65
- this.hideWidgetButton.style.display = "block";
66
- document.body.appendChild(this.hideWidgetButton);
67
- }
57
+ this.websiteWidgetVisible = true;
58
+
59
+ try {
60
+ // Widget-Element erstellen und zum DOM hinzufügen
61
+ let elem = document.createElement("div");
62
+ elem.onclick = () => {
63
+ this.openWebsiteWidget();
64
+ };
65
+ document.body.appendChild(elem);
66
+ this.websiteWidget = elem;
67
+
68
+ // Überprüfen, ob das Schließen des Widgets erlaubt ist
69
+ if (RbirdSessionManager.getInstance().widgetSettings?.allowClose && !this.noButton) {
70
+ this.hideWidgetButton = document.createElement("div");
71
+ this.hideWidgetButton.className = "hideWidgetButton";
72
+ this.hideWidgetButton.innerHTML = hideWidgetImage;
73
+ this.hideWidgetButton.onclick = () => this.handleHideWidgetButton();
74
+ this.hideWidgetButton.style.display = "block";
75
+ document.body.appendChild(this.hideWidgetButton);
76
+ }
77
+
78
+ // Badge erstellen und zum DOM hinzufügen
79
+ this.countBadge = document.createElement("span");
80
+ this.countBadge.className = "rbird_badge";
81
+ this.countBadge.style.display = "none";
82
+ document.body.appendChild(this.countBadge);
68
83
 
69
- this.countBadge = document.createElement("span");
70
- this.countBadge.className = "rbird_badge";
71
- this.countBadge.style.display = "none";
72
- document.body.appendChild(this.countBadge);
84
+ // Stil des Widgets anpassen
85
+ this.styleWidget();
73
86
 
74
- this.styleWidget();
87
+ // Erfolgreich aufgelöst, nachdem das Widget hinzugefügt und gestylt wurde
88
+ resolve();
89
+ } catch (error) {
90
+ // Falls ein Fehler während der Widget-Erstellung oder des DOM-Manipulationsprozesses auftritt, wird das Promise abgelehnt
91
+ reject(new Error("Error rendering the website widget: " + error.message));
92
+ }
93
+ });
75
94
  }
76
95
 
77
96
  isOpen() {
@@ -172,7 +191,7 @@ export default class RbirdWebsiteWidget {
172
191
  if (this.countBadge) {
173
192
  this.countBadge.innerText = "0";
174
193
  this.countBadge.style.display = "none";
175
- if (!this.isOpen() && this.hideWidgetButton) {
194
+ if (!this.isOpen() && this.hideWidgetButton && !this.noButton) {
176
195
  this.hideWidgetButton.style.display = "block";
177
196
  }
178
197
  }
@@ -187,9 +206,9 @@ export default class RbirdWebsiteWidget {
187
206
  }
188
207
 
189
208
  showButton(showButton) {
190
- if (showButton) {
209
+ if (showButton && !this.noButton) {
191
210
  this.websiteWidget.style.display = 'block';
192
- if (this.hideWidgetButton) {
211
+ if (this.hideWidgetButton && !this.noButton) {
193
212
  this.hideWidgetButton.style.display = "block";
194
213
  }
195
214
  } else {
@@ -206,13 +225,15 @@ export default class RbirdWebsiteWidget {
206
225
  type: 'close',
207
226
  }, '*');
208
227
  }
209
- if (this.hideWidgetButton && this.countBadge.style.display === "none") {
228
+ if (this.hideWidgetButton && this.countBadge.style.display === "none" && !this.noButton) {
210
229
  this.hideWidgetButton.style.display = "block";
211
230
  }
212
231
  RbirdUtils.removeClass(this.widgetContent, "cta__modal--visible");
213
232
 
214
233
  this.initButton();
215
- this.websiteWidget.style.display = 'block';
234
+ if (!this.noButton) {
235
+ this.websiteWidget.style.display = 'block';
236
+ }
216
237
 
217
238
  this.websiteWidget.onclick = () => this.openWebsiteWidget();
218
239
  this.unregisterListeners();
package/src/index.js CHANGED
@@ -62,11 +62,14 @@ class Rbird {
62
62
  console.warn("Rbird already initialized.");
63
63
  return;
64
64
  }
65
- RbirdSessionManager.getInstance().init(apiKey);
66
- instance.initialized = true;
67
- if (showButton != null) {
68
- RbirdWebsiteWidget.getInstance().showButton(showButton);
69
- }
65
+ RbirdWebsiteWidget.getInstance().noButton = !showButton;
66
+ RbirdSessionManager.getInstance().init(apiKey).then(() => {
67
+ instance.initialized = true;
68
+ if (showButton != null) {
69
+ RbirdWebsiteWidget.getInstance().showButton(showButton);
70
+ }
71
+ })
72
+
70
73
  } catch (e) {
71
74
  console.error(e);
72
75
  }