DmitrMakeev commited on
Commit
6632feb
1 Parent(s): dbe43e4

Update settings.html

Browse files
Files changed (1) hide show
  1. settings.html +96 -73
settings.html CHANGED
@@ -52,89 +52,112 @@
52
  </div>
53
  <script type="text/javascript" src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>
54
  <script>
55
- var intervalId = null
56
- const qrCodeElement = document.getElementById("img-qr-code")
57
- const qrTempalte = qrCodeElement.getAttribute('src');
58
-
59
- document.getElementById("getQrBtn").addEventListener("click", function () {
60
- try {
61
- isAuthed();
62
- clearInterval(intervalId)
63
- updateQRCode()
64
- intervalId = setInterval(updateQRCode, 5000);
65
-
66
- } catch (reason) {
67
- console.error(reason);
68
- document.getElementById("resultText").innerHTML = reason + ". See logs for details";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  }
70
- });
71
 
72
- document.getElementById("logoutQrBtn").addEventListener("click", function () {
73
- try {
74
- const restAPI = whatsAppClient.restAPI(({
75
- idInstance: document.getElementById("idInstance").value,
76
- apiTokenInstance: document.getElementById("apiTokenIsntance").value
77
- }))
78
-
79
- restAPI.instance.logout()
80
- .then((data) => {
81
- document.getElementById("resultText").innerHTML = "isLogout=" + data.isLogout
82
- isAuthed();
 
 
 
 
 
 
 
 
 
83
  }).catch((reason) => {
84
  console.error(reason);
85
  document.getElementById("resultText").innerHTML = reason + ". See logs for details";
86
  });
87
- } catch (reason) {
88
- console.error(reason);
89
- document.getElementById("resultText").innerHTML = reason + ". See logs for details";
90
- }
91
- });
92
-
93
- function updateQRCode() {
94
- const restAPI = whatsAppClient.restAPI(({
95
- idInstance: document.getElementById("idInstance").value,
96
- apiTokenInstance: document.getElementById("apiTokenIsntance").value
97
- }))
98
- restAPI.instance.qr()
99
- .then((data) => {
100
- console.log(data);
101
- document.getElementById("resultText").innerHTML = "";
102
- if (data.type === 'qrCode') {
103
- qrCodeElement.hidden = false;
104
- qrCodeElement.setAttribute('src', qrTempalte.replace("%QR_DATA%", data.message))
105
- } else {
106
- qrCodeElement.hidden = true;
107
- clearInterval(intervalId)
108
- isAuthed();
109
- document.getElementById("resultText").innerHTML = data.message
110
- }
111
- }).catch((reason) => {
112
  console.error(reason);
113
  document.getElementById("resultText").innerHTML = reason + ". See logs for details";
114
- });
115
- }
116
-
117
- function isAuthed() {
118
- const restAPI = whatsAppClient.restAPI(({
119
- idInstance: document.getElementById("idInstance").value,
120
- apiTokenInstance: document.getElementById("apiTokenIsntance").value
121
- }))
122
- restAPI.instance.getStateInstance().then(data => {
123
- document.getElementById("statusText").innerHTML = data.stateInstance;
124
-
125
- if (data.stateInstance === 'authorized') {
126
- document.getElementById("getQrBtn").style.display = "none"
127
- document.getElementById("logoutQrBtn").style.display = "block"
128
- } else {
129
- document.getElementById("getQrBtn").style.display = "block"
130
- document.getElementById("logoutQrBtn").style.display = "none"
131
  }
132
-
133
-
134
- })
135
- }
136
  </script>
137
 
138
  </body>
139
 
140
- </html>
 
52
  </div>
53
  <script type="text/javascript" src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>
54
  <script>
55
+ document.addEventListener("DOMContentLoaded", function () {
56
+ var intervalId = null;
57
+ const qrCodeElement = document.getElementById("img-qr-code");
58
+ const qrTempalte = qrCodeElement.getAttribute('src');
59
+
60
+ document.getElementById("getQrBtn").addEventListener("click", function () {
61
+ try {
62
+ if (typeof whatsAppClient === 'undefined') {
63
+ throw new Error("whatsAppClient is not defined. Make sure the script is loaded correctly.");
64
+ }
65
+ isAuthed();
66
+ clearInterval(intervalId);
67
+ updateQRCode();
68
+ intervalId = setInterval(updateQRCode, 5000);
69
+
70
+ } catch (reason) {
71
+ console.error(reason);
72
+ document.getElementById("resultText").innerHTML = reason + ". See logs for details";
73
+ }
74
+ });
75
+
76
+ document.getElementById("logoutQrBtn").addEventListener("click", function () {
77
+ try {
78
+ const restAPI = whatsAppClient.restAPI(({
79
+ idInstance: document.getElementById("idInstance").value,
80
+ apiTokenInstance: document.getElementById("apiTokenIsntance").value
81
+ }));
82
+
83
+ restAPI.instance.logout()
84
+ .then((data) => {
85
+ document.getElementById("resultText").innerHTML = "isLogout=" + data.isLogout;
86
+ isAuthed();
87
+ }).catch((reason) => {
88
+ console.error(reason);
89
+ document.getElementById("resultText").innerHTML = reason + ". See logs for details";
90
+ });
91
+ } catch (reason) {
92
+ console.error(reason);
93
+ document.getElementById("resultText").innerHTML = reason + ". See logs for details";
94
+ }
95
+ });
96
+
97
+ function updateQRCode() {
98
+ try {
99
+ if (typeof whatsAppClient === 'undefined') {
100
+ throw new Error("whatsAppClient is not defined. Make sure the script is loaded correctly.");
101
+ }
102
+ const restAPI = whatsAppClient.restAPI(({
103
+ idInstance: document.getElementById("idInstance").value,
104
+ apiTokenInstance: document.getElementById("apiTokenIsntance").value
105
+ }));
106
+ restAPI.instance.qr()
107
+ .then((data) => {
108
+ console.log(data);
109
+ document.getElementById("resultText").innerHTML = "";
110
+ if (data.type === 'qrCode') {
111
+ qrCodeElement.hidden = false;
112
+ qrCodeElement.setAttribute('src', qrTempalte.replace("%QR_DATA%", data.message));
113
+ } else {
114
+ qrCodeElement.hidden = true;
115
+ clearInterval(intervalId);
116
+ isAuthed();
117
+ document.getElementById("resultText").innerHTML = data.message;
118
+ }
119
+ }).catch((reason) => {
120
+ console.error(reason);
121
+ document.getElementById("resultText").innerHTML = reason + ". See logs for details";
122
+ });
123
+ } catch (reason) {
124
+ console.error(reason);
125
+ document.getElementById("resultText").innerHTML = reason + ". See logs for details";
126
+ }
127
  }
 
128
 
129
+ function isAuthed() {
130
+ try {
131
+ if (typeof whatsAppClient === 'undefined') {
132
+ throw new Error("whatsAppClient is not defined. Make sure the script is loaded correctly.");
133
+ }
134
+ const restAPI = whatsAppClient.restAPI(({
135
+ idInstance: document.getElementById("idInstance").value,
136
+ apiTokenInstance: document.getElementById("apiTokenIsntance").value
137
+ }));
138
+ restAPI.instance.getStateInstance().then(data => {
139
+ document.getElementById("statusText").innerHTML = data.stateInstance;
140
+
141
+ if (data.stateInstance === 'authorized') {
142
+ document.getElementById("getQrBtn").style.display = "none";
143
+ document.getElementById("logoutQrBtn").style.display = "block";
144
+ } else {
145
+ document.getElementById("getQrBtn").style.display = "block";
146
+ document.getElementById("logoutQrBtn").style.display = "none";
147
+ }
148
+
149
  }).catch((reason) => {
150
  console.error(reason);
151
  document.getElementById("resultText").innerHTML = reason + ". See logs for details";
152
  });
153
+ } catch (reason) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  console.error(reason);
155
  document.getElementById("resultText").innerHTML = reason + ". See logs for details";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  }
157
+ }
158
+ });
 
 
159
  </script>
160
 
161
  </body>
162
 
163
+ </html>