PageSourceSearch

https://www.babyandfamily.de/plugins/dzm_resources/frontend/js/consent-script-activator.js?v=1.11.2

js babyandfamily.de collected 2026-10-08 10:38:27 UTC 5,052 bytes, 144 lines download raw bytes

1(function () {
2    'use strict';
3
4    const SELECTOR_INERT_SCRIPT = 'script[type="text/plain"][data-dzm-consent]';
5    const SELECTOR_INERT_TEMPLATE = 'template[data-dzm-consent]';
6    const SELECTOR_FALLBACK = '[data-dzm-consent-fallback]';
7    const STORAGE_KEY = 'consent';
8
9    function readStoredSettings() {
10        try {
11            const raw = localStorage.getItem(STORAGE_KEY);
12            if (!raw) {
13                return null;
14            }
15            const parsed = JSON.parse(raw);
16            return parsed && typeof parsed === 'object' ? (parsed.settings || null) : null;
17        } catch (e) {
18            return null;
19        }
20    }
21
22    function activateScript(original) {
23        const clone = document.createElement('script');
24
25        for (const attr of Array.from(original.attributes)) {
26            const name = attr.name;
27            if (name === 'type' || name === 'data-dzm-consent') {
28                continue;
29            }
30            if (name === 'data-dzm-src') {
31                clone.setAttribute('src', attr.value);
32            } else if (name === 'data-dzm-async') {
33                clone.async = true;
34            } else if (name === 'data-dzm-defer') {
35                clone.defer = true;
36            } else {
37                clone.setAttribute(name, attr.value);
38            }
39        }
40
41        if (!clone.hasAttribute('type')) {
42            clone.type = 'text/javascript';
43        }
44
45        if (!clone.src) {
46            clone.text = original.textContent || '';
47        }
48
49        const parent = original.parentNode;
50        if (parent) {
51            parent.insertBefore(clone, original);
52            parent.removeChild(original);
53        }
54    }
55
56    function activateTemplate(template) {
57        const parent = template.parentNode;
58        if (!parent) {
59            return;
60        }
61        const fragment = template.content ? template.content.cloneNode(true) : null;
62        if (fragment) {
63            // Script-Tags innerhalb des Templates werden beim reinen clone nicht ausgefuehrt.
64            // Deshalb manuell als echte Script-Elemente nachziehen.
65            const inlineScripts = fragment.querySelectorAll ? fragment.querySelectorAll('script') : [];
66            inlineScripts.forEach(function (scriptNode) {
67                const real = document.createElement('script');
68                for (const attr of Array.from(scriptNode.attributes)) {
69                    real.setAttribute(attr.name, attr.value);
70                }
71                if (!real.src) {
72                    real.text = scriptNode.textContent || '';
73                }
74                scriptNode.parentNode.replaceChild(real, scriptNode);
75            });
76            parent.insertBefore(fragment, template);
77        }
78        parent.removeChild(template);
79    }
80
81    function removeFallback(consentUid) {
82        const escaped = (window.CSS && CSS.escape) ? CSS.escape(consentUid) : consentUid.replace(/"/g, '\\"');
83        const nodes = document.querySelectorAll(SELECTOR_FALLBACK + '[data-dzm-consent-fallback="' + escaped + '"]');
84        nodes.forEach(function (node) {
85            node.parentNode && node.parentNode.removeChild(node);
86        });
87    }
88
89    function activateAcceptedScripts(settings) {
90        if (!settings || typeof settings !== 'object') {
91            return;
92        }
93
94        const activatedUids = new Set();
95
96        document.querySelectorAll(SELECTOR_INERT_SCRIPT).forEach(function (script) {
97            const uid = script.getAttribute('data-dzm-consent');
98            if (!uid || settings[uid] !== true) {
99                return;
100            }
101            try {
102                activateScript(script);
103                activatedUids.add(uid);
104            } catch (e) {
105                // absichtlich still – ein defektes Script darf den Rest nicht blockieren
106            }
107        });
108
109        document.querySelectorAll(SELECTOR_INERT_TEMPLATE).forEach(function (template) {
110            const uid = template.getAttribute('data-dzm-consent');
111            if (!uid || settings[uid] !== true) {
112                return;
113            }
114            try {
115                activateTemplate(template);
116                activatedUids.add(uid);
117            } catch (e) {
118                // dito
119            }
120        });
121
122        activatedUids.forEach(removeFallback);
123    }
124
125    function onConsentUpdated(event) {
126        const settings = event && event.detail ? event.detail : null;
127        activateAcceptedScripts(settings);
128    }
129
130    document.addEventListener('consent.updated', onConsentUpdated);
131    document.addEventListener('consent.ready', onConsentUpdated);
132
133    // Initial-Run: consent.updated feuert beim reinen Page-Load nicht zwingend erneut.
134    const initial = readStoredSettings();
135    if (initial) {
136        if (document.readyState === 'loading') {
137            document.addEventListener('DOMContentLoaded', function () {
138                activateAcceptedScripts(initial);
139            }, { once: true });
140        } else {
141            activateAcceptedScripts(initial);
142        }
143    }
144})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.