|
| 1 | +/**! |
| 2 | + * @license File-Loader.js - A JavaScript library to easily load scripts / stylesheets to HTML pages - with support for loading MULTIPLE file |
| 3 | + * VERSION: 1.0.0 |
| 4 | + * LICENSED UNDER MIT LICENSE |
| 5 | + * MORE INFO CAN BE FOUND AT https://github.com/MarketingPipeline/File-Loader.js/ |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +export function FileLoader(files_to_load) { |
| 10 | + |
| 11 | +////// FUNCTION TO LOAD SCRIPTS TO THE DOM / DOCUMENT //////// |
| 12 | + async function loadFileToDOM(file_to_load) { |
| 13 | + return new Promise(function(resolve, reject) { |
| 14 | + |
| 15 | + if (!file_to_load.url) { |
| 16 | + throw "No File Source / URL was found to load" |
| 17 | + } |
| 18 | + |
| 19 | + let file = null; |
| 20 | + |
| 21 | + |
| 22 | + let file_type = file_to_load.file_type |
| 23 | + |
| 24 | + |
| 25 | + if (file_type != "js" && file_type != "css") { |
| 26 | + throw `${file_to_load.url} failed to load - no file type was found for Script-Loader.js` |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + if (file_type == "js") { |
| 32 | + file = document.createElement('script'); |
| 33 | + file.src = file_to_load.url; |
| 34 | + for (const attributeName in file_to_load.attributes) { |
| 35 | + file.setAttribute(attributeName, file_to_load.attributes[attributeName]); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + if (file_type == "css") { |
| 42 | + file = document.createElement("link"); |
| 43 | + file.type = "text/css"; |
| 44 | + file.rel = "stylesheet"; |
| 45 | + file.href = file_to_load.url; |
| 46 | + for (const attributeName in file_to_load.attributes) { |
| 47 | + file.setAttribute(attributeName, file_to_load.attributes[attributeName]); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + file.onload = () => { |
| 55 | + resolve(file_to_load); |
| 56 | + }; |
| 57 | + file.onerror = () => { |
| 58 | + reject(file_to_load); |
| 59 | + }; |
| 60 | + |
| 61 | + if (file_to_load.append_to_head) { |
| 62 | + /// APPEND TO HEAD IS TRUE |
| 63 | + document.head.appendChild(file); |
| 64 | + } else { |
| 65 | + // APPENDING TO BODY BY DEFAULT |
| 66 | + document.body.appendChild(file); |
| 67 | + } |
| 68 | + |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | +////// END OF FUNCTION TO LOAD SCRIPTS TO THE DOM / DOCUMENT /// |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +/////// MAIN FUNCTION FOR SCRIPT ///// |
| 78 | + const promiseData = []; |
| 79 | + let filesAlreadyLoaded = "" |
| 80 | + |
| 81 | + async function loadFiles() { |
| 82 | + |
| 83 | + |
| 84 | + /// CHECK IF USER HAS PROVIDED VALID INPUT |
| 85 | + |
| 86 | + if (!files_to_load) { |
| 87 | + throw "No files to load were passed - see the github repo for proper usage" |
| 88 | + } |
| 89 | + |
| 90 | + /// CHECK IF USER IS ONLINE - DISABLED as of V1.0.0 until improved. |
| 91 | + /* |
| 92 | + if (!navigator.onLine) { |
| 93 | + throw "You are disconnected from the internet!" |
| 94 | +
|
| 95 | + } |
| 96 | + */ |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + /// Fetch files_to_load to load from URL (JSON FILE) |
| 101 | + async function fetchJSON() { |
| 102 | + const rsp = await fetch(files_to_load), |
| 103 | + data = await rsp.json(); |
| 104 | + return data |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + /// CHECK IF USER PASSED AN ARRAY OR URL |
| 109 | + if (!(files_to_load instanceof Array)) { |
| 110 | + /// USER DID NOT PASS AN ARRAY |
| 111 | + try { |
| 112 | + // FETCH SCRIPTS TO LOAD FROM URL |
| 113 | + let data = await fetchJSON() |
| 114 | + for (const fileToLoad in data) { |
| 115 | + // MAKE SURE JSON IS VALID.. |
| 116 | + if (data[fileToLoad].file_url === undefined) { |
| 117 | + throw "JSON file loaded from url was not valid.." |
| 118 | + } else { |
| 119 | + // JSON WAS VALID AND CONTAINS FILE URLS TO LOAD. |
| 120 | + files_to_load = data |
| 121 | + } |
| 122 | + } |
| 123 | + } catch (error) { |
| 124 | + /// SOMETHING WENT WRONG :( |
| 125 | + throw `Failed To Load JSON: ${error}` |
| 126 | + } |
| 127 | + } else { |
| 128 | + /// User is NOT loading files_to_load from a JSON url stored somewhere & is passing them locally to scriptloader() |
| 129 | + |
| 130 | + |
| 131 | + // make sure arrary is not empty |
| 132 | + if (files_to_load.length == 0) { |
| 133 | + throw "No files to load were passed - see the github repo for proper usage" |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + // function to make sure files_to_load are not already loaded by user for another instance of a library... |
| 141 | + function isFileAlreadyLoaded(url) { |
| 142 | + if (!url) url = "http://xxx.co.uk/xxx/script.js"; |
| 143 | + |
| 144 | + let file_type = url.file_type |
| 145 | + |
| 146 | + let files_to_load_in_page; |
| 147 | + |
| 148 | + if (file_type == "js") { |
| 149 | + files_to_load_in_page = document.getElementsByTagName('script'); |
| 150 | + } else { |
| 151 | + files_to_load_in_page = document.getElementsByTagName('link'); |
| 152 | + } |
| 153 | + |
| 154 | + for (var i = files_to_load_in_page.length; i--;) { |
| 155 | + |
| 156 | + //console.log(files_to_load) |
| 157 | + if (file_type == "js") { |
| 158 | + if (files_to_load_in_page[i].src == url.url) |
| 159 | + return true; |
| 160 | + } |
| 161 | + // |
| 162 | + if (file_type == "css") { |
| 163 | + if (files_to_load_in_page[i].href == url.url) |
| 164 | + //console.log("gf") |
| 165 | + return true; |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + files_to_load.forEach(function(file_to_load) { |
| 174 | + // make sure files_to_load are not already loaded by user for another library etc.. |
| 175 | + // console.log(files_to_load[1]) |
| 176 | + if (isFileAlreadyLoaded(file_to_load) === false) { |
| 177 | + // file is not already in the DOM |
| 178 | + promiseData.push(loadFileToDOM(file_to_load)); |
| 179 | + } else { |
| 180 | + // File is already loaded for some reason? :o |
| 181 | + filesAlreadyLoaded += file_to_load.url |
| 182 | + } |
| 183 | + }); |
| 184 | + const LoadedFiles = await Promise.all(promiseData).then(async function() { |
| 185 | + if (filesAlreadyLoaded.length != 0) { |
| 186 | + return `Scripts Have Been Loaded - these files have been avoided. They were already loaded by user / you in another script. |
| 187 | +${filesAlreadyLoaded}` |
| 188 | + } else { |
| 189 | + return `Scripts Have Been Loaded` |
| 190 | + } |
| 191 | + |
| 192 | + }).catch(function(error) { |
| 193 | + if(error.url){ |
| 194 | + throw `File-Loader.js Error: ${error.url} failed to load! File Type Set To - ${error.file_type}`; |
| 195 | + }else{ |
| 196 | + throw `File-Loader.js Error: ${error}`; |
| 197 | + } |
| 198 | + }); |
| 199 | + |
| 200 | + return await LoadedFiles |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + return loadFiles() |
| 206 | +/// END OF MAIN FUNCTION FOR SCRIPT //// |
| 207 | +} |
0 commit comments