vanilla-lazyload
2 months ago
jquery.lazy.av.min.js
4 years ago
jquery.lazy.iframe.min.js
4 years ago
jquery.lazy.js
3 years ago
jquery.lazy.min.js
3 years ago
lazyload.min.js
4 years ago
loader.js
3 years ago
two_delay.js
2 years ago
two_elementor_video_to_iframe.js
4 years ago
two_lazyload.js
3 years ago
two_merge_google_font_faces.js
3 years ago
two_worker.js
2 years ago
two_yt_vi_lazyload.js
3 years ago
two_yt_vi_lazyload.min.js
3 years ago
two_worker.js
288 lines
| 1 | let two_css_length = 0; |
| 2 | let two_connected_css_length = 0; |
| 3 | let two_uncritical_fonts = null; |
| 4 | let two_uncritical_fonts_status = false; |
| 5 | if(two_font_actions == "not_load" || two_font_actions == "exclude_uncritical_fonts"){ |
| 6 | two_uncritical_fonts_status = true; |
| 7 | } |
| 8 | |
| 9 | self.addEventListener("message", function(e) { |
| 10 | two_css_length = e.data.css.length; |
| 11 | if(!e.data.critical_data.critical_css || !e.data.critical_data.critical_fonts){ |
| 12 | two_uncritical_fonts_status = false; |
| 13 | } |
| 14 | if(e.data.font.length>0){ |
| 15 | two_fetch_inbg(e.data.font, "font"); |
| 16 | } |
| 17 | if(e.data.js.length>0){ |
| 18 | two_fetch_inbg(e.data.js, "js"); |
| 19 | } |
| 20 | if(e.data.excluded_js.length>0){ |
| 21 | two_fetch_inbg(e.data.excluded_js, "js" , true); |
| 22 | } |
| 23 | if(e.data.css.length>0){ |
| 24 | two_fetch_inbg(e.data.css, "css"); |
| 25 | } |
| 26 | }, false); |
| 27 | |
| 28 | function two_fetch_inbg(data, type, excluded_js = false) { |
| 29 | for(let i in data){ |
| 30 | if(typeof data[i].url != "undefined"){ |
| 31 | var modifiedScript = null; |
| 32 | if(type === "js" && typeof data[i].exclude_blob != "undefined" && data[i].exclude_blob){ |
| 33 | modifiedScript = { |
| 34 | id: i, |
| 35 | status: 'ok', |
| 36 | type: type, |
| 37 | url: data[i].url, |
| 38 | uid:data[i].uid |
| 39 | }; |
| 40 | two_send_worker_data(modifiedScript); |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | fetch(data[i].url, {mode:'no-cors',redirect: 'follow'}).then((r) => { |
| 47 | if (!r.ok || r.status!==200) { |
| 48 | throw Error(r.statusText); |
| 49 | } |
| 50 | if(two_uncritical_fonts_status && type== "css"){ |
| 51 | return (r.text()); |
| 52 | }else{ |
| 53 | return (r.blob()); |
| 54 | } |
| 55 | }).then((content_) => { |
| 56 | let sheetURL = ""; |
| 57 | if(two_uncritical_fonts_status && type == "css"){ |
| 58 | sheetURL = two_create_blob(content_); |
| 59 | }else{ |
| 60 | sheetURL = URL.createObjectURL(content_); |
| 61 | } |
| 62 | modifiedScript = null; |
| 63 | if(type == "css"){ |
| 64 | modifiedScript = { |
| 65 | id: i, |
| 66 | type: type, |
| 67 | status: 'ok', |
| 68 | media: data[i].media, |
| 69 | url: sheetURL, |
| 70 | uid:data[i].uid, |
| 71 | original_url: data[i].url, |
| 72 | two_uncritical_fonts:two_uncritical_fonts, |
| 73 | }; |
| 74 | }else if(type == "js"){ |
| 75 | modifiedScript = { |
| 76 | id: i, |
| 77 | status: 'ok', |
| 78 | type: type, |
| 79 | url: sheetURL, |
| 80 | uid:data[i].uid |
| 81 | }; |
| 82 | }else if(type == "font"){ |
| 83 | modifiedScript = { |
| 84 | status: 'ok', |
| 85 | type: type, |
| 86 | main_url: data[i].url, |
| 87 | url:sheetURL, |
| 88 | font_face:data[i].font_face |
| 89 | }; |
| 90 | } |
| 91 | if(excluded_js){ |
| 92 | modifiedScript.excluded_from_delay = true; |
| 93 | } |
| 94 | two_send_worker_data(modifiedScript); |
| 95 | }).catch(function(error) { |
| 96 | console.log("error in fetching: "+error.toString()+", bypassing "+data[i].url); |
| 97 | fetch(data[i].url, {redirect: 'follow'}).then((r) => { |
| 98 | if (!r.ok || r.status!==200) { |
| 99 | throw Error(r.statusText); |
| 100 | } |
| 101 | if(two_uncritical_fonts_status && type== "css"){ |
| 102 | return (r.text()); |
| 103 | }else{ |
| 104 | return (r.blob()); |
| 105 | } |
| 106 | }).then((content_) => { |
| 107 | let sheetURL = ""; |
| 108 | if(two_uncritical_fonts_status && type == "css"){ |
| 109 | sheetURL = two_create_blob(content_); |
| 110 | }else{ |
| 111 | sheetURL = URL.createObjectURL(content_); |
| 112 | } |
| 113 | var modifiedScript = null; |
| 114 | if(type == "css"){ |
| 115 | modifiedScript = { |
| 116 | id: i, |
| 117 | type: type, |
| 118 | status: 'ok', |
| 119 | media: data[i].media, |
| 120 | url: sheetURL, |
| 121 | uid:data[i].uid, |
| 122 | original_url: data[i].url, |
| 123 | two_uncritical_fonts:two_uncritical_fonts, |
| 124 | }; |
| 125 | }else if(type == "js"){ |
| 126 | modifiedScript = { |
| 127 | id: i, |
| 128 | status: 'ok', |
| 129 | type: type, |
| 130 | url: sheetURL, |
| 131 | uid:data[i].uid |
| 132 | }; |
| 133 | }else if(type == "font"){ |
| 134 | modifiedScript = { |
| 135 | status: 'ok', |
| 136 | type: type, |
| 137 | main_url: data[i].url, |
| 138 | url:sheetURL, |
| 139 | font_face:data[i].font_face |
| 140 | }; |
| 141 | } |
| 142 | if(excluded_js){ |
| 143 | modifiedScript.excluded_from_delay = true; |
| 144 | } |
| 145 | two_send_worker_data(modifiedScript); |
| 146 | }).catch(function(error) { |
| 147 | console.log("error in fetching no-cors: "+error.toString()+", bypassing "+data[i].url); |
| 148 | try { |
| 149 | console.log("error in fetching: "+error.toString()+", sending XMLHttpRequest"+data[i].url); |
| 150 | let r = new XMLHttpRequest; |
| 151 | if(two_uncritical_fonts_status && type== "css"){ |
| 152 | r.responseType = "text"; |
| 153 | }else{ |
| 154 | r.responseType = "blob"; |
| 155 | } |
| 156 | r.onload = function (content_) { |
| 157 | let sheetURL = ""; |
| 158 | if(two_uncritical_fonts_status && type == "css"){ |
| 159 | sheetURL = two_create_blob(content_.target.response); |
| 160 | }else{ |
| 161 | sheetURL = URL.createObjectURL(content_.target.response); |
| 162 | } |
| 163 | if(r.status !== 200){ |
| 164 | two_XMLHttpRequest_error(excluded_js, data[i], type, i); |
| 165 | return; |
| 166 | } |
| 167 | console.log("error in fetching: "+error.toString()+", XMLHttpRequest success "+data[i].url); |
| 168 | let modifiedScript = null; |
| 169 | if(type == "css"){ |
| 170 | modifiedScript = { |
| 171 | id: i, |
| 172 | type: type, |
| 173 | status: 'ok', |
| 174 | media: data[i].media, |
| 175 | url: sheetURL, |
| 176 | uid:data[i].uid, |
| 177 | two_uncritical_fonts:two_uncritical_fonts, |
| 178 | }; |
| 179 | }else if(type == "js"){ |
| 180 | modifiedScript = { |
| 181 | id: i, |
| 182 | type: type, |
| 183 | status: 'ok', |
| 184 | url: sheetURL, |
| 185 | uid:data[i].uid |
| 186 | }; |
| 187 | }else if(type == "font"){ |
| 188 | modifiedScript = { |
| 189 | type: type, |
| 190 | status: 'ok', |
| 191 | main_url: data[i].url, |
| 192 | url: sheetURL, |
| 193 | font_face:data[i].font_face |
| 194 | }; |
| 195 | } |
| 196 | if(excluded_js){ |
| 197 | modifiedScript.excluded_from_delay = true; |
| 198 | } |
| 199 | two_send_worker_data(modifiedScript); |
| 200 | }; |
| 201 | r.onerror = function () { |
| 202 | two_XMLHttpRequest_error(excluded_js, data[i], type, i) |
| 203 | }; |
| 204 | r.open("GET", data[i].url, true); |
| 205 | r.send(); |
| 206 | |
| 207 | } catch (e) { |
| 208 | console.log("error in fetching: "+e.toString()+", running fallback for "+data[i].url); |
| 209 | var modifiedScript = null; |
| 210 | if(type == "css" || type == "js"){ |
| 211 | modifiedScript = { |
| 212 | id: i, |
| 213 | type: type, |
| 214 | status: 'error', |
| 215 | url: data[i].url, |
| 216 | uid:data[i].uid |
| 217 | }; |
| 218 | }else if(type == "font"){ |
| 219 | modifiedScript = { |
| 220 | type: type, |
| 221 | status: 'error', |
| 222 | url: data[i].url, |
| 223 | font_face:data[i].font_face |
| 224 | }; |
| 225 | } |
| 226 | if(excluded_js){ |
| 227 | modifiedScript.excluded_from_delay = true; |
| 228 | } |
| 229 | two_send_worker_data(modifiedScript); |
| 230 | } |
| 231 | }); |
| 232 | }); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | function two_XMLHttpRequest_error(excluded_js , data_i, type, i){ |
| 239 | console.log("error in fetching: XMLHttpRequest failed "+data_i.url); |
| 240 | var modifiedScript = null; |
| 241 | if(type == "css" || type == "js"){ |
| 242 | modifiedScript = { |
| 243 | id: i, |
| 244 | type: type, |
| 245 | status: 'error', |
| 246 | url: data_i.url, |
| 247 | uid:data_i.uid |
| 248 | }; |
| 249 | }else if(type == "font"){ |
| 250 | modifiedScript = { |
| 251 | type: type, |
| 252 | status: 'error', |
| 253 | url: data_i.url, |
| 254 | font_face:data_i.font_face |
| 255 | }; |
| 256 | } |
| 257 | if(excluded_js){ |
| 258 | modifiedScript.excluded_from_delay = true; |
| 259 | } |
| 260 | two_send_worker_data(modifiedScript); |
| 261 | } |
| 262 | |
| 263 | function two_create_blob(str){ |
| 264 | two_uncritical_fonts = ""; |
| 265 | const regex = /@font-face\s*\{(?:[^{}])*\}/sig; |
| 266 | str = str.replace(regex, function (e){ |
| 267 | if(e.includes("data:application")){ |
| 268 | return e; |
| 269 | } |
| 270 | two_uncritical_fonts += e; |
| 271 | return ""; |
| 272 | }); |
| 273 | let blob_data = new Blob([str], {type : "text/css"}); |
| 274 | let sheetURL = URL.createObjectURL(blob_data); |
| 275 | return sheetURL; |
| 276 | } |
| 277 | |
| 278 | function two_send_worker_data(data){ |
| 279 | if(data.type == "css"){ |
| 280 | two_connected_css_length++; |
| 281 | data.length = two_css_length; |
| 282 | data.connected_length = two_connected_css_length; |
| 283 | } |
| 284 | self.postMessage(data) |
| 285 | } |
| 286 | |
| 287 | |
| 288 |