Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created April 26, 2010 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahemoff/378850 to your computer and use it in GitHub Desktop.
Save mahemoff/378850 to your computer and use it in GitHub Desktop.
function yoink(complete) {
var all={
scripts: [],
stylesheets: []
}
function downloadResources(urls, onComplete) {
var resources = [];
(function inlineResource(index) {
if (index==urls.length) {
onComplete(resources);
return;
}
var xhr = new XMLHttpRequest();
xhr.open("GET", urls[index], true);
xhr.onreadystatechange = function() {
if (xhr.readyState!=4) return;
if (xhr.status==200) resources.push(xhr.responseText);
inlineResource(index+1);
}
xhr.send();
})(0);
}
var scripts = document.getElementsByTagName("script");
var scriptSources = [];
for (var i=0; i<scripts.length; i++) {
if (scripts[i].src) scriptSources.push(scripts[i].src); // remote
else all.scripts.push(scripts[i].innerHTML); // inline
};
downloadResources(scriptSources, function(resources) {
all.scripts = all.scripts.concat(resources);
var styles = document.getElementsByTagName("styles");
for (var i=0; i<styles.length; i++) { all.stylesheets.push(styles[i]); };
var allElements = document.getElementsByTagName("*");
for (var i=0; i<allElements.length; i++) {
var inlineStyle = allElements[i].getAttribute("style")
if (inlineStyle) all.stylesheets.push(inlineStyle);
};
var links = document.getElementsByTagName("link");
var cssHrefs = [];
for (var i=0; i<links.length; i++) {
if (links[i].type=="text/css") cssHrefs.push(links[i].href);
};
downloadResources(cssHrefs, function(resources) {
all.stylesheets = all.stylesheets.concat(resources);
complete(all);
});
});
}
/*
yoink(function(all) {
console.log("scripts", all.scripts); // string array
console.log("stylesheets", all.stylesheets); // string array
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment