-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply-settings.js
124 lines (105 loc) · 4.52 KB
/
apply-settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function() {
function interceptDOMContentLoaded(event) {
// Prevent the default DOMContentLoaded event from firing
event.stopImmediatePropagation();
console.log('DOMContentLoaded intercepted');
}
// Add an event listener to intercept the DOMContentLoaded event
document.addEventListener('DOMContentLoaded', interceptDOMContentLoaded, true);
function applyGridSettings(settings) {
// Function to replace text placeholders
function replaceTextPlaceholders(node, settings) {
let textContent = node.textContent;
// Find all text placeholders in the text content
const textPlaceholders = textContent.match(/{{txt:(.*?):(.*?)}}/g);
// console.log({textPlaceholders})
if (textPlaceholders) {
// Replace each text placeholder with the corresponding value from settings
textPlaceholders.forEach((placeholder) => {
const key = placeholder.match(/{{txt:(.*?):(.*?)}}/)[1];
console.log({key});
if (settings[key]) {
textContent = textContent.replace(placeholder, settings[key]);
}
});
// Update the node text content
node.textContent = textContent;
}
}
function replaceAttributePlaceholders(node, settings, attribute, type = 'txt') {
console.log('Attribute:', attribute, node.getAttribute(attribute));
let textContent = node.getAttribute(attribute);
// Find all text placeholders in the text content
const textPlaceholders = textContent.match(/{{(.*?):(.*?):(.*?)}}/g);
console.log({textPlaceholders})
if (textPlaceholders) {
// Replace each text placeholder with the corresponding value from settings
textPlaceholders.forEach((placeholder) => {
const [index, type, key, description] = placeholder.match(/{{(.*?):(.*?):(.*?)}}/);
console.log({index, type, key, description});
if (settings[key]) {
if (type === 'img') {
textContent = textContent.replace(placeholder, settings[key].media.url);
} else {
textContent = textContent.replace(placeholder, settings[key]);
}
}
});
console.log('Updated attibute content:', attribute, textContent)
// Update the node text content
node.setAttribute(attribute, textContent);
}
}
// Function to replace image placeholders
function replaceImagePlaceholders(element, settings) {
let srcValue = element.getAttribute('src');
// Find all image placeholders in the src attribute
const imgPlaceholders = srcValue.match(/{{img:(.*?):(.*?)}}/g);
// console.log({imgPlaceholders})
if (imgPlaceholders) {
// Replace each image placeholder with the corresponding value from settings
imgPlaceholders.forEach((placeholder) => {
const key = placeholder.match(/{{img:(.*?):(.*?)}}/)[1];
console.log({key});
if (settings[key]) {
srcValue = srcValue.replace(placeholder, settings[key]);
}
});
// Update the element's src attribute
element.setAttribute('src', srcValue);
}
}
// Iterate through all elements in the document
document.querySelectorAll('*').forEach((element) => {
// Check all child nodes of the element for text placeholders
element.childNodes.forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
replaceTextPlaceholders(node, settings);
}
});
if (element.getAttribute('source') ) {
replaceAttributePlaceholders(element, settings, 'source');
}
if (element.getAttribute('style') ) {
replaceAttributePlaceholders(element, settings, 'style');
}
// Check if the element is an image and has src attribute for image placeholders
if (element.tagName === 'IMG' && element.hasAttribute('src')) {
replaceAttributePlaceholders(element, settings, 'src', 'img');
}
});
}
window.addEventListener('GridappReady', function () {
// Remove the intercepting listener
document.removeEventListener('DOMContentLoaded', interceptDOMContentLoaded, true);
console.log('GridappReady event received');
const settings = window.gridapp.getSettings();
console.log({settings});
applyGridSettings(settings);
// Dispatch the DOMContentLoaded event manually
console.log('Dispatching DOMContentLoaded event');
const event = document.createEvent('Event');
event.initEvent('DOMContentLoaded', true, true);
document.dispatchEvent(event);
});
})();