Skip to content

Instantly share code, notes, and snippets.

@jcblw
Last active August 29, 2015 14:25
Show Gist options
  • Save jcblw/c3bb6cf94baa057858c9 to your computer and use it in GitHub Desktop.
Save jcblw/c3bb6cf94baa057858c9 to your computer and use it in GitHub Desktop.
Find page image assets
(function(){
function getBackgroundImages(tagName) {
var els = document.getElementsByTagName(tagName || '*'),
_els = [];
for (var i = 0; i < els.length; i++) {
var el = els[i],
bgImage = getStyle(el, 'background-image') || '';
if (bgImage.trim().match(/^url\((.*)\.(svg|gif|jpeg|jpg|png)(.*)\)/i)) {
_els.push(bgImage.split('url(').join('').split(')').join());
}
}
return _els;
}
function getInlineImages() {
var images = document.getElementsByTagName('img'),
_images = [];
for (var i = 0; i < images.length; i++) {
var image = images[i];
if (image.src.match(/\.(svg|gif|jpeg|jpg|png)/i)) {
_images.push(image.src);
}
}
return _images;
}
function getStyle(el, styleProp) {
var style = undefined;
if (el.currentStyle && el.currentStyle[styleProp]) {
style = el.currentStyle[styleProp];
} else if (window.getComputedStyle) {
style = window.getComputedStyle(el, null).getPropertyValue(styleProp);
}
return style;
}
return getBackgroundImages()
.concat(getInlineImages());
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment