Files

81 lines
2.3 KiB
Vue
Raw Permalink Normal View History

2024-07-05 09:48:34 +07:00
<script setup lang="ts">
const props = defineProps<{
SCRIPT_ID?: any,
SCRIPT_SRC?: any,
CONTAINER_ID?: any,
options?: any,
2024-07-05 14:29:49 +07:00
inside?: boolean,
2024-07-05 09:48:34 +07:00
widgetKey?: any
}>()
const widgets : any = {};
const instance = getCurrentInstance();
const canUseDOM = () => {
return typeof window !== 'undefined' && window.document && window.document.createElement;
};
const getScriptElement = () => {
return document.getElementById(props.SCRIPT_ID);
}
const updateOnloadListener = (onload : any) => {
const script : any = getScriptElement();
const oldOnload = script.onload;
return script.onload = () => {
oldOnload();
onload();
};
}
const scriptExists = () => {
return getScriptElement() !== null;
}
const appendScript = (onload : any) => {
if (!canUseDOM()) {
onload();
return;
}
if (scriptExists()) {
if (typeof window[props.widgetKey] === 'undefined') {
updateOnloadListener(onload);
return;
}
onload();
return;
}
const script = document.createElement('script');
script.id = props.SCRIPT_ID;
script.type = 'text/javascript';
2024-07-05 14:29:49 +07:00
script.async = true;
2024-07-05 09:48:34 +07:00
script.src = props.SCRIPT_SRC;
script.onload = onload;
2024-07-05 14:29:49 +07:00
if (props.inside) document.getElementById(props.CONTAINER_ID) && document.getElementById(props.CONTAINER_ID).appendChild(script);
else {
document.getElementsByTagName('body')[0].appendChild(script);
}
2024-07-05 09:48:34 +07:00
}
const initWidget = (key: any) => {
if (typeof widgets[key].key === 'undefined') {
return;
}
widgets[key].script()
}
onMounted(async () => {
await Object.assign(widgets, {
FireAnt: {
2024-07-05 13:05:21 +07:00
script: () => new window.FireAnt.MarketsWidget({ container_id: props.CONTAINER_ID, ...props.options }),
2024-07-05 09:48:34 +07:00
key: window.FireAnt
2024-07-05 13:05:21 +07:00
},
TradingView: {
script: () => new window.TradingView.widget({ container_id: props.CONTAINER_ID, ...props.options }),
key: window.TradingView
}
})
2024-07-05 09:48:34 +07:00
appendScript(initWidget(props.widgetKey));
})
</script>
<template>
2024-07-05 14:29:49 +07:00
<div :key="props.CONTAINER_ID" :id="props.CONTAINER_ID"></div>
2024-07-05 09:48:34 +07:00
</template>