99</template >
1010
1111<script setup lang="ts">
12- import { computed , ref , onMounted , onBeforeUnmount } from ' vue'
13-
12+ import { computed , ref , onMounted , onBeforeUnmount , inject } from ' vue'
13+ const chatUserProfile = inject ( ' chatUserProfile ' ) as any
1414const htmlRef = ref <HTMLIFrameElement >()
1515const props = withDefaults (
1616 defineProps <{
@@ -37,6 +37,40 @@ function createIframeHtml(sourceHtml: string) {
3737* { margin: 0; padding: 0; box-sizing: border-box; }
3838html, body { margin: 0 !important; padding: 0 !important; overflow: hidden; }
3939</style>
40+ <script>
41+ const _INSTANCE_ID = '${instanceId }';
42+ window.chatUserProfile=function chatUserProfile() {
43+ return new Promise((resolve, reject) => {
44+ const requestId = Date.now() + '_' + Math.random()
45+
46+ function handler(e) {
47+ const data = e.data
48+
49+ if (
50+ data?.type === 'chatUserProfile:response' &&
51+ data.requestId === requestId
52+ ) {
53+ window.removeEventListener('message', handler)
54+ resolve(data.data)
55+ }
56+ }
57+ window.addEventListener('message', handler)
58+ parent.postMessage(
59+ {
60+ type: 'chatUserProfile',
61+ requestId,
62+ instanceId: _INSTANCE_ID
63+ },
64+ '*'
65+ )
66+
67+ setTimeout(() => {
68+ window.removeEventListener('message', handler)
69+ reject(new Error('timeout'))
70+ }, 10000)
71+ })
72+ }
73+ <\/ script>
4074</head>
4175<body>
4276${sourceHtml }
@@ -46,7 +80,6 @@ const INSTANCE_ID = '${instanceId}';
4680function sendMessage(message) {
4781 parent.postMessage({ type: 'chatMessage', instanceId: INSTANCE_ID, message }, '*');
4882}
49-
5083let lastSentHeight = 0;
5184let timer = null;
5285
@@ -88,6 +121,20 @@ function onMessage(e: MessageEvent) {
88121 if (e .data .type === ' chatMessage' ) {
89122 props .sendMessage ?.(e .data .message , ' new' )
90123 }
124+ if (e .data ?.type === ' chatUserProfile' ) {
125+ const iframe = htmlRef .value
126+ if (! iframe ) return
127+ chatUserProfile ().then ((ok : any ) => {
128+ iframe .contentWindow ?.postMessage (
129+ {
130+ type: ' chatUserProfile:response' ,
131+ requestId: e .data .requestId ,
132+ data: ok ,
133+ },
134+ ' *' ,
135+ )
136+ })
137+ }
91138}
92139
93140onMounted (() => {
0 commit comments