|
1 | 1 | /***** |
2 | 2 | Exemple de cas d'utilisation sur le media slider >>>https://solocaldudaadmin.eu-responsivesiteeditor.com/site/89d42e60376b4fd2bc8889771347e00a/home?preview=true&insitepreview=true&dm_device=desktop |
3 | 3 | ******/ |
4 | | -const startCheckMutation = (params) => { |
5 | | - const { targetClassName, targetChild, replaceNode, classe } = params; |
| 4 | +export const replaceOrRemoveNodeHn = (params) => { |
| 5 | + const { targetSelector, targetChild, replaceNode, classe, remove } = params; |
6 | 6 |
|
7 | 7 | const checkInitialNodes = () => { |
8 | | - const nodes = document.querySelectorAll(`.${targetClassName}`); |
| 8 | + const nodes = document.querySelectorAll(targetSelector); |
9 | 9 | nodes.forEach((node) => { |
10 | | - elementReplace({ targetClassName, targetChild, replaceNode, classe }); |
| 10 | + elementReplace({ targetSelector, targetChild, replaceNode, classe, remove }); |
11 | 11 | }); |
12 | 12 | }; |
13 | 13 |
|
14 | 14 | checkInitialNodes(); |
15 | 15 |
|
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 | + } |
30 | 31 | } |
31 | | - } |
32 | | -}; |
| 32 | + }; |
| 33 | + |
33 | 34 |
|
34 | 35 | const observer = new MutationObserver(mutationCallback); |
35 | 36 | observer.observe(document.body, { childList: true, subtree: true }); |
36 | 37 | }; |
37 | 38 |
|
38 | 39 | 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 | + }); |
43 | 52 | }); |
44 | 53 | }; |
45 | 54 |
|
46 | 55 |
|
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