PageSourceSearch

https://fashionisers.com/wp-content/plugins/tenweb-speed-optimizer…/js/vanilla-lazyload/two_bg_vanilla.js

js fashionisers.com collected 2026-09-24 09:29:34 UTC 6,825 bytes, 172 lines download raw bytes

1function two_kick_bg_lazy() {
2    if (typeof window.two_replace_backgrounds === "function") {
3        window.two_replace_backgrounds();
4    }
5    if (window.two_lazyLoadInstance && typeof window.two_lazyLoadInstance.update === "function") {
6        window.two_lazyLoadInstance.update();
7    }
8}
9
10window.addEventListener("load", function () {
11    // Initial attempt (may still be too early for React/WVC rendered DOM)
12    two_kick_bg_lazy();
13
14    // If React/WVC mounts/hydrates after load, observe DOM mutations and run once.
15    // No timeouts: we react to real DOM changes and disconnect immediately after success.
16    if (window.MutationObserver) {
17        // Keep legacy behavior for non-React sites:
18        // only attach the observer when the React/WVC root exists.
19        var root = document.querySelector("#home-root");
20        if (root) {
21            var obs = new MutationObserver(function (mutations) {
22                // Run only when elements with placeholder backgrounds appear, then disconnect.
23                // Keep the per-mutation work small: check only added nodes and a limited number of descendants.
24                function hasPlaceholderBg(el) {
25                    if (!window.two_svg_placeholder) return false;
26                    var st = window.getComputedStyle(el, false);
27                    return !!(st && st.backgroundImage && st.backgroundImage.indexOf(window.two_svg_placeholder) !== -1);
28                }
29
30                for (var i = 0; i < mutations.length; i++) {
31                    var m = mutations[i];
32
33                    // React/WVC often updates classes/styles without adding new nodes.
34                    // Catch the moment the placeholder background appears via class/style changes.
35                    if (m.type === "attributes" && m.target instanceof Element) {
36                        if (hasPlaceholderBg(m.target)) {
37                            obs.disconnect();
38                            two_kick_bg_lazy();
39                            return;
40                        }
41                        continue;
42                    }
43
44                    if (m.type !== "childList") continue;
45
46                    var added = m.addedNodes;
47                    if (!added || !added.length) continue;
48
49                    for (var j = 0; j < added.length; j++) {
50                        var node = added[j];
51                        if (!(node instanceof Element)) continue;
52
53                        if (hasPlaceholderBg(node)) {
54                            obs.disconnect();
55                            two_kick_bg_lazy();
56                            return;
57                        }
58
59                        // Check a limited number of descendants to avoid heavy scans on large subtrees.
60                        var walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT);
61                        var checked = 0;
62                        while (walker.nextNode()) {
63                            var el = walker.currentNode;
64                            if (hasPlaceholderBg(el)) {
65                                obs.disconnect();
66                                two_kick_bg_lazy();
67                                return;
68                            }
69                            checked++;
70                            if (checked >= 50) break;
71                        }
72                    }
73                }
74            });
75            obs.observe(root, {
76                childList: true,
77                subtree: true,
78                attributes: true,
79                attributeFilter: ["class", "style"]
80            });
81        }
82    }
83}, { once: true });
84window.addEventListener( 'elementor/frontend/init', () => {
85    elementorFrontend.hooks.addAction( 'frontend/element_ready/global', function( $scope ) {
86        $scope.find("*").addClass("two_elementor_element");
87        two_replace_backgrounds($scope.get( 0 ));
88    } );
89} );
90
91let twbb_slides_wrapper = document.querySelectorAll(".twbb_slides-wrapper , .e-flex.elementor-element");
92if (twbb_slides_wrapper.length > 0) {
93    window.addEventListener("scroll", function () {
94        two_calculate_position(twbb_slides_wrapper);
95    });
96
97}
98
99
100function two_calculate_position(lazy_elements) {
101    lazy_elements.forEach(function(element) {
102        if(!element.classList.contains("two_bg_lazy_init")){
103            let scrollPosition = window.scrollY || document.documentElement.scrollTop;
104            let element_top = two_get_element_position(element).top;
105            if(parseInt(element_top) < parseInt(scrollPosition)+500){
106                element.classList.add("two_bg_lazy_init");
107                two_replace_backgrounds();
108                two_lazyLoadInstance.update();
109            }
110        }
111    });
112}
113
114function two_get_element_position(element) {
115    const rect = element.getBoundingClientRect();
116    const scrollLeft = window.scrollX || document.documentElement.scrollLeft;
117    const scrollTop = window.scrollY || document.documentElement.scrollTop;
118    return {
119        top: rect.top + scrollTop,
120        left: rect.left + scrollLeft
121    };
122}
123
124
125function two_replace_backgrounds(elementor_elements = false) {
126    let two_elements_list;
127    if(!elementor_elements){
128        two_elements_list = document.querySelectorAll("*:not(br):not(hr):not(iframe):not(pre)");
129    }else{
130        two_elements_list = elementor_elements.querySelectorAll(".two_elementor_element");
131    }
132    two_elements_list.forEach((elem) => {
133        let style = elem.currentStyle || window.getComputedStyle(elem, false);
134
135        let bg_image = style.backgroundImage;
136
137        if (bg_image === 'none' || bg_image.indexOf(window['two_svg_placeholder']) === -1) {
138            return;
139        }
140
141        bg_image = bg_image.replace(window['two_svg_placeholder'], "");
142        if (!bg_image) {
143            return;
144        }
145
146        elem.classList.add("two_bg");
147        elem.classList.add("lazy");
148        elem.classList.remove("two_elementor_element");
149        elem.setAttribute("data-bg-multi", bg_image);
150    });
151
152    if (typeof two_lazyLoadInstance === "undefined") {
153         window.two_lazyLoadInstance = new LazyLoad({
154            'callback_applied': function(element, instance){
155                let settings = instance._settings;
156                var bgDataValue = element.getAttribute("data-" + settings.data_bg_multi);
157                if (!bgDataValue) {
158                    return;
159                }
160
161                if(window.getComputedStyle(element).getPropertyValue("background-image") !== bgDataValue) {
162                    let style = element.getAttribute("style");
163                    style += "background-image: " + bgDataValue + " !important;";
164                    element.setAttribute("style", style);
165                }
166            }
167        });
168    } else {
169        two_lazyLoadInstance.update();
170    }
171}
172

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.