Last active
September 7, 2024 15:02
-
-
Save adamhavel/d7c87f6dcde81a6fbd8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
function getType(src) { | |
return src.match(/\.([^\.\?]+)(?:\?.*)?$/)[1]; | |
} | |
function injectContent(content, type) { | |
if (type === 'css') { | |
var style = document.createElement('style'); | |
document.head.appendChild(style); | |
style.textContent = content; | |
} else { | |
document.head.insertAdjacentHTML('beforeend', content); | |
} | |
} | |
window.loadFile = function(src) { | |
if (!window.localStorage || !window.addEventListener) { | |
return false; | |
} | |
var content = localStorage[src]; | |
if (content) { | |
injectContent(content, getType(src)); | |
} else { | |
var xhr = new XMLHttpRequest(); | |
xhr.addEventListener('load', function() { | |
localStorage[src] = xhr.responseText; | |
injectContent(xhr.responseText, getType(src)); | |
}); | |
xhr.open('GET', src); | |
xhr.send(); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment