Using and requesting new external libraries

We have found a way to enable loading of external libraries. :grinning:

You can use this code to load further external libraries.

async function loadScripts(...urls) {
  for (const thisUrl of urls) {
    console.log(`Loading script "${thisUrl}"...`);

    const response = await fetch(thisUrl);
    const responseBody = await response.text();

    const scriptElm = document.createElement("script");
    const inlineCode = document.createTextNode(responseBody);
    scriptElm.appendChild(inlineCode);
    document.body.appendChild(scriptElm);

    console.log(`Script "${thisUrl}" successfully loaded`);
  }
}

Then load the scripts with:

loadScripts('https://cdn.plot.ly/plotly-2.9.0.min.js');

This example is for chart.js.

@parth.parmar @mellerbeck - per you question from the kick off yesterday

1 Like

@EddyA – can you comment including an external CSS,

e.g.

Some libraries have both a JS and CSS component to the library and ability to load the CSS would also be a great advantage.

You can run this through a minifier, too, and make something a little more compact to put on the top of all your widgets.

async function loadScripts(...e){for(const o of e){console.log(`Loading script "${o}"...`);const e=await fetch(o),t=await e.text(),c=document.createElement("script"),n=document.createTextNode(t);c.appendChild(n),document.body.appendChild(c),console.log(`Script "${o}" successfully loaded`)}}

@parth.parmar Did you see this thread?
I think you should be able to switch out the url in the first line for the toastify url.
Let us know how you get on.

1 Like