New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ember-inject-scripts

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-inject-scripts

Seamlessly inject any sort of scripts inside your ember application

0.0.7
latest
Source
npm
Version published
Weekly downloads
1.1K
-10.18%
Maintainers
1
Weekly downloads
 
Created
Source

ember-inject-scripts

Seamlessly inject any sort of scripts inside your ember application

Build Status npm npm version dependencies Status devDependencies Status EmberObserver

Features

  • Inject single script
  • Inject multiple scripts
  • Get onload and onerror callbacks for individual scripts
  • Inject scripts with attributes
  • Inject inline scripts
  • Inject script only once
  • Get all scripts cached events on done.

Demo

https://prakashchokalingam.github.io/ember-inject-scripts!

Installation

npm install --save ember-inject-scripts

Usage

Single script injection

  import 'injectScript' from 'inject-scripts';

  injectScript("https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js").then(() => {
    console.log ( "🤟Yay ! script injected" );
  });

                -------- or  --------

  let singleScript = {
    id: 'datefns',
    src: 'https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js',
    attributes: [
      {
        name: 'data-library',
        value: 'datefns'
      }
    ]
  };

  injectScript(singleScript).then(() => {
    console.log ( "🤟Yay ! script injected" );
  });

  ---->
  <script type="text/javascript" id="datefns" data-test-script="datefns" data-library="datefns"
  src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"></script>

Multiple scripts injection

  import 'injectScripts' from 'inject-scripts';

  let multipleScripts = [
    {
      id: 'datefns',
      src: 'https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js',
      attributes: [
        {
          name: 'data-library',
          value: 'datefns'
        }
      ],
      once: true
    },
    {
      id: 'inline',
      inline: `console.log('inline script rendered')`,
      attributes: [
        {
          name: 'data-inline',
          value: 'true'
        }
      ]
    }
  ]

  injectScripts(multipleScripts).then((events) => {
    // events - holds data for all scripts injection as array
    console.log ( "🤟🤟🤟Yay ! multiple scripts injected" );
  });

  ---->
  <script type="text/javascript" id="datefns" data-test-script="datefns" data-library="datefns"
  src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js"></script>

  <script type="text/javascript" id="inline" data-test-script="inline" data-inline="true">
    console.log('inline script rendered');
  </script>

injectScripts function expects object for single script inject and array of objects for multiple scripts injection as parameter, the object items are,

nametypedescription
idstring : optionalAdds an id attribute to the script element id=id and also creates a test selector data-test-script=id
typestring : optional : default text/javascriptAdds type to the script element
srcstring : optionalAdds a source to the script element src=src
inlinestring : optionalAdds inline contents to the script element <script> inline </script>
onceboolean : optionalLooks for an element with the same id passed. If founded avoids the injection and returns the dom element as target
attributesArray of objects : optional { name: 'data-inline', value: 'true' }Adds attribute contents to the script element <script data-inline="true"> inline </script>
onloadfunction : optionalCallback function for successive load of script. not available for inline scripts
onerrorfunction : optionalCallback function for failure while loading of script. not available for inline scripts

injectScripts.then() will return all the cacheable event values as array for all passed script elements.

inline scripts will have only target as the event value.

Running

Running Tests

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.

Keywords

ember-addon

FAQs

Package last updated on 09 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts