From a2beb71de0c396c44d3ebe1443548671e6dbf18e Mon Sep 17 00:00:00 2001 From: Lubber Date: Tue, 17 Oct 2017 21:28:27 +0200 Subject: [PATCH] Also look for @import url in stylesheets --- stackload.js | 30 +++++++++++++++++++++++++----- stackload.min.js | 2 +- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/stackload.js b/stackload.js index b80809c..e80c7dd 100644 --- a/stackload.js +++ b/stackload.js @@ -28,11 +28,13 @@ eT.removeEventListener("error", stackLoadError); eT.removeEventListener("load", stackLoadDone); currentLoadIndex++; - if(eT.jsonp && eT.parentNode) { eT.parentNode.removeChild(eT); } if(!stopAll){ + if(eT.href) { + searchCssImport(document.styleSheets[document.styleSheets.length-1]); + } if(currentLoadIndex===callBacks[0].doneIndex) { callBacks[0].success(); callBacks.shift(); @@ -78,6 +80,9 @@ catch (e) {} }, setup = function(stack){ + if(typeof stack === 'undefined') { + return; + } if(typeof stack === 'string' || stack.url){ stack = {files:[stack]}; } else if(Array.isArray(stack)){ @@ -119,6 +124,7 @@ if(cL>0){ fullStack = fullStack.concat(cleanedStack); var fL = fullStack.length; + if (typeof stack.error !== 'function') { stack.error = function(){}; } @@ -134,20 +140,34 @@ stack.success(); } } + }, + searchCssImport = function(styleSheet) { + if (styleSheet.cssRules) { + for (var j = 0, jl = styleSheet.cssRules.length; j < jl; j++) { + if (styleSheet.cssRules[j].href && registry.indexOf(styleSheet.cssRules[j].href)===-1) { + registry.push(styleSheet.cssRules[j].href); + } + } + } } ; window.stackLoad = function(stack) { stopAll=false; if(registry.length===0){ - var elements = document.querySelectorAll('link,script'),i; + var elements = document.querySelectorAll('link,script'),i,il; //forEach on querySelectorAll not supported in IE/Edge, thus usual for loop - for (i=0;i0&&(fullStack.splice(currentLoadIndex,r),callBacks.shift(),callBacks.forEach(function(e,t,n){n[t].doneIndex-=r}))}else currentLoadIndex===callBacks[0].doneIndex&&(callBacks[0].success(),callBacks.shift()),currentLoadIndex===fullStack.length||stopAll||loadSingle()},loadSingle=function(){var o=fullStack[currentLoadIndex];try{if(!o.check||void 0===eval(o.check)){var element;o.type&&"css"===o.type?(element=document.createElement("link"),element.type="text/css",element.rel="stylesheet",element.href=o.url):(element=document.createElement("script"),element.type="text/javascript",element.src=o.url,element.async=!1,o.type&&"jsonp"===o.type&&(element.jsonp=!0)),document.head.appendChild(element),element.addEventListener("load",stackLoadDone),element.addEventListener("error",stackLoadError)}}catch(e){}},setup=function(e){if("string"==typeof e||e.url?e={files:[e]}:Array.isArray(e)&&(e={files:e}),e.files){var t=[];("string"==typeof e.files||e.files.url)&&(e.files=[e.files]),e.files.forEach(function(e){if("string"==typeof e&&(e={url:e}),e.url&&-1===registry.indexOf(e.url)){if(!e.type){var r=e.url.match(/\.([0-9a-zA-Z]+)(?:[?#]|$)/i);r&&(e.type=r[1].toLowerCase())}if("jsonp"===e.type.toLowerCase()){var n=(new Date).getTime();e.url.match(/\?/)?e.url+="&_="+n:e.url+="?_="+n}t.push(e),registry.push(e.url)}});var r=t.length;if("function"!=typeof e.success&&(e.success=function(){}),r>0){var n=(fullStack=fullStack.concat(t)).length;"function"!=typeof e.error&&(e.error=function(){}),callBacks.push({doneIndex:n,success:e.success,error:e.error}),currentLoadIndex===n-r&&loadSingle()}else e.success()}};window.stackLoad=function(e){if(stopAll=!1,0===registry.length){var t,r=document.querySelectorAll("link,script");for(t=0;t0&&(fullStack.splice(currentLoadIndex,t),callBacks.shift(),callBacks.forEach(function(e,r,s){s[r].doneIndex-=t}))}else r.href&&searchCssImport(document.styleSheets[document.styleSheets.length-1]),currentLoadIndex===callBacks[0].doneIndex&&(callBacks[0].success(),callBacks.shift()),currentLoadIndex===fullStack.length||stopAll||loadSingle()},loadSingle=function(){var o=fullStack[currentLoadIndex];try{if(!o.check||void 0===eval(o.check)){var element;o.type&&"css"===o.type?(element=document.createElement("link"),element.type="text/css",element.rel="stylesheet",element.href=o.url):(element=document.createElement("script"),element.type="text/javascript",element.src=o.url,element.async=!1,o.type&&"jsonp"===o.type&&(element.jsonp=!0)),document.head.appendChild(element),element.addEventListener("load",stackLoadDone),element.addEventListener("error",stackLoadError)}}catch(e){}},setup=function(e){if(void 0!==e&&("string"==typeof e||e.url?e={files:[e]}:Array.isArray(e)&&(e={files:e}),e.files)){var r=[];("string"==typeof e.files||e.files.url)&&(e.files=[e.files]),e.files.forEach(function(e){if("string"==typeof e&&(e={url:e}),e.url&&-1===registry.indexOf(e.url)){if(!e.type){var t=e.url.match(/\.([0-9a-zA-Z]+)(?:[?#]|$)/i);t&&(e.type=t[1].toLowerCase())}if("jsonp"===e.type.toLowerCase()){var s=(new Date).getTime();e.url.match(/\?/)?e.url+="&_="+s:e.url+="?_="+s}r.push(e),registry.push(e.url)}});var t=r.length;if("function"!=typeof e.success&&(e.success=function(){}),t>0){var s=(fullStack=fullStack.concat(r)).length;"function"!=typeof e.error&&(e.error=function(){}),callBacks.push({doneIndex:s,success:e.success,error:e.error}),currentLoadIndex===s-t&&loadSingle()}else e.success()}},searchCssImport=function(e){if(e.cssRules)for(var r=0,t=e.cssRules.length;r