npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

attachflow

v0.0.6

Published

![hero](https://raw.githubusercontent.com/andresclua/attachflow/37b5c456d41a996655012778f8f9d4e86b43c372/public/cover.png)

Downloads

4

Readme

Attach Flow - Dynamic Resource Loader

hero

This documentation provides details on how to dynamically load JavaScript files and CSS stylesheets into a webpage using the loadScript and loadStyle utility functions. These functions support appending to the head, body, or any specified DOM element, and they allow for the addition of custom attributes to the <script> and <link> elements.

Setup via command line

npm i attachflow
import  {loadScript} from 'attachflow';

loadScript({ 
  url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js',
  appendTo: 'head'
}).then(() => console.log('Script loaded successfully.'))
.catch(error => console.error(error));

Setup for via CDN

Load the file from CDN

<script type="text/javascript" src="https://unpkg.com/attachflow/dist/attachflow.umd.js">
attachflow.loadScript({ 
  url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js',
  appendTo: 'head'
}).then(() => console.log('Script loaded successfully.'))
.catch(error => console.error(error));

> example with CDN

Functions

loadScript

Dynamically loads a JavaScript file into the page.

Parameters:

  • url (string): The URL of the script to load.
  • inlineScript (string): Optional. The JavaScript code to execute inline. If provided, the script will be executed in the context of the page. If url is also provided, inlineScript will execute after the external script is loaded.
  • attributes (Array): Optional. An array of strings representing additional attributes to set on the script element. Each attribute can be in the form of "key=value" or just "key" for boolean attributes.
  • appendTo (string | Element): Optional. Specifies the element to which the script element will be appended. Can be a selector string ('head', 'body'), or a DOM element reference. Defaults to 'head'.

Returns:

  • A Promise that resolves when the script is successfully loaded and executed or rejects with an error if the script fails to load or execute.

Example:

loadScript({ 
    url: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.j', 
    attributes: ['async', 'data-name="exampleScript"'], 
    appendTo: document.getElementById('customDiv') 
}).then(() => console.log('Script loaded successfully.'))
  .catch(error => console.error(error));

loadScript({ 
    inlineScript: `console.log('This is inline script execution.');`, 
    appendTo: document.getElementById('customDiv') 
}).then(() => console.log('Inline script executed successfully.'))
.catch(error => console.error(error));

loadStyle

Dynamically loads a CSS stylesheet into the page.

Parameters:

  • url (string): The URL of the script to load.
  • inlineStyle (string): Optional. The CSS styles to apply inline. If provided, the styles will be applied directly to the page within a <style> tag.
  • attributes (Array): Optional. An array of strings representing additional attributes to set on the script element. Each attribute can be in the form of "key=value" or just "key" for boolean attributes.
  • appendTo (string | Element): Optional. Specifies the element to which the script element will be appended. Can be a selector string ('head', 'body'), or a DOM element reference. Defaults to 'head'.

Returns:

  • A Promise that resolves when the stylesheet is successfully loaded and applied or rejects with an error if the stylesheet fails to load or apply.

Example:

loadStyle({ 
  url: 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css',
  attributes: ['integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrbQGdQYeo+IcNZDhBCZlgn/9ehbqeAyhUcBWmTx", crossorigin="anonymous"'],
  appendTo: document.querySelector('.test') // will append inside div with class test
}).then(() => console.log('Stylesheet loaded successfully.'))
.catch(error => console.error(error));

loadStyle({ 
    inlineStyle: `body { background-color: coral; }`,
    appendTo: document.querySelector('.test') // will append inside div with class test
}).then(() => console.log('Inline styles applied successfully.'))
.catch(error => console.error(error));

Usage Notes

Ensure the provided url is correct and accessible to avoid loading errors. When using the attributes parameter, make sure the attribute values are properly formatted. The appendTo parameter can accept a string identifier ('head', 'body') or a direct DOM element reference, offering flexibility in where your scripts and styles are injected. Providing an invalid appendTo value will result in an error, so ensure it correctly references an existing DOM element or uses the allowed string identifiers.

License

This module is open-source and free to use. Attribution is appreciated but not required.