-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathpreviewImages.js
More file actions
98 lines (75 loc) · 2.06 KB
/
previewImages.js
File metadata and controls
98 lines (75 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// previewImages.js
// https://github.com/bgrins/devtools-snippets
// Get a preview of all images(background) in a page.
(function(console) {
"use strict"
var urls = [];
function stash(value) {
for(var i = 0, j = urls.length; i < j; i++) {
if(urls[i] == value) {
return true;
}
}
return false;
}
function getBox(width, height) {
return {
string: "+",
style: "font-size: 1px; padding: " + Math.floor(height/2) + "px " + Math.floor(width/2) + "px; line-height: " + height + "px;"
}
}
function getallBgimages(){
var url, $el = document.getElementsByTagName('*');
$el = Array.prototype.slice.call($el, 0, $el.length);
while($el.length){
url = getStyle($el.shift(),'background-image');
if(url) {
url = /url\(['"]?([^")]+)/.exec(url) || [];
}
url = url[1];
if(url && urls.indexOf(url) == -1) {
if(!stash(url)) {
urls.push(url);
}
}
}
}
function getAllimages() {
var $el = document.getElementsByTagName('img');
$el = Array.prototype.slice.call($el, 0, $el.length);
for(var i = 0, j = $el.length; i < j; i++) {
if(!stash($el[i].src)) {
urls.push($el[i].src);
}
}
getallBgimages();
}
function init() {
getAllimages();
for(var i = 0, j = urls.length; i < j; i++) {
consoleImage(urls[i]);
}
}
function getStyle($el, css){
if(!$el || !$el.style) return '';
var style = css.replace(/\-([a-z])/g, function(a, b){
return b.toUpperCase();
});
if($el.currentStyle){
return $el.style[style] || $el.currentStyle[style] || '';
}
var view = document.defaultView || window;
return $el.style[style] || view.getComputedStyle($el,"").getPropertyValue(css) || '';
}
function consoleImage(url, scale) {
scale = scale || 1;
var img = new Image();
img.onload = function() {
var dim = getBox(200, 100);
console.log("%c" + dim.string, dim.style + "background: url(" + url + "); background-size: 200px 200px; color: transparent;");
console.log(url);
};
img.src = url;
};
init();
})(console);