|
40 | 40 | const getChatContainerHtml = (data) => { |
41 | 41 | return ` |
42 | 42 | <div id="sqlbot-assistant-chat-container"> |
43 | | - <iframe id="sqlbot-assistant-chat" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}"></iframe> |
| 43 | + <iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}"></iframe> |
44 | 44 | <div class="sqlbot-assistant-operate"> |
45 | 45 | <div class="sqlbot-assistant-closeviewport sqlbot-assistant-viewportnone"> |
46 | 46 | <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none"> |
|
311 | 311 | #sqlbot-assistant #sqlbot-assistant-chat-container .sqlbot-assistant-viewportnone{ |
312 | 312 | display:none; |
313 | 313 | } |
314 | | - #sqlbot-assistant #sqlbot-assistant-chat-container #sqlbot-assistant-chat{ |
| 314 | + #sqlbot-assistant #sqlbot-assistant-chat-container #sqlbot-assistant-chat-iframe-${data.id}{ |
315 | 315 | height:100%; |
316 | 316 | width:100%; |
317 | 317 | border: none; |
|
334 | 334 | const url = new URL(src) |
335 | 335 | return url.searchParams.get(key) |
336 | 336 | } |
| 337 | + function parsrCertificate(config) { |
| 338 | + const certificateList = config.certificate |
| 339 | + if (!certificateList?.length) { |
| 340 | + return null |
| 341 | + } |
| 342 | + const list = certificateList.map((item) => formatCertificate(item)).filter((item) => !!item) |
| 343 | + return JSON.stringify(list) |
| 344 | + } |
| 345 | + function isEmpty(obj) { |
| 346 | + return obj == null || typeof obj == 'undefined' |
| 347 | + } |
| 348 | + function formatCertificate(item) { |
| 349 | + const { type, source, target, target_key, target_val } = item |
| 350 | + let source_val = null |
| 351 | + if (type.toLocaleLowerCase() == 'localstorage') { |
| 352 | + source_val = localStorage.getItem(source) |
| 353 | + } |
| 354 | + if (type.toLocaleLowerCase() == 'sessionstorage') { |
| 355 | + source_val = sessionStorage.getItem(source) |
| 356 | + } |
| 357 | + if (type.toLocaleLowerCase() == 'cookie') { |
| 358 | + source_val = getCookie(source) |
| 359 | + } |
| 360 | + if (type.toLocaleLowerCase() == 'custom') { |
| 361 | + source_val = source |
| 362 | + } |
| 363 | + if (isEmpty(source_val)) { |
| 364 | + return null |
| 365 | + } |
| 366 | + return { |
| 367 | + target, |
| 368 | + key: target_key || source, |
| 369 | + value: (target_val && eval(target_val)) || source_val, |
| 370 | + } |
| 371 | + } |
| 372 | + function getCookie(key) { |
| 373 | + if (!key || !document.cookie) { |
| 374 | + return null |
| 375 | + } |
| 376 | + const cookies = document.cookie.split(';') |
| 377 | + for (let i = 0; i < cookies.length; i++) { |
| 378 | + const cookie = cookies[i].trim() |
337 | 379 |
|
| 380 | + if (cookie.startsWith(key + '=')) { |
| 381 | + return decodeURIComponent(cookie.substring(key.length + 1)) |
| 382 | + } |
| 383 | + } |
| 384 | + return null |
| 385 | + } |
| 386 | + function registerMessageEvent(id, data) { |
| 387 | + const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`) |
| 388 | + const url = iframe.src |
| 389 | + const eventName = 'sqlbot_assistant_event' |
| 390 | + window.addEventListener('message', (event) => { |
| 391 | + if (event.data?.eventName === eventName) { |
| 392 | + if (event.data?.messageId !== id) { |
| 393 | + return |
| 394 | + } |
| 395 | + if (event.data?.busi == 'ready' && event.data?.ready) { |
| 396 | + const certificate = parsrCertificate(data) |
| 397 | + console.log(certificate) |
| 398 | + params = { |
| 399 | + busi: 'certificate', |
| 400 | + certificate, |
| 401 | + eventName, |
| 402 | + messageId: id, |
| 403 | + } |
| 404 | + const contentWindow = iframe.contentWindow |
| 405 | + contentWindow.postMessage(params, url) |
| 406 | + } |
| 407 | + } |
| 408 | + }) |
| 409 | + } |
338 | 410 | function loadScript(src, id) { |
339 | 411 | const domain_url = getDomain(src) |
340 | 412 | let url = `${domain_url}/api/v1/system/assistant/info/${id}` |
|
353 | 425 | tempData = Object.assign(tempData, config) |
354 | 426 | } |
355 | 427 | initsqlbot_assistant(tempData) |
| 428 | + if (data.type == 1) { |
| 429 | + registerMessageEvent(id, tempData) |
| 430 | + // postMessage the certificate to iframe |
| 431 | + } |
356 | 432 | }) |
357 | 433 | } |
358 | 434 | function getDomain(src) { |
|
0 commit comments