-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotter.js
326 lines (270 loc) · 7.06 KB
/
spotter.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var spotter = (function () {
var body = document.body;
var width = 176;
var height = 109;
var whRatio = width / height;
var minSize = 100;
var notFilenameRegex = /^.*[/]/;
/**
* Create a HTML element
*
* @param {string} tag - HTML tag
* @param {?string} [id]
* @param {?string} [cls]
* @returns {Element}
*/
function createEl(tag, id, cls) {
var el = document.createElement(tag);
if (id) {
el.id = id;
}
if (cls) {
el.className = cls;
}
return el;
}
/**
* Shortcut function
*
* @param {string} id
* @returns {Element}
*/
function getById(id) {
return document.getElementById(id);
}
/**
* Builds a <style> element containing CSS used by the script generated HTML structure
*
* @returns {HTMLStyleElement}
*/
function buildStyleEl() {
var css = [
'#spotterWrapper{',
'background-color:rgba(255, 255, 255, 0.9);',
'opacity:0;',
'position:absolute;',
'top:0;',
'z-index:999999999;',
'width: 100%;',
'height: 100%;',
'}',
'#spotterContent{',
'margin-top:35px;',
'text-align:center;',
'}',
'#spotterClose{',
'background-color:#8DCB0C;',
'height:35px;',
'position:fixed;',
'width:100%;',
'z-index:1000000001;',
'}',
'#spotterClose:hover{',
'background-color:#7DA81F;',
'}',
'#spotterClose__p{',
'color:white;',
'cursor: pointer;',
'font:bold 15px Helvetica,Arial;',
'line-height:35px;',
'text-align:center;',
'text-decoration:none;',
'}',
'#spotterList{',
'padding:30px;',
'}',
'.spotterList__li{',
'background-color:#333;',
'border:1px solid #DDD;',
'display:inline-block;',
'list-style:none;',
'margin:0 20px 20px 0;',
'padding:5px;',
'}',
'.spotterList__li:hover{',
'background-color:#FFF;',
'border:1px solid #BBB;',
'}',
'.spotterList__a--wide{',
'display:table-cell;',
'vertical-align:middle;',
'}',
'.spotterList__img{',
'display:block;',
'margin:0 auto;',
'}',
'.spotterList__a:active,.spotterList__a:focus{',
'border:0;',
'outline:#888 solid 2px;',
'}',
'.spotter__opaque{',
'transition:opacity 0.1s;',
'}',
'#spotterMsg{',
'display:block;',
'font-size:14px;',
'line-height:25px;',
'margin:0 auto;',
'padding-top:30px;',
'width:600px;',
'}'
];
var style = createEl('style');
style.innerHTML = css.join('');
return style;
}
/**
* @returns {number}
*/
function getWindowHeight() {
var html = document.documentElement;
return Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
}
/**
* Build a <div> that will wrap the HTML structure generated by the script
*
* @returns {HTMLDivElement}
*/
function buildWrapperEl() {
var closeDiv = createEl('div', 'spotterClose', 'spotter__opaque');
closeDiv.onclick = close;
var closeTxt = createEl('p', 'spotterClose__p');
closeTxt.innerHTML = 'Close';
closeDiv.appendChild(closeTxt);
var wrapper = createEl('div', 'spotterWrapper', 'spotter__opaque');
//wrapper.style.height = getWindowHeight() + 'px';
wrapper.appendChild(closeDiv);
wrapper.appendChild(
createEl('div', 'spotterContent')
);
return wrapper;
}
/**
* Close and remove the wrapper element
*/
function close() {
var wrapper = getById('spotterWrapper');
wrapper.style.opacity = 0;
setTimeout(function() {
body.removeChild(wrapper);
}, 1000);
}
/**
* Build and attach an HTML structure overlaying the current page
*/
function buildOverlay() {
body.appendChild(buildStyleEl());
var wrapper = buildWrapperEl();
body.appendChild(wrapper);
body.style.margin = 0;
body.style.padding = 0;
// show the wrapper
wrapper.style.height = getWindowHeight() + 'px';
wrapper.style.opacity = 1;
// close on "ESC"
window.onkeyup = function(e) {
if (e.which == 27) {
close();
}
};
}
/**
* Check that an image's dimensions are bigger than the desired minimum size
*
* @param {HTMLImageElement} img
* @returns {boolean}
*/
function isImgBigEnough(img) {
return img.naturalWidth >= minSize && img.naturalHeight >= minSize;
}
/**
* @typedef {string} ImgSrc
*/
/**
* @typedef {object} ImgSize
* @property {number} width
* @property {number} height
*/
/**
* Store image's dimensions by their src attribute
*
* @param {object.<ImgSrc, ImgSize>} images
* @param {HTMLImageElement} img
* @returns {object.<ImgSrc, ImgSize>}
*/
function getImgData(images, img) {
images[img.src] = {
width: img.naturalWidth,
height: img.naturalHeight
};
return images;
}
/**
* Attach an image to the overlay
*
* @param {HTMLUListElement} imgList
* @param {object.<ImgSrc, ImgSize>} imgData
* @param {ImgSrc} imgSrc
*/
function makeImgSpot(imgList, imgData, imgSrc) {
var linkClass = 'spotterList__a';
var imgWidth = imgData[imgSrc].width;
var imgHeight = imgData[imgSrc].height;
var isWide = imgWidth / imgHeight >= whRatio;
var img = createEl('img', null, 'spotterList__img');
img.src = imgSrc;
if (isWide) {
img.style.width = width + 'px';
img.style.height = imgHeight * width / imgWidth + 'px';
linkClass += ' spotterList__a--wide';
} else {
img.style.width = imgWidth * height / imgHeight + 'px';
img.style.height = height+'px';
}
var a = createEl('a', null, linkClass);
a.href = imgSrc;
a.download = imgSrc.replace(notFilenameRegex, '');
a.appendChild(img);
var li = createEl('li', null, 'spotterList__li');
li.appendChild(a);
imgList.appendChild(li);
}
/**
* Get the images on the current page and show them on the overlay
*/
function getImgs() {
// the reduce function removes duplicate images
var imgData = Array.prototype.filter.call(
document.getElementsByTagName('img'),
isImgBigEnough
).reduce(getImgData, {});
var srcs = Object.keys(imgData);
var content = getById('spotterContent');
if (!srcs.length) {
var p = createEl('p', 'spotterMsg');
p.innerHTML = 'We are sorry to tell you that the images on this page are too small.';
content.appendChild(p);
} else {
var ul = createEl('ul', 'spotterList', ' spotter__opaque');
srcs.forEach(makeImgSpot.bind(null, ul, imgData));
content.appendChild(ul);
}
}
return function init() {
// skip if the overlay is already built
if (getById('spotterWrapper')) {
return;
}
// scroll to the top of the window so the found images are visible
scrollTo(0, 0);
buildOverlay();
getImgs();
};
})();
spotter();