es-injector

Tiny tool for dynamically injecting javascript code on your page. Written on ES5, so You even don't need Babel in your project.

Usage no npm install needed!

<script type="module">
  import esInjector from 'https://cdn.skypack.dev/es-injector';
</script>

README

es-injector

Tiny tool for dynamically injecting javascript code on your page. Written on ES5, so You even don't need Babel in your project.

Creates promise which appends script tag on the page. Waits when code will be loaded and then resolves. If load time tooks to long (3 sec by default) - rejects.

Install

npm install es-injector --save

Usage

import esInjector from "es-injector";

const config = {
  src: "https://www.google.com/recaptcha/api.js", // required
  variableName: "grecaptcha", // required
  variableMethod: ["execute", "render"] // optional, if need
  // see other params in Params scheme
};

esInjector.inject(config)
  .then(
    () => console.log(grecaptcha, "Script loaded!"),
    () => console.log("Connection timed out!")
  );

Params scheme

If load time took more time than You specifyed in config.waitTime (3 seconds by default) the promise will be rejected.

/**
  * @param {object} config - (required)
  * @param {string} config.src - (required)
  * @param {string} config.variableName - (required)
  * @param {string[]} config.varibableMethod - (optional)
  * @param {boolean} config.defer - (optional)
  * @param {boolean} config.async - (optional)
  * @param {number} config.waitTime - (optional)
*/