Last active
December 18, 2018 07:45
-
-
Save r17x/dfcdf19c81c23e10c29a08c1393d0b0d to your computer and use it in GitHub Desktop.
InjectScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple Injection HTML Script resource | |
* @author [email protected] | |
* Example use: | |
* rel its experimental | maybe not work on some browser | |
* const scriptList = [ | |
* { | |
* src: 'url', | |
* async: boolean, | |
* defer: boolean, | |
* rel: 'preload|prefetch' | |
* }]; | |
* | |
* InjectScript.addScript(scriptList) | |
* InjectScript.removeScript(scriptList) | |
*/ | |
export class InjectScript { | |
/** | |
* @param {array} scriptList | |
*/ | |
static addScript(scriptList) { | |
scriptList.forEach((script, key) => { | |
script.id = `script:${key}`; | |
InjectScript.addToBody({ ...script }); | |
}); | |
} | |
/** | |
* @param {array} scriptList | |
*/ | |
static removeScript(scriptList) { | |
scriptList.forEach((script, key) => { | |
script = document.getElementById(`script:${key}`); | |
script.parentNode.removeChild(script); | |
}); | |
} | |
/** | |
* @param {object} | |
*/ | |
static addToBody({ id, src, async, defer, rel }) { | |
const element = document.createElement("script"); | |
element.src = src; | |
element.async = async; | |
element.defer = defer; | |
element.id = id; | |
document.body.appendChild(element); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can use https://github.com/ri7nz/edotensei