Skip to content

Commit

Permalink
feat: [It-33] implement external script inserter
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor-romaniuk committed Jun 19, 2024
1 parent a0d3c54 commit aeadfbd
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 121 deletions.
1 change: 0 additions & 1 deletion scripts/index.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {
import {
configure as configureAnalytics, SegmentAnalyticsService, identifyAnonymousUser, identifyAuthenticatedUser,
} from './analytics';
import { GoogleAnalyticsLoader, TealiumLoader } from './scripts';
import { GoogleAnalyticsLoader, ScriptInserter } from './scripts';
import {
getAuthenticatedHttpClient,
configure as configureAuth,
Expand Down Expand Up @@ -290,7 +290,7 @@ export async function initialize({
analyticsService = SegmentAnalyticsService,
authService = AxiosJwtAuthService,
authMiddleware = [],
externalScripts = [GoogleAnalyticsLoader, TealiumLoader],
externalScripts = [GoogleAnalyticsLoader, ScriptInserter],
requireAuthenticatedUser: requireUser = false,
hydrateAuthenticatedUser: hydrateUser = false,
messages,
Expand Down
78 changes: 78 additions & 0 deletions src/scripts/ScriptInserter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Class representing a Script Inserter.
*/
class ScriptInserter {
/**
* Create a Script Inserter.
* @param {Array<Object>} scripts - An array of script objects to insert.
* @param {string} [scripts[].head] - The script to insert into the head section.
* @param {string} [scripts[].body.top] - The script to insert at the top of the body section.
* @param {string} [scripts[].body.bottom] - The script to insert at the bottom of the body section.
*/
constructor({ config }) {
this.scripts = config.EXTERNAL_SCRIPTS || [];
}

/**
* Inserts the scripts into their respective locations (head, body start, body end).
*/
loadScript() {
if (!this.scripts.length) {
return;
}

this.scripts.forEach((script) => {
if (script.head) {
this.insertToHead(script.head);
}
if (script.body?.top) {
this.insertToBodyTop(script.body.top);
}
if (script.body?.bottom) {
this.insertToBodyBottom(script.body.bottom);
}
});
}

/**
* Inserts content into the head section.
* @param {string} content - The content to insert into the head section.
*/
insertToHead(content) {
const head = document.head;

Check failure on line 42 in src/scripts/ScriptInserter.js

View workflow job for this annotation

GitHub Actions / tests

Use object destructuring
const tempDiv = document.createElement('div');
tempDiv.innerHTML = content;
while (tempDiv.firstChild) {
head.appendChild(tempDiv.firstChild);
}
}

/**
* Inserts content at the start of the body section.
* @param {string} content - The content to insert at the top of the body section.
*/
insertToBodyTop(content) {
const body = document.body;

Check failure on line 55 in src/scripts/ScriptInserter.js

View workflow job for this annotation

GitHub Actions / tests

Use object destructuring
const tempDiv = document.createElement('div');
tempDiv.innerHTML = content;
while (tempDiv.firstChild) {
body.insertBefore(tempDiv.firstChild, body.firstChild);
}
}

/**
* Inserts content at the end of the body section.
* @param {string} content - The content to insert at the bottom of the body section.
*/
insertToBodyBottom(content) {
const body = document.body;

Check failure on line 68 in src/scripts/ScriptInserter.js

View workflow job for this annotation

GitHub Actions / tests

Use object destructuring
const tempDiv = document.createElement('div');
tempDiv.innerHTML = content;
while (tempDiv.firstChild) {
body.appendChild(tempDiv.firstChild);
}
}
}

export default ScriptInserter;

Check failure on line 78 in src/scripts/ScriptInserter.js

View workflow job for this annotation

GitHub Actions / tests

Too many blank lines at the end of file. Max of 0 allowed
Empty file.
47 changes: 0 additions & 47 deletions src/scripts/TealiumLoader.js

This file was deleted.

69 changes: 0 additions & 69 deletions src/scripts/TealiumLoader.test.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/* eslint-disable import/prefer-default-export */
export { default as GoogleAnalyticsLoader } from './GoogleAnalyticsLoader';
export { default as TealiumLoader } from './TealiumLoader';
export { default as ScriptInserter } from './ScriptInserter';

0 comments on commit aeadfbd

Please sign in to comment.