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 + added _uxa array placeholder #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/injectContentsquareScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ describe("injectContentsquareScript", () => {
expect(script.async).toBe(true);
expect(script.integrity).toBe("");
});

it('should attached TAG to head', () => {
const clientId = '123456789';
const scriptOptions = { clientId };
injectContentsquareScript(scriptOptions);
// Check if the script has been appended to document.head
const scriptTags = document.head.getElementsByTagName('script');
const contentsquareScript = scriptTags[scriptTags.length - 1];
expect(contentsquareScript).toBeTruthy();
expect(contentsquareScript.type).toBe('text/javascript');
expect(contentsquareScript.defer).toBe(false);
expect(contentsquareScript.async).toBe(true);
expect(contentsquareScript.crossOrigin).toBe('anonymous');
expect(contentsquareScript.src).toContain('https://t.contentsquare.net/uxa/' + clientId + '.js');
});
});
24 changes: 16 additions & 8 deletions src/injectContentsquareScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ type ScriptOptions = {
clientId: string;
} & Partial<Pick<HTMLScriptElement, "defer" | "async" | "integrity">>;

const csURL = "https://t.contentsquare.net/uxa/";

/**
* 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(scriptOptions: ScriptOptions): HTMLScriptElement {

const scriptElement = document.createElement("script");

scriptElement.type = "text/javascript";
Expand All @@ -27,8 +32,11 @@ export function injectContentsquareScript(scriptOptions: ScriptOptions) {

scriptElement.crossOrigin = "anonymous";

scriptElement.src =
"https://t.contentsquare.net/uxa/" + scriptOptions.clientId + ".js";
scriptElement.src = csURL + scriptOptions.clientId + ".js";

// @ts-ignore
window._uxa = window._uxa || [];

return document.head.appendChild(scriptElement);

}