Skip to content

Instantly share code, notes, and snippets.

@XOP
Last active August 29, 2015 14:26
Show Gist options
  • Save XOP/7382fda61bb1790219dc to your computer and use it in GitHub Desktop.
Save XOP/7382fda61bb1790219dc to your computer and use it in GitHub Desktop.
collect hrefs, srcs, urls on the page
(function(undefined, body, selectors){
var collection = [];
var current;
for(var key in selectors){
current = body.querySelectorAll(selectors[key]);
current = [].slice.call(current);
current = current.map(function(i){ return i[key] });
collection = collection.concat(current);
}
return collection;
})(undefined, document.body, {
href : "[href]",
src : "img"
});
(function(body, selectors){
var
collection = [],
current, isStyle, url;
for(var key in selectors){
var selector = selectors[key];
// collect nodes
current = body.querySelectorAll(selector);
// convert to array
current = [].slice.call(current);
// check if style
isStyle = selector.indexOf("style") > -1;
// iterate over search map
current = current.map(function(i){
if(isStyle) {
// url(http://foo.bar) --> http://foo.bar
url = i.style.backgroundImage.replace(/(url\()(.+)(\))/g, "$2");
} else {
url = i[key];
}
return url;
});
collection = collection.concat(current);
}
return collection;
})(document.body, {
href : "a[href]",
src : "img[src]",
url : "[style*=url]"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment