即时图片集
正如@vsync 所说,查找所有HTML 图像就像var images = document.images 一样简单。这将是一个实时列表,因此从页面中动态添加或删除的任何图像都将自动反映在列表中。
提取背景图片(内联和 CSS)
有几种方法可以检查背景图像,但也许最可靠的方法是遍历所有页面元素并使用window.getComputedStyle 检查每个元素的backgroundImage 是否不等于none。这将获得内联和 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, "")
}
}
从window.getComputedStyle 获取背景图像将返回完整的CSS background-image 属性,格式为url(...),因此您需要删除url( 和)。您还需要删除 URL 周围的所有 " 或 '。您可以使用 backgroundImage.slice( 4, -1 ).replace(/['"]/g, "")
来完成此操作
只有在 DOM 准备好后才开始检查,否则您的初始扫描可能会丢失元素。
动态添加的背景图片
这不会提供实时列表,因此您需要MutationObserver 来观看文档,并检查任何更改的元素是否存在backgroundImage。
在配置观察者时,确保您的 MutationObserver 配置将 childList 和 subtree 设置为 true。这意味着它可以监视指定元素的所有子元素(在您的情况下为 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 );
由于搜索背景图片需要检查 DOM 中的每个元素,因此您不妨同时检查 <img>s,而不是使用 document.images。
代码
您可能希望修改上面的代码,以便除了检查它是否有背景图像之外,还要检查它的标签名称是否为IMG。您还应该将它放在一个在 DOM 准备好时运行的函数中。
更新:为了区分图像和背景图像,您可以将它们推送到不同的数组,例如 images 和 bg_images。要同时识别图像的父级,您可以将image.parentNode 推送到第三个数组,例如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 );
});