Skip to content

Commit 2ff2ff6

Browse files
authored
Update replaceHnDynamicMutation.js
1 parent 8075d21 commit 2ff2ff6

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed
Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,78 @@
11
/*****
22
Exemple de cas d'utilisation sur le media slider >>>https://solocaldudaadmin.eu-responsivesiteeditor.com/site/89d42e60376b4fd2bc8889771347e00a/home?preview=true&insitepreview=true&dm_device=desktop
33
******/
4-
const startCheckMutation = (params) => {
5-
const { targetClassName, targetChild, replaceNode, classe } = params;
4+
export const replaceOrRemoveNodeHn = (params) => {
5+
const { targetSelector, targetChild, replaceNode, classe, remove } = params;
66

77
const checkInitialNodes = () => {
8-
const nodes = document.querySelectorAll(`.${targetClassName}`);
8+
const nodes = document.querySelectorAll(targetSelector);
99
nodes.forEach((node) => {
10-
elementReplace({ targetClassName, targetChild, replaceNode, classe });
10+
elementReplace({ targetSelector, targetChild, replaceNode, classe, remove });
1111
});
1212
};
1313

1414
checkInitialNodes();
1515

16-
const mutationCallback = (mutationsList, observer) => {
17-
for (const mutation of mutationsList) {
18-
if (mutation.type === 'childList') {
19-
mutation.addedNodes.forEach((addedNode) => {
20-
if (addedNode.nodeType === Node.ELEMENT_NODE) {
21-
const targetNodes = addedNode.matches(`.${targetClassName}`)
22-
? [addedNode]
23-
: Array.from(addedNode.querySelectorAll(`.${targetClassName}`));
24-
25-
targetNodes.forEach((node) => {
26-
elementReplace({ targetClassName, targetChild, replaceNode, classe });
27-
});
28-
}
29-
});
16+
const mutationCallback = (mutationsList, observer) => {
17+
for (const mutation of mutationsList) {
18+
if (mutation.type === 'childList') {
19+
mutation.addedNodes.forEach((addedNode) => {
20+
if (addedNode.nodeType === Node.ELEMENT_NODE) {
21+
const matchesTarget = addedNode.matches(targetSelector);
22+
const targetNodes = matchesTarget
23+
? [addedNode]
24+
: Array.from(addedNode.querySelectorAll(targetSelector));
25+
targetNodes.forEach((node) => {
26+
elementReplace({ targetSelector, targetChild, replaceNode, classe, remove });
27+
});
28+
}
29+
});
30+
}
3031
}
31-
}
32-
};
32+
};
33+
3334

3435
const observer = new MutationObserver(mutationCallback);
3536
observer.observe(document.body, { childList: true, subtree: true });
3637
};
3738

3839
const elementReplace = (params) => {
39-
const { targetClassName, targetChild, replaceNode, classe } = params;
40-
$('.'+targetClassName)?.find(targetChild)?.each(function () {
41-
$(this).replaceWith(`<${replaceNode} class="${classe} processed">${$(this).text()}</${replaceNode}>`);
42-
startCheckMutation(params);
40+
const { targetSelector, targetChild, replaceNode, classe, remove } = params;
41+
document.querySelectorAll(targetSelector).forEach((targetNode) => {
42+
targetNode.querySelectorAll(targetChild)?.forEach((childNode) => {
43+
if (!remove) {
44+
const newNode = document.createElement(replaceNode);
45+
newNode.className = `${classe} processed`;
46+
newNode.textContent = childNode.textContent;
47+
childNode.replaceWith(newNode);
48+
} else {
49+
childNode.remove();
50+
}
51+
});
4352
});
4453
};
4554

4655

47-
startCheckMutation({
48-
targetClassName: 'd-ext-mediaSlider-slidesContainer__slide',
49-
targetChild: 'h3',
50-
replaceNode: 'div',
51-
classe: 'SoMS-slideSlot-item-title',
52-
});
56+
/************ Mis en place et utilisation du script (depuis un module ESM importé) ***********/
57+
58+
59+
/*
60+
<script type="module" async>
61+
import * as RHN from 'https://de.cdn-website.com/12fb6ded409c4e3489847c649d17e9f6/files/uploaded/replaceOrRemoveNodesHn-1.03.mjs';
62+
const env = dmAPI.getCurrentEnvironment();
63+
if(env !== 'editor') {
64+
//Remplacement des h3 du widget "accordion"
65+
RHN.replaceOrRemoveNodeHn({targetSelector: '[data-auto="runtime-accordion-widget"]',targetChild: 'h3',
66+
replaceNode: 'div',classe: 'SoMS-accordion-item-title',remove: false});
67+
68+
//Remplacement des h3 du widget "slider"
69+
RHN.replaceOrRemoveNodeHn({targetSelector: '[data-auto="slider-filmRole"]',targetChild: 'h3',
70+
replaceNode: 'div',classe: 'SoMS-slideSlot-item-title',remove: false});
71+
72+
//Remplacement des h3.caption-title du widget "galerie de photos"
73+
RHN.replaceOrRemoveNodeHn({targetSelector: '.dmPhotoGallery ',targetChild: 'h3',
74+
replaceNode: 'span',classe: 'SoMS-caption-title',remove: false});
75+
}
76+
</script>
77+
78+
*/

0 commit comments

Comments
 (0)