Live collection of imgs
To find all HTML images, as @vsync has said, is as simple as var images = document.images
. This will be a live list so any images that are dynamically added or removed from the page will be automatically reflected in the list.
Extracting background images (inline and CSS)
There are a few ways to check for background images, but perhaps the most reliable way is to iterate over all the page's elements and use window.getComputedStyle
to check if each element's backgroundImage
does not equal none
. This will get background images set both inline and in CSS.
var images = [];
var elements = document.body.getElementsByTagName("*");
Array.prototype.forEach.call( elements, function ( el ) {
var style = window.getComputedStyle( el, false );
if ( style.backgroundImage != "none" ) {
images.push( style.backgroundImage.slice( 4, -1 ).replace(/['"]/g, "")
}
}
Getting the background image from window.getComputedStyle
will return the full CSS background-image
property, in the form url(...)
so you will need to remove the url(
and )
. You'll also need to remove any "
or '
surrounding the URL. You might accomplish this using backgroundImage.slice( 4, -1 ).replace(/['"]/g, "")
Only start checking once the DOM is ready, otherwise your initial scan might miss elements.
Dynamically added background images
This will not provide a live list, so you will need a MutationObserver
to watch the document, and check any changed elements for the presence of backgroundImage
.
When configuring your observer, make sure your MutationObserver
config has childList
and subtree
set to true. This means it can watch all children of the specified element (in your case the body
).
var body = document.body;
var callback = function( mutationsList, observer ){
for( var mutation of mutationsList ) {
if ( mutation.type == 'childList' ) {
// all changed children are in mutation.target.children
// so iterate over them as in the code sample above
}
}
}
var observer = new MutationObserver( callback );
var config = { characterData: true,
attributes: false,
childList: true,
subtree: true };
observer.observe( body, config );
Since searching for background images requires you to check every element in the DOM, you might as well check for <img>
s at the same time, rather than using document.images
.
Code
You would want to modify the code above so that, in addition to checking if it has a background image, you would check if its tag name is IMG
. You should also put it in a function that runs when the DOM is ready.
UPDATE: To differentiate between images and background images, you could push them to different arrays, for example to images
and bg_images
. To also identify the parents of images, you would push the image.parentNode
to a third array, eg image_parents
.
var images = [],
bg_images = [],
image_parents = [];
document.addEventListener('DOMContentLoaded', function () {
var body = document.body;
var elements = document.body.getElementsByTagName("*");
/* When the DOM is ready find all the images and background images
initially loaded */
Array.prototype.forEach.call( elements, function ( el ) {
var style = window.getComputedStyle( el, false );
if ( el.tagName === "IMG" ) {
images.push( el.src ); // save image src
image_parents.push( el.parentNode ); // save image parent
} else if ( style.backgroundImage != "none" ) {
bg_images.push( style.backgroundImage.slice( 4, -1 ).replace(/['"]/g, "") // save background image url
}
}
/* MutationObserver callback to add images when the body changes */
var callback = function( mutationsList, observer ){
for( var mutation of mutationsList ) {
if ( mutation.type == 'childList' ) {
Array.prototype.forEach.call( mutation.target.children, function ( child ) {
var style = child.currentStyle || window.getComputedStyle(child, false);
if ( child.tagName === "IMG" ) {
images.push( child.src ); // save image src
image_parents.push( child.parentNode ); // save image parent
} else if ( style.backgroundImage != "none" ) {
bg_images.push( style.backgroundImage.slice( 4, -1 ).replace(/['"]/g, "") // save background image url
}
} );
}
}
}
var observer = new MutationObserver( callback );
var config = { characterData: true,
attributes: false,
childList: true,
subtree: true };
observer.observe( body, config );
});