|
1 | 1 | ;(function () { |
| 2 | + window.sqlbot_assistant_handler = window.sqlbot_assistant_handler || {} |
2 | 3 | const defaultData = { |
3 | 4 | id: '1', |
4 | 5 | show_guide: false, |
|
67 | 68 | const getChatContainerHtml = (data) => { |
68 | 69 | return ` |
69 | 70 | <div id="sqlbot-assistant-chat-container"> |
70 | | - <iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}"></iframe> |
| 71 | + <iframe id="sqlbot-assistant-chat-iframe-${data.id}" allow="microphone" src="${data.domain_url}/#/assistant?id=${data.id}&online=${!!data.online}"></iframe> |
71 | 72 | <div class="sqlbot-assistant-operate"> |
72 | 73 | <div class="sqlbot-assistant-closeviewport sqlbot-assistant-viewportnone"> |
73 | 74 | <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none"> |
|
440 | 441 | } |
441 | 442 | function loadScript(src, id) { |
442 | 443 | const domain_url = getDomain(src) |
| 444 | + const online = getParam(src, 'online') |
443 | 445 | let url = `${domain_url}/api/v1/system/assistant/info/${id}` |
444 | 446 | if (domain_url.includes('5173')) { |
445 | 447 | url = url.replace('5173', '8000') |
|
455 | 457 | Object.assign(tempData, config) |
456 | 458 | tempData = Object.assign(tempData, config) |
457 | 459 | } |
| 460 | + tempData['online'] = online && online.toString().toLowerCase() == 'true' |
458 | 461 | initsqlbot_assistant(tempData) |
459 | 462 | if (data.type == 1) { |
460 | 463 | registerMessageEvent(id, tempData) |
|
471 | 474 | const src_list = scriptsArray.map((script) => script.src) |
472 | 475 | src_list.forEach((src) => { |
473 | 476 | const id = getParam(src, 'id') |
| 477 | + window.sqlbot_assistant_handler[id] = window.sqlbot_assistant_handler[id] || {} |
| 478 | + window.sqlbot_assistant_handler[id]['id'] = id |
474 | 479 | const propName = script_id_prefix + id + '-state' |
475 | 480 | if (window[propName]) { |
476 | 481 | return true |
477 | 482 | } |
478 | 483 | window[propName] = true |
479 | 484 | loadScript(src, id) |
| 485 | + expposeGlobalMethods(id) |
480 | 486 | }) |
481 | 487 | } |
482 | | - window.addEventListener('load', init) |
| 488 | + function updateOnlineParam(target_url, newValue) { |
| 489 | + try { |
| 490 | + const url = new URL(target_url) |
| 491 | + const [hashPath, hashQuery] = url.hash.split('?') |
| 492 | + let searchParams |
| 493 | + if (hashQuery) { |
| 494 | + searchParams = new URLSearchParams(hashQuery) |
| 495 | + } else { |
| 496 | + searchParams = url.searchParams |
| 497 | + } |
| 498 | + searchParams.set('online', newValue) |
| 499 | + if (hashQuery) { |
| 500 | + url.hash = `${hashPath}?${searchParams.toString()}` |
| 501 | + } else { |
| 502 | + url.search = searchParams.toString() |
| 503 | + } |
| 504 | + return url.toString() |
| 505 | + } catch (e) { |
| 506 | + console.error('Invalid URL:', target_url) |
| 507 | + return target_url |
| 508 | + } |
| 509 | + } |
| 510 | + function expposeGlobalMethods(id) { |
| 511 | + window.sqlbot_assistant_handler[id]['setOnline'] = (online) => { |
| 512 | + if (online != null && typeof online != 'boolean') { |
| 513 | + throw new Error('The parameter can only be of type boolean') |
| 514 | + } |
| 515 | + const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`) |
| 516 | + if (iframe) { |
| 517 | + const url = iframe.src |
| 518 | + const eventName = 'sqlbot_assistant_event' |
| 519 | + const params = { |
| 520 | + busi: 'setOnline', |
| 521 | + online, |
| 522 | + eventName, |
| 523 | + messageId: id, |
| 524 | + } |
| 525 | + const contentWindow = iframe.contentWindow |
| 526 | + contentWindow.postMessage(params, url) |
| 527 | + } |
| 528 | + } |
| 529 | + window.sqlbot_assistant_handler[id]['refresh'] = (online) => { |
| 530 | + if (online != null && typeof online != 'boolean') { |
| 531 | + throw new Error('The parameter can only be of type boolean') |
| 532 | + } |
| 533 | + const iframe = document.getElementById(`sqlbot-assistant-chat-iframe-${id}`) |
| 534 | + if (iframe) { |
| 535 | + const url = iframe.src |
| 536 | + let new_url = `${url}&t=${Date.now()}` |
| 537 | + if (online != null) { |
| 538 | + new_url = updateOnlineParam(new_url, online) |
| 539 | + } |
| 540 | + iframe.src = new_url |
| 541 | + } |
| 542 | + } |
| 543 | + } |
| 544 | + // window.addEventListener('load', init) |
| 545 | + const executeWhenReady = (fn) => { |
| 546 | + if ( |
| 547 | + document.readyState === 'complete' || |
| 548 | + (document.readyState !== 'loading' && !document.documentElement.doScroll) |
| 549 | + ) { |
| 550 | + setTimeout(fn, 0) |
| 551 | + } else { |
| 552 | + const onReady = () => { |
| 553 | + document.removeEventListener('DOMContentLoaded', onReady) |
| 554 | + window.removeEventListener('load', onReady) |
| 555 | + fn() |
| 556 | + } |
| 557 | + document.addEventListener('DOMContentLoaded', onReady) |
| 558 | + window.addEventListener('load', onReady) |
| 559 | + } |
| 560 | + } |
| 561 | + |
| 562 | + executeWhenReady(init) |
483 | 563 | })() |
0 commit comments