diff --git a/src/index.jsx b/src/index.jsx index 7293d21..32f8e4f 100755 --- a/src/index.jsx +++ b/src/index.jsx @@ -5,6 +5,23 @@ function removeEmpty(obj) { return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null)); } +function createOutbrainScript(scriptSrc) { + if (!window.OBR) { + const script = document.createElement('script'); + script.src = scriptSrc; + + document.head.appendChild(script); + + return script; + } +} + +function removeOutbrainScript(script) { + if (script) { + script.remove(); + } +} + const OutbrainWidget = (props) => { const { dataSrc = '', @@ -21,7 +38,9 @@ const OutbrainWidget = (props) => { obLanguage = null, obPsub = null, obAppId = null, - externalId = null + externalId = null, + obInjectScript = true, + scriptSrc = 'https://widgets.outbrain.com/outbrain.js' } = props; const attrs = removeEmpty({ @@ -42,7 +61,13 @@ const OutbrainWidget = (props) => { 'data-external-id': externalId }); - console.log('zagao', attrs); + useEffect(() => { + if (!obInjectScript) { return; } + + const script = createOutbrainScript(scriptSrc); + + return () => { removeOutbrainScript(script); } + }, [scriptSrc, obInjectScript]); useEffect(() => { const { OBR } = window;