-
Notifications
You must be signed in to change notification settings - Fork 25
/
hoverzoom.js
596 lines (522 loc) · 18.4 KB
/
hoverzoom.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright (c) 2010 Romain Vallet
// Licensed under the MIT license, read license.txt
var hoverZoomPlugins = hoverZoomPlugins || [];
var hoverZoom = {
options: {},
loadHoverZoom: function() {
var wnd = $(window),
hoverZoomImg = null,
hoverZoomCaption = null,
imgFullSize = null,
imgLoading = null,
imgSrc = '',
imgHost = '',
currentLink = null,
mousePos = {},
loading = false,
loadFullSizeImageTimeout,
actionKeyDown = false,
fullZoomKeyDown = false,
pageActionShown = false,
lastImgWidth = 0,
skipFadeIn = false;
// Calculate optimal image position and size
function posImg(position) {
if (!imgFullSize) {
return;
}
position = position || {top: mousePos.top, left: mousePos.left};
var offset = 20,
padding = 10,
wndWidth = wnd.width(),
wndHeight = wnd.height(),
wndScrollLeft = wnd.scrollLeft(),
wndScrollTop = wnd.scrollTop(),
displayOnRight = (position.left - wndScrollLeft < wndWidth / 2);
function posCaption() {
if (hoverZoomCaption) {
hoverZoomCaption.css('max-width', imgFullSize.width());
if (hoverZoomCaption.height() > 20) {
hoverZoomCaption.css('font-weight', 'normal');
}
while (hoverZoomImg.height() > wndHeight) {
imgFullSize.height(wndHeight - padding - hoverZoomCaption.height()).width('auto');
hoverZoomCaption.css('max-width', imgFullSize.width());
}
}
}
if (displayOnRight) {
position.left += offset;
} else {
position.left -= offset;
}
if (imgLoading) {
position.top -= 10;
if (!displayOnRight) {
position.left -= 25;
}
} else if (fullZoomKeyDown) {
imgFullSize.width(wndWidth - padding).height('auto');
if (hoverZoomImg.height() > wndHeight) {
imgFullSize.height(wndHeight - padding).width('auto');
position.top = wndScrollTop;
position.left = wndScrollLeft + wndWidth / 2 - hoverZoomImg.width() / 2;
} else {
position.top = wndScrollTop + wndHeight / 2 - hoverZoomImg.height() / 2;
position.left = wndScrollLeft;
}
posCaption();
} else {
imgFullSize.width('auto').height('auto');
// Image natural dimensions
var naturalWidth = imgFullSize.width(),
naturalHeight = imgFullSize.height();
if (!naturalWidth || !naturalHeight) {
return;
}
// Width adjustment
if (displayOnRight) {
if (naturalWidth + padding > wndWidth - position.left) {
imgFullSize.width(wndWidth - position.left - padding + wndScrollLeft);
}
} else {
if (naturalWidth + padding > position.left) {
imgFullSize.width(position.left - padding - wndScrollLeft);
}
}
// Height adjustment
if (hoverZoomImg.height() > wndHeight) {
imgFullSize.height(wndHeight - padding).width('auto');
}
posCaption();
position.top -= hoverZoomImg.height() / 2;
// Display image on the left side if the mouse is on the right
if (!displayOnRight) {
position.left -= hoverZoomImg.width() + padding;
}
// Vertical position adjustments
var maxTop = wndScrollTop + wndHeight - hoverZoomImg.height() - padding;
if (position.top > maxTop) {
position.top = maxTop;
}
if (position.top < wndScrollTop) {
position.top = wndScrollTop;
}
}
position = {top: Math.round(position.top), left: Math.round(position.left)};
hoverZoomImg.css(position);
}
function posWhileLoading() {
if (loading) {
posImg();
if (imgLoading && hoverZoomImg.width() > 40) {
imgLoading.remove();
imgLoading = null;
posImg();
setTimeout(function() { skipFadeIn = true; }, 200);
} else {
setTimeout(posWhileLoading, 100);
}
}
}
function hideHoverZoomImg(now) {
if (!now && !imgFullSize || !hoverZoomImg || fullZoomKeyDown) {
return;
}
imgFullSize = null;
if (loading) { now = true; }
hoverZoomImg.stop(true, true).fadeOut(now ? 0 : options.fadeDuration, function() {
hoverZoomCaption = null;
imgLoading = null;
hoverZoomImg.empty();
});
}
function documentMouseMove(event) {
if (!options.extensionEnabled || isExcludedSite()) {
return;
}
// Test if the action key was pressed without moving the mouse
var staticActionKeyPressed = ((options.actionKey || options.fullZoomKey) && event.pageY == undefined);
// If so, the MouseMove event was triggered programmaticaly and we don't have details
// about the mouse position and the event target, so we use the last saved ones.
var links;
if (staticActionKeyPressed) {
links = currentLink;
} else {
mousePos = {top: event.pageY, left: event.pageX};
var target = $(event.target);
links = target.parents('.hoverZoomLink');
if (target.hasClass('hoverZoomLink')) {
links = links.add(target);
}
}
if (links && links.length > 0) {
var hoverZoomSrcIndex = links.data('hoverZoomSrcIndex') || 0;
if (links.data('hoverZoomSrc') && links.data('hoverZoomSrc') != 'undefined' &&
links.data('hoverZoomSrc')[hoverZoomSrcIndex] &&
links.data('hoverZoomSrc')[hoverZoomSrcIndex] != 'undefined') {
// Happens when the mouse goes from an image to another without hovering the page background
if (links.data('hoverZoomSrc')[hoverZoomSrcIndex] != imgSrc) {
hideHoverZoomImg();
}
// Is the image source has not been set yet
if (!imgFullSize) {
currentLink = links;
if (!options.actionKey || actionKeyDown) {
imgSrc = links.data('hoverZoomSrc')[hoverZoomSrcIndex];
imgHost = getHostFromUrl(imgSrc);
clearTimeout(loadFullSizeImageTimeout);
// If the action key has been pressed over an image, no delay is applied
var delay = staticActionKeyPressed ? 0 : options.displayDelay;
loadFullSizeImageTimeout = setTimeout(loadFullSizeImage, delay);
loading = true;
}
} else {
posImg();
}
} else {
return;
}
} else if (currentLink) {
cancelImageLoading();
}
}
function loadFullSizeImage() {
// If no image is currently displayed...
if (!imgFullSize) {
// Full size image container
hoverZoomImg = hoverZoomImg || $('<div id="hoverZoomImg"></div>').appendTo(document.body);
hoverZoomImg.empty();
hoverZoomImg.stop(true, true).fadeIn(options.fadeDuration);
// Loading image container
imgLoading = imgLoading || $('<img class="loading" src="' + chrome.extension.getURL('images/loading.gif') + '"/>');
imgLoading.appendTo(hoverZoomImg);
imgFullSize = $('<img/>').appendTo(hoverZoomImg).load(imgFullSizeOnLoad).error(imgFullSizeOnError).attr('src', imgSrc);
skipFadeIn = false;
var showWhileLoading = imgSrc.substr(-4).toLowerCase() == '.gif';
if (showWhileLoading) {
lastImgWidth = hoverZoomImg.width();
posWhileLoading();
} else {
imgFullSize.addClass('progress');
}
function imgFullSizeOnLoad() {
// Only the last hovered link gets displayed
if (imgSrc == $(imgFullSize).attr('src')) {
loading = false;
hoverZoomImg.stop(true, true);
hoverZoomImg.offset({top:-9000, left:-9000}); // hides the image while making it available for size calculations
hoverZoomImg.empty();
imgLoading = null;
imgFullSize.removeClass('progress').appendTo(hoverZoomImg).mousemove(imgFullSizeOnMouseMove);
if (options.showCaptions && currentLink && currentLink.data('hoverZoomCaption')) {
hoverZoomCaption = $('<div/>', {id: 'hoverZoomCaption', text: currentLink.data('hoverZoomCaption')}).appendTo(hoverZoomImg);
}
if (!skipFadeIn) {
hoverZoomImg.hide().fadeIn(options.fadeDuration);
}
setTimeout(posImg, 10);
if (options.addToHistory && !chrome.extension.inIncognitoTab) {
chrome.extension.sendRequest({action: 'addUrlToHistory', url: imgSrc});
}
chrome.extension.sendRequest({action: 'trackEvent', event: {category: 'Actions', action: 'ImageDisplayedOnSite', label: document.location.host}});
chrome.extension.sendRequest({action: 'trackEvent', event: {category: 'Actions', action: 'ImageDisplayedFromSite', label: imgHost}});
}
}
function imgFullSizeOnError() {
if (imgSrc == $(this).attr('src')) {
var hoverZoomSrcIndex = currentLink ? currentLink.data('hoverZoomSrcIndex') : 0;
if (currentLink && hoverZoomSrcIndex < currentLink.data('hoverZoomSrc').length - 1) {
// If the link has several possible sources, we try to load the next one
imgFullSize.remove();
imgFullSize = null;
hoverZoomSrcIndex++;
currentLink.data('hoverZoomSrcIndex', hoverZoomSrcIndex);
console.info('[HoverZoom] Failed to load image: ' + imgSrc + '\nTrying next one...');
imgSrc = currentLink.data('hoverZoomSrc')[hoverZoomSrcIndex];
setTimeout(loadFullSizeImage, 100);
} else {
hideHoverZoomImg();
console.warn('[HoverZoom] Failed to load image: ' + imgSrc);
chrome.extension.sendRequest({action: 'trackEvent', event: {category: 'Errors', action: 'LoadingErrorFromSite', label: imgHost}});
}
}
}
function imgFullSizeOnMouseMove() {
if (!imgFullSize) {
hideHoverZoomImg(true);
}
}
}
posImg();
}
function cancelImageLoading() {
currentLink = null;
clearTimeout(loadFullSizeImageTimeout);
hideHoverZoomImg();
}
function prepareImgCaption(link) {
var titledElement = null;
if (link.attr('title')) {
titledElement = link;
} else {
titledElement = link.find('[title]:eq(0)');
if (!titledElement.length) {
titledElement = link.parents('[title]:eq(0)');
}
}
if (titledElement && titledElement.length) {
link.data('hoverZoomCaption', titledElement.attr('title'));
titledElement.parents('[title]').andSelf().add('[title]').removeAttr('title');
} else {
var alt = link.attr('alt') || link.find('[alt]').attr('alt');
if (alt && alt.length > 6 && !/^\d+$/.test(alt)) {
link.data('hoverZoomCaption', alt);
}
}
}
// Callback function called by plugins after they finished preparing the links
function imgLinksPrepared(links) {
var showPageAction = false;
links.each(function() {
var _this = $(this);
if (!_this.data('hoverZoomSrc')) {
prepareImgLinksAsync(true);
} else {
// Skip if the image has the same URL as the thumbnail.
// Base64 embedded thumbnails are filtered to avoid a freeze.
try {
if (_this.data('hoverZoomSrc')[0] == this.attr('src') ||
_this.data('hoverZoomSrc')[0].indexOf("'") == -1 &&
_this.find('img[src]').length &&
_this.find('img[src^=data]').length == 0 &&
_this.find('img[src="' + _this.data('hoverZoomSrc')[0] + '"]').length) {
return;
}
} catch(e) {
console.error(e);
}
showPageAction = true;
// If the extension is disabled or the site is excluded, we only need to know
// whether the page action needs to be shown or not.
if (!options.extensionEnabled || isExcludedSite()) {
return;
}
_this.addClass('hoverZoomLink');
// Convert URL special characters
var srcs = _this.data('hoverZoomSrc');
for (var i=0; i<srcs.length; i++) {
srcs[i] = deepUnescape(srcs[i]);
}
_this.data('hoverZoomSrc', srcs);
_this.data('hoverZoomSrcIndex', 0);
// Caption
if (options.showCaptions && !_this.data('hoverZoomCaption')) {
prepareImgCaption(_this);
}
}
});
if (options.pageActionEnabled && !pageActionShown && showPageAction) {
chrome.extension.sendRequest({action : 'showPageAction'});
pageActionShown = true;
}
}
function prepareImgLinks() {
pageActionShown = false;
$('.hoverZoomLink').removeClass('hoverZoomLink').removeData('hoverZoomSrc');
for (var i = 0; i < hoverZoomPlugins.length; i++) {
hoverZoomPlugins[i].prepareImgLinks(imgLinksPrepared);
}
}
var prepareImgLinksDelay = 500, prepareImgLinksTimeout;
function prepareImgLinksAsync(dontResetDelay) {
if (!options.extensionEnabled || isExcludedSite()) {
return;
}
if (!dontResetDelay) {
prepareImgLinksDelay = 500;
}
clearTimeout(prepareImgLinksTimeout);
prepareImgLinksTimeout = setTimeout(prepareImgLinks, prepareImgLinksDelay);
prepareImgLinksDelay *= 2;
}
function deepUnescape(url) {
var ueUrl = unescape(encodeURIComponent(url));
while (url != ueUrl) {
url = ueUrl;
ueUrl = unescape(url);
}
return decodeURIComponent(escape(url));
}
function applyOptions() {
init();
if (!options.extensionEnabled || isExcludedSite()) {
hideHoverZoomImg();
$(document).unbind('mousemove', documentMouseMove);
}
}
var webSiteExcluded = null;
function isExcludedSite() {
// If site exclusion has already been tested
if (webSiteExcluded != null) {
return webSiteExcluded;
}
var siteHost = document.location.href.split('/', 3)[2];
for (var i = 0; i < options.excludedSites.length; i++) {
if (options.excludedSites[i] && options.excludedSites[i].length <= siteHost.length) {
if (siteHost == options.excludedSites[i] || siteHost.substr(siteHost.length - options.excludedSites[i].length - 1) == '.' + options.excludedSites[i]) {
webSiteExcluded = true;
return true;
}
}
}
webSiteExcluded = false;
return false;
}
function loadOptions() {
chrome.extension.sendRequest({action : 'getOptions'}, function(result) {
options = result;
applyOptions();
});
}
function onRequest(request, sender, sendResponse) {
if (request.action == 'optionsChanged') {
options = request.options;
applyOptions();
}
}
function windowOnDOMNodeInserted(event) {
if (event.srcElement) {
var srcElement = $(event.srcElement);
if (srcElement.parents('#hoverZoomImg').length) { return; }
if (event.srcElement.nodeName == 'A' || event.srcElement.nodeName == 'IMG' || srcElement.find('a, img').length || srcElement.parents('a, img').length) {
prepareImgLinksAsync();
} else if (event.srcElement.nodeName == 'EMBED' || event.srcElement.nodeName == 'OBJECT')
fixFlash();
}
}
function bindEvents() {
$(document).mousemove(documentMouseMove).mouseleave(cancelImageLoading);
if (options.actionKey || options.fullZoomKey) {
$(document).keydown(function(event) {
if (event.which == options.actionKey && !actionKeyDown) {
actionKeyDown = true;
$(this).mousemove();
if (loading || imgFullSize) {
return false;
}
}
if (event.which == options.fullZoomKey && !fullZoomKeyDown) {
fullZoomKeyDown = true;
posImg();
if (imgFullSize) {
return false;
}
}
if (imgFullSize && (event.which == options.actionKey || event.which == options.fullZoomKey)) {
return false;
}
}).keyup(function(event) {
if (event.which == options.actionKey) {
actionKeyDown = false;
hideHoverZoomImg();
}
if (event.which == options.fullZoomKey) {
fullZoomKeyDown = false;
$(this).mousemove();
}
});
}
wnd.bind('DOMNodeInserted', windowOnDOMNodeInserted);
wnd.load(prepareImgLinksAsync);
wnd.scroll(hideHoverZoomImg);
}
function fixFlash() {
if ($('.hoverZoomLink').length == 0) { return; }
$('embed:not([wmode]), embed[wmode=window]').each(function() {
var embed = this.cloneNode(true);
embed.setAttribute('wmode', 'opaque');
$(this).replaceWith(embed);
});
$('object').filter(function() {
var param = $(this).children('param[name=wmode]');
return param.length == 0 || param.attr('value').toLowerCase() == 'window';
}).each(function() {
var object = this.cloneNode(true);
$('<param name="wmode" value="opaque">').appendTo(object);
$(this).replaceWith(object);
});
}
function getHostFromUrl(url) {
var host = '';
if (url.indexOf('://') > -1) {
host = url.replace(/.+:\/\/([^\/]*).*/, '$1');
} else {
host = window.location.host;
}
var aHost = host.split('.'),
maxItems = 2;
if (aHost.length > 2) {
var preTld = aHost[aHost.length - 2];
if (preTld == 'co' || preTld == 'com' || preTld == 'net' || preTld == 'org') {
maxItems = 3;
}
}
while (aHost.length > maxItems) {
aHost.shift();
}
return aHost.join('.');
}
function init() {
webSiteExcluded = null;
prepareImgLinks();
bindEvents();
fixFlash();
}
chrome.extension.onRequest.addListener(onRequest);
loadOptions();
},
// Public function to be used by plugins.
// Search for links or images using the 'filter' parameter,
// process their src or href attribute using the 'search' and 'replace' values,
// store the result in the link and add the link to the 'res' array.
urlReplace: function(res, filter, search, replace, parentFilter) {
$(filter).each(function() {
var _this = $(this), link, url;
if (this.src) {
if (!parentFilter) {
parentFilter = 'a:eq(0)';
}
link = _this.parents(parentFilter);
if (!link.length) {
link = _this;
}
} else {
link = _this;
}
if (link.data('hoverZoomSrc')) { return; }
url = hoverZoom.getThumbUrl(this);
if (!url) { return; }
if (Array.isArray(search)) {
for (var i=0; i<search.length; i++) {
url = url.replace(search[i], replace[i]);
}
} else {
url = url.replace(search, replace);
}
link.data('hoverZoomSrc', [url]);
res.push(link);
});
},
// Public function
// Extract a thumbnail url from an element, whether it be a link,
// an image or a element with a background image.
getThumbUrl: function(el) {
if (el.style && el.style.backgroundImage && el.style.backgroundImage.indexOf('url') > -1) {
return el.style.backgroundImage.replace(/.*url\s*\(\s*(.*)\s*\).*/i, '$1');
} else {
return el.src || el.href;
}
}
};
hoverZoom.loadHoverZoom();