Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Moris Kramer committed Jul 16, 2023
1 parent 562f88e commit 8e11c17
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/injectContentsquareScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,26 @@ type ScriptOptions = {

/**
* Appends the contentsquare script to document.head
* @param scriptOptions.clientId - the client id in the form of f6f72d509axzd - mandatory
* @param scriptOptions.defer - the script is fetched in parallel and evaluated after the document is parsed - defaults to false
* @param scriptOptions.async - the script is fetched in parallel and evaluated asap - defaults to true
* @param scriptOptions.integrity - the integrity hash of the contentsquare script - defaults to empty
* @returns the contentsquare script that was appended to document.head
* @param {ScriptOptions} scriptOptions - Options for injecting the script
* @param {string} scriptOptions.clientId - Client id, provided by Contentsquare.com (e.g.: 'a6f73d509') - Mandatory.
* @param {boolean} [scriptOptions.defer=false] - Script will be fetched in parallel with the HTML parsing, but execution will occur after the HTML document has been fully parsed.
* @param {boolean} [scriptOptions.async=true] - Script will be fetched in parallel with the HTML parsing, and it will be executed as soon as it is available, even if the HTML document is not fully parsed.
* @param {string} [scriptOptions.integrity] - Integrity hash (SRI) of Contentsquare script. Must be generated by Contentsquare.
* @returns {HTMLScriptElement} The contentsquare script that was appended to document.head
*/
export function injectContentsquareScript(scriptOptions: ScriptOptions) {
export function injectContentsquareScript({ clientId, defer = false, async = true, integrity }: ScriptOptions) {
const scriptElement = document.createElement("script");

scriptElement.type = "text/javascript";
scriptElement.defer = defer;
scriptElement.async = async;

scriptElement.defer =
typeof scriptOptions.defer === "boolean" ? scriptOptions.defer : false;

scriptElement.async =
typeof scriptOptions.async === "boolean" ? scriptOptions.async : true;

if (scriptOptions.integrity) {
scriptElement.integrity = scriptOptions.integrity;
if (integrity) {
scriptElement.integrity = integrity;
}

scriptElement.crossOrigin = "anonymous";

scriptElement.src =
"https://t.contentsquare.net/uxa/" + scriptOptions.clientId + ".js";
scriptElement.src = `https://t.contentsquare.net/uxa/${clientId}.js`;

return document.head.appendChild(scriptElement);
}

0 comments on commit 8e11c17

Please sign in to comment.