Skip to content

Instantly share code, notes, and snippets.

@jdeagle
Created November 2, 2012 19:30
Show Gist options
  • Save jdeagle/4003806 to your computer and use it in GitHub Desktop.
Save jdeagle/4003806 to your computer and use it in GitHub Desktop.
Get all images and background images on the page
getImageURLS : function () {
var images = [],
key = {},
tags = document.getElementsByTagName('*'),
url,
el;
for (var i = 0, len = tags.length; i < len; i++) {
el = tags[i];
if (el.currentStyle) {
if (el.currentStyle.backgroundImage !== 'none') {
//el.className += ' bg_found';
url = el.currentStyle;
//console.log(i + " " + url);
}
}
else if (window.getComputedStyle) {
if (document.defaultView.getComputedStyle(el, null).getPropertyValue('background-image') !== 'none') {
//el.className += ' bg_found';
url = document.defaultView.getComputedStyle(el, null).getPropertyValue('background-image');
//console.log(i + " " + url);
}
}
if (el.nodeName === "IMG" || el.tagName === "IMG") {
url = el.src;
if (url.indexOf("http://") > -1 && key[url] === undefined) {
images.push($.trim(url));
key[url] = images.length;
}
}
if (url !== "" && url !== undefined) {
if (url.indexOf("http://") > -1 && key[url] === undefined) {
//var isURL = url.indexOf("static");
// trim quotes
url = url.indexOf('url("') > -1 ? url.slice(5, url.length - 2) : url;
url = url.indexOf("url('") > -1 ? url.slice(5, url.length - 2) : url;
url = url.indexOf('url(') > -1 ? url.slice(4, url.length - 1) : url;
//console.log(i + " " + url);
images.push($.trim(url));
key[url] = images.length;
url = "";
}
}
}
return images;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment