const tags = [
{
"type": "head",
"src": "https://cnt.trvdp.com/js/955/14320.js",
"async": true,
"defer": true,
"name": "VideoInstream_Adeq",
"delivery_rate": 100,
"group_id": "2"
},
{
"type": "compound",
"headSrc": "https://adeqmedia.com",
"bodyContent": "\n",
"bodyId": null,
"defer": true,
"name": "VideoSlider_ADEQ",
"delivery_rate": 100,
"group_id": "3"
}
];
// Changed on 2025-01-10
// Utility function to log messages with color
function logMessage(message, color = 'black') {
console.log(`%c${message}`, `color: ${color}`);
}
// Function to append custom scripts to the head
function appendCustomHeadTag(content) {
try {
const head = document.head || document.getElementsByTagName('head')[0];
const tempDiv = document.createElement('div');
tempDiv.innerHTML = content;
while (tempDiv.firstChild) {
const child = tempDiv.firstChild;
head.appendChild(child);
if (child.tagName === 'SCRIPT') {
const script = document.createElement('script');
if (child.src) {
script.src = child.src;
script.async = child.async;
script.defer = child.defer;
} else {
script.type = child.type || 'text/javascript';
script.textContent = child.textContent;
}
head.removeChild(child);
head.appendChild(script);
}
}
} catch (error) {
logMessage(`[RMA] Error appending custom head tag: ${error.message}`, 'red');
}
}
// Function to create a script element
function createScript(src, isAsync = false, isDefer = false) {
const script = document.createElement('script');
script.src = src;
if (isAsync) script.async = true;
if (isDefer) script.defer = true;
return script;
}
// Function to append scripts to the head
function appendToHead(src, isAsync = false, isDefer = false) {
try {
const head = document.head || document.getElementsByTagName('head')[0];
const scriptElement = createScript(src, isAsync, isDefer);
head.appendChild(scriptElement);
} catch (error) {
logMessage(`[RMA] Error appending to head: ${error.message}`, 'red');
}
}
// Function to append scripts to the body
function appendScriptToBody(src, id, isAsync = false) {
try {
const container = id ? document.getElementById(id) : document.body;
if (container) {
const scriptElement = createScript(src, isAsync);
container.appendChild(scriptElement);
} else {
logMessage(`[RMA] Container with ID ${id} not found.`, 'orange');
}
} catch (error) {
logMessage(`[RMA] Error appending script to body: ${error.message}`, 'red');
}
}
// Function to append HTML content to the body and execute inline scripts
function appendHtmlToBody(content, id) {
try {
const container = id ? document.getElementById(id) : document.body;
if (container) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = content;
while (tempDiv.firstChild) {
const child = tempDiv.firstChild;
container.appendChild(child);
if (child.tagName === 'SCRIPT') {
const script = document.createElement('script');
if (child.src) {
script.src = child.src;
script.async = child.async;
script.defer = child.defer;
} else {
script.type = child.type || 'text/javascript';
script.textContent = child.textContent;
}
container.removeChild(child);
container.appendChild(script);
}
}
} else {
logMessage(`[RMA] Container with ID ${id} not found.`, 'orange');
}
} catch (error) {
logMessage(`[RMA] Error appending HTML to body: ${error.message}`, 'red');
}
}
// Function to select a tag by delivery rate
function selectTagByDeliveryRate(tags) {
const totalRate = tags.reduce((sum, tag) => sum + (tag.delivery_rate || 0), 0);
const randomRate = Math.random() * totalRate;
let cumulativeRate = 0;
for (const tag of tags) {
cumulativeRate += (tag.delivery_rate || 0);
if (randomRate <= cumulativeRate) {
return tag;
}
}
return tags[0]; // Fallback to the first tag
}
// Function to process all tags
function processTags(tags) {
logMessage('[RMA] Master tag has been loaded and is being executed.', 'red');
if (!Array.isArray(tags) || tags.length === 0) {
logMessage("[RMA] No valid tags provided.", 'orange');
return;
}
// Group tags by group_id or handle ungrouped tags
const groupedTags = tags.reduce((groups, tag) => {
const groupId = tag.group_id || null; // Null for ungrouped tags
groups[groupId] = groups[groupId] || [];
groups[groupId].push(tag);
return groups;
}, {});
Object.keys(groupedTags).forEach(groupId => {
const tagGroup = groupedTags[groupId];
if (groupId) {
const selectedTag = selectTagByDeliveryRate(tagGroup);
logMessage(`[RMA] Group ID ${groupId}: Selected tag -> ${JSON.stringify(selectedTag)}`, 'green');
processSingleTag(selectedTag);
} else {
tagGroup.forEach(tag => {
logMessage(`[RMA] Ungrouped tag processed -> ${JSON.stringify(tag)}`, 'purple');
processSingleTag(tag);
});
}
});
}
// Function to process a single tag
function processSingleTag(tag) {
try {
if (!tag || !tag.type) {
logMessage(`[RMA] Invalid tag provided: ${JSON.stringify(tag)}`, 'red');
return;
}
if (tag.type === 'head') {
appendToHead(tag.src, tag.async, tag.defer);
} else if (tag.type === 'body') {
if (tag.id) {
appendScriptToBody(tag.src, tag.id, tag.async);
} else {
// Fallback: Append directly to the body
const scriptElement = createScript(tag.src, tag.async, tag.defer);
document.body.appendChild(scriptElement);
}
} else if (tag.type === 'compound') {
appendToHead(tag.headSrc, false, tag.defer);
appendHtmlToBody(tag.bodyContent, tag.bodyId);
} else if (tag.type === 'customHead') {
appendCustomHeadTag(tag.content);
} else {
logMessage(`[RMA] Unknown tag type encountered: ${tag.type}`, 'orange');
}
} catch (error) {
logMessage(`[RMA] Error processing single tag: ${error.message}`, 'red');
}
}
// Execute the process
function runProcess() {
try {
processTags(tags);
} catch (error) {
logMessage(`[RMA] Error executing master tag: ${error.message}`, 'red');
}
}
if (document.readyState === 'loading') {
// If the document is still loading, listen for DOMContentLoaded
document.addEventListener("DOMContentLoaded", runProcess);
} else {
// If the document has already loaded, just run the process
runProcess();
}