Last active
August 29, 2015 14:26
-
-
Save XOP/7382fda61bb1790219dc to your computer and use it in GitHub Desktop.
collect hrefs, srcs, urls on the page
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(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" | |
}); |
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(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