Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactoring #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}