doo.functions.scripts

Functions for working with external scripts. E. g. load external script from url.

Download External Script / Library

downloadScript

In Tabidoo, the downloadScript function provides a flexible way to load external JavaScript libraries dynamically. This can significantly simplify your work when you need a specific library to perform tasks such as data parsing, complex calculations, or other operations. However, using this functionality comes with certain limitations and considerations, which are essential to understand for optimal use.

Features and Behavior

The behavior of the downloadScript function can vary depending on whether it is executed on the client (browser) or on the server:

Client-Side Execution (Browser):

  • The script is downloaded and executed in the browser.
  • Typically, the library adds a property to the window object (e.g., window.MyLibrary).
  • This property is returned as the result of the downloadScript function.

Server-Side Execution (Node.js):

  • The script is handled using require().
  • The library's export object is returned as the result of the downloadScript function.

Key Considerations

Library Requirements:

  • The library must attach itself to the window object when used in the browser. If it does not, the function will not return the expected result.
  • Libraries with complex dependencies may not work correctly, as Tabidoo does not automatically resolve these dependencies.

Environment-Specific Behavior
Ensure the script supports both client-side (browser) and server-side environments if you plan to use it in both contexts. Check the library’s documentation for compatibility details.

Security and Integrity:

  • Always use libraries from trusted sources.
  • Prefer URLs served over HTTPS to prevent security risks.
  • Verify that the library does not introduce potential vulnerabilities.

Performance Impact
Loading large external libraries can affect performance, especially on the client side. Use only what is necessary and cache results wherever possible.

Error Handling:

Ensure proper error handling when downloading and executing external scripts.

try {
       const MyLibrary = await doo.functions.scripts.downloadScript('https://example.com/library.js', 'MyLibrary');
    } catch (error) {
       console.error('Failed to load the script:', error);
    }

 

Example: Parsing JSON to CSV

Here’s an example of using the downloadScript function to load the popular xlsx library for working with spreadsheets.

// download script
    // on server it just require()
    // in browser the script create a variable window.XLSX, which is returned
    const XLSX = await doo.functions.scripts.downloadScript('https://cdn.sheetjs.com/xlsx-0.19.3/package/dist/xlsx.full.min.js', 'XLSX');
    // example data
    var data = [ { "name": "John", "city": "Seattle" }, { "name": "Zach", "city": "New York" } ];
    const worksheet = XLSX.utils.json_to_sheet(data);
    const csvOutput: string = XLSX.utils.sheet_to_csv(worksheet);
    // show download file dialog
    doo.functions.browser.saveFile(csvOutput, 'myfile2.csv', 'text/csv');

 

Additional Use Cases

  1. Custom Data Transformations: Import libraries to handle data conversion or manipulation (e.g., XML, JSON, CSV).
  2. Charting and Visualization: Load client-side libraries for advanced UI components.
  3. Custom Algorithms: Incorporate specialized algorithms or utilities not natively available in JavaScript.