File tree Expand file tree Collapse file tree 1 file changed +29
-6
lines changed
src/routes/chat/[agentId]/[conversationId]/rich-content Expand file tree Collapse file tree 1 file changed +29
-6
lines changed Original file line number Diff line number Diff line change 4141 }
4242
4343 function initCode () {
44- const text = message? .rich_content ? .message ? .text || message? .text || ' ' ;
45- const parsedText = marked .lexer (text);
46- // @ts-ignore
47- const codeText = parsedText .find (token => token .type === ' code' || token .type === ' html' )? .text || ' ' ;
48- const code = codeText .replace (/ <script\b [^ >] * >([\s\S ] *? )<\/ script>/ i , ' $1' );
49- eval (code);
44+ try {
45+ const text = message? .rich_content ? .message ? .text || message? .text || ' ' ;
46+ const parsedText = marked .lexer (text);
47+ // @ts-ignore
48+ const codeText = parsedText .filter (x => !! x .text ).map (x => x .text ).join (' ' );
49+ loadScript (codeText);
50+ } catch (error) {
51+ console .error (' Error parsing js code:' , error);
52+ }
53+ }
54+
55+ /** @param {string} codeText */
56+ function loadScript (codeText ) {
57+ const code = codeText .replace (/ <script\b [^ >] * >([\s\S ] *? )<\/ script>/ gi , ' $1' );
58+ const matchedSrcs = codeText .match (/ <script\s + [^ >] * src\s * =\s * ["'] ([^ "'] + )["'][^ >] * >/ i );
59+ if (matchedSrcs && matchedSrcs[1 ]) {
60+ const curScripts = document .head .getElementsByTagName (" script" );
61+ const found = Array .from (curScripts).find (x => x .src === matchedSrcs[1 ]);
62+ if (found) {
63+ found .remove ();
64+ }
65+
66+ const script = document .createElement (' script' );
67+ script .src = matchedSrcs[1 ];
68+ script .onload = () => eval (code);
69+ document .head .appendChild (script);
70+ } else {
71+ eval (code);
72+ }
5073 }
5174< / script>
5275
You can’t perform that action at this time.
0 commit comments