()
| 1 | import { useDataReady, useGlobalConfigStore } from 'tailchat-shared'; |
| 2 | |
| 3 | export function useInjectTianjiScript() { |
| 4 | const { tianji } = useGlobalConfigStore(); |
| 5 | |
| 6 | useDataReady( |
| 7 | () => |
| 8 | typeof tianji.scriptUrl === 'string' && |
| 9 | typeof tianji.websiteId === 'string' && |
| 10 | tianji.websiteId.length > 0, |
| 11 | () => { |
| 12 | if (!tianji.scriptUrl) { |
| 13 | console.error( |
| 14 | 'Cannot inject Tianji script because of scriptUrl not cool:', |
| 15 | tianji.scriptUrl |
| 16 | ); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | const el = document.createElement('script'); |
| 21 | el.src = tianji.scriptUrl; |
| 22 | el.setAttribute('data-website-id', String(tianji.websiteId)); |
| 23 | el.setAttribute('async', 'async'); |
| 24 | el.setAttribute('defer', 'defer'); |
| 25 | document.head.append(el); |
| 26 | } |
| 27 | ); |
| 28 | } |
no test coverage detected