|
| 1 | +function loadResources(sources, callback) { |
| 2 | + let loadedCount = 0; |
| 3 | + const totalSources = sources.length; |
| 4 | + |
| 5 | + function loadScript(source) { |
| 6 | + const script = document.createElement('script'); |
| 7 | + script.src = source; |
| 8 | + script.onload = script.onreadystatechange = function() { |
| 9 | + if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') { |
| 10 | + loadedCount++; |
| 11 | + if (loadedCount === totalSources && typeof callback === 'function') { |
| 12 | + callback(); |
| 13 | + } |
| 14 | + script.onload = script.onreadystatechange = null; |
| 15 | + } |
| 16 | + }; |
| 17 | + script.onerror = function() { |
| 18 | + console.error('Error loading script: ', source); |
| 19 | + loadedCount++; |
| 20 | + if (loadedCount === totalSources && typeof callback === 'function') { |
| 21 | + callback(); |
| 22 | + } |
| 23 | + }; |
| 24 | + document.body.appendChild(script); |
| 25 | + } |
| 26 | + |
| 27 | + function loadCSS(source) { |
| 28 | + const link = document.createElement('link'); |
| 29 | + link.rel = 'stylesheet'; |
| 30 | + link.href = source; |
| 31 | + link.onload = function() { |
| 32 | + loadedCount++; |
| 33 | + if (loadedCount === totalSources && typeof callback === 'function') { |
| 34 | + callback(); |
| 35 | + } |
| 36 | + }; |
| 37 | + link.onerror = function() { |
| 38 | + console.error('Error loading CSS: ', source); |
| 39 | + loadedCount++; |
| 40 | + if (loadedCount === totalSources && typeof callback === 'function') { |
| 41 | + callback(); |
| 42 | + } |
| 43 | + }; |
| 44 | + document.head.appendChild(link); |
| 45 | + } |
| 46 | + |
| 47 | + sources.forEach(source => { |
| 48 | + if (source.endsWith('.css')) { |
| 49 | + loadCSS(source); |
| 50 | + } else if (source.endsWith('.js')) { |
| 51 | + loadScript(source); |
| 52 | + } else { |
| 53 | + console.error('Unsupported file type for source: ', source); |
| 54 | + loadedCount++; |
| 55 | + if (loadedCount === totalSources && typeof callback === 'function') { |
| 56 | + callback(); |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +function executeAppResponse() { |
| 63 | + try { |
| 64 | + // Assuming app.response.js is in the same directory |
| 65 | + import('./app.response.js') |
| 66 | + .then(module => { |
| 67 | + if (typeof module.default === 'function') { |
| 68 | + const result = module.default(); |
| 69 | + // Assuming you have a function to handle the result |
| 70 | + handleResult(result); |
| 71 | + } else { |
| 72 | + console.error('Error: app.response is not a function'); |
| 73 | + } |
| 74 | + }) |
| 75 | + .catch(error => { |
| 76 | + console.error('Error loading app.response.js: ', error); |
| 77 | + }); |
| 78 | + } catch (error) { |
| 79 | + console.error('Error executing app.response: ', error); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Example usage: |
| 84 | +const sources = [ |
| 85 | + /* SCRIPT section */ |
| 86 | + 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js', |
| 87 | + 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.3/js/bootstrap.min.js', |
| 88 | + 'https://cdnjs.cloudflare.com/ajax/libs/foundation/6.7.5/js/foundation.min.js', |
| 89 | + 'https://semantic-ui.com/dist/semantic.min.js', |
| 90 | + 'https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js', |
| 91 | + 'https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js', |
| 92 | + 'https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js', |
| 93 | + 'https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js', |
| 94 | + 'https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.min.js', |
| 95 | + 'https://d3js.org/d3.v5.min.js', |
| 96 | + 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js', |
| 97 | + 'https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js', |
| 98 | + 'https://cdnjs.cloudflare.com/ajax/libs/uikit/3.16.19/js/uikit.min.js', |
| 99 | + 'https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js', |
| 100 | + 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js', |
| 101 | + 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.min.js', |
| 102 | + 'https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.sound.min.js', |
| 103 | + 'https://unpkg.com/rxjs/bundles/rxjs.umd.min.js', |
| 104 | + /* CSS section */ |
| 105 | + 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.3/css/bootstrap.min.css', |
| 106 | + 'https://cdnjs.cloudflare.com/ajax/libs/foundation/6.7.5/css/foundation.min.css', |
| 107 | + 'https://semantic-ui.com/dist/semantic.min.css', |
| 108 | + 'https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.4/css/bulma.min.css', |
| 109 | + 'https://cdnjs.cloudflare.com/ajax/libs/hint.css/2.7.0/hint.min.css', |
| 110 | + 'https://cdn.tailwindcss.com/3.3.1.css', |
| 111 | + 'https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css', |
| 112 | + 'https://cdnjs.cloudflare.com/ajax/libs/uikit/3.16.19/css/uikit.min.css', |
| 113 | + 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css', |
| 114 | + 'https://use.fontawesome.com/releases/v5.8.2/css/all.css' |
| 115 | +]; |
| 116 | + |
| 117 | +loadResources(sources, function() { |
| 118 | + // All scripts and CSS files have been loaded, execute further actions here |
| 119 | + console.log('All resources loaded successfully.'); |
| 120 | + executeAppResponse(); |
| 121 | +}); |
| 122 | + |
| 123 | +function handleResult(result) { |
| 124 | + // Assuming you want to store the result in a multidimensional array with timestamp as index |
| 125 | + const timestamp = new Date().getTime(); |
| 126 | + const indexedResult = { [timestamp]: result }; |
| 127 | + console.log('Result:', indexedResult); |
| 128 | +} |
0 commit comments