-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopup.js
More file actions
384 lines (318 loc) · 9.82 KB
/
Copy pathpopup.js
File metadata and controls
384 lines (318 loc) · 9.82 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
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
var dbg = false;
$(document.body).append($('<div id="fa_popup"></div>'));
var popup_div = $($('#fa_popup')[0])
popup_div.css({'z-index': 2147483647, 'position': 'absolute'}); // LOL
popup_div.hide();
var callbacks = {};
var px = -1;
var py = -1;
var hoveredImgSrc = null;
var hoveredSid = null;
var imgs = $('img');
$.each(imgs, function(_, img) {
// Make sure it's a submission
if (!isThumbnail(img)) {
return;
}
var src = $(img).attr('src');
$(img).hoverIntent(
function(e) {
//
// Apparently hover intent gives you the location
// of mouse /entrance/, not current location...
// So we update the x and y coordinates of the event
// object to the latest known position
//
e.pageX = px;
e.pageY = py;
mouse_move(e);
popup(this, e);
//
// Hack since the loading image will only appear after
// you move your mouse again after hoverIntent triggers
// and I'm too stupid and lazy to figure out why
//
mouse_move(e);
},
function() {
px = py = -1;
hoveredImgSrc = null;
hoveredSid = null;
$(popup_div.children("#popup_img")).hide();
$(popup_div.children("#loading_img")).hide();
$(popup_div.children("#error_img")).hide();
popup_div.hide();
}
);
img.onmousemove = mouse_move;
});
$(document).ready(function() {
$('body').keyup(function (e) {
if (e.which == 83) { // 'S'
if (hoveredImgSrc != null) {
downloadImage(hoveredImgSrc);
}
} else if (e.which == 70) { // 'F'
if (hoveredSid != null) {
doFave(hoveredSid);
}
}
})
});
function isThumbnail(img) {
return $(img).siblings('i[title]').length > 0;
}
function getSubmissionPage(sid_num, callback) {
sub_link = "/view/" + sid_num + "/";
$.ajax(sub_link, {dataType: 'text'})
.success(function (data) {
callback(data);
})
.error(function (data, text, xhr) {
console.error('Submission request failed: ' + text + '\n' + data);
callback(null);
});
}
function getSidNumFromIdAttribute(sid_str) {
if (!sid_str) {
return null;
}
var match = sid_str.match(/\w+(\d+)/);
return match[0];
}
function getFullImageSrc(thumbnail, callback) {
var sid_str = $(thumbnail).parents('*[id*="sid"]').first().attr('id');
var sid = getSidNumFromIdAttribute(sid_str);
if (!sid) {
console.error('Failed to find sid for thumbnail: ' + thumbnail);
return;
}
if (localStorage[sid]) {
callback(sid, localStorage[sid]);
return;
}
if (callbacks[sid]) {
var orig_callback = callbacks[sid];
callbacks[sid] = function (sid, img_src) {
orig_callback(sid, img_src);
callback(sid, img_src);
}
return;
}
callbacks[sid] = callback;
getSubmissionPage(sid, function (data) {
if (!data) {
callbacks[sid](null, chrome.extension.getURL("error.png"));
callbacks[sid] = null;
return;
}
var match = data.match(/var\s+full_url\s*=\s*"([^"]+)"/);
if (match) {
var img_src = match[1];
} else {
console.error('Failed to extract full url for submission.');
return;
}
if (img_src && sid) {
localStorage[sid] = img_src;
callbacks[sid](sid, img_src);
} else {
console.error("Couldn't find submission image (sid: " + sid +") => " + img_src);
callbacks[sid](null, chrome.extension.getURL("error.png"));
callbacks[sid] = null;
}
})
}
function getSubmissionTitle(sid) {
var title = $("#" + sid + " span").attr('title');
if (title) {
return '"' + title + '"';
}
return "a murry masterpiece"
}
function doFave(sid) {
var submission_title = getSubmissionTitle(sid);
var pnotify = $.pnotify({
title: 'Faving Image...',
text: 'Faving ' + submission_title,
type: 'info'
});
getSubmissionPage(sid, function(data) {
if (!data) {
onFaveError(pnotify, submission_title);
return;
}
var has_faved_match = data.match(/>\s*-Remove from Favorites\s*</);
if (has_faved_match) {
// Already faved
onFaveSuccess(pnotify, submission_title);
return;
}
var fave_url_match = data.match(/a\s+href\s*=\s*"(\/fav\/[^"]+)"/);
if (!fave_url_match) {
onFaveError(pnotify, submission_title);
return;
}
$.ajax(fave_url_match[1])
.success(function (data) {
onFaveSuccess(pnotify, submission_title);
})
.error(function (data, text, xhr) {
console.error('Fave request failed: ' + text + '\n' + data);
});
});
}
function onFaveError(pnotify, submission_title) {
pnotify.pnotify({
title: 'Fave Error',
text: 'There was an error faving ' + submission_title +' :(',
type: 'error'
});
pnotify.pnotify_queue_remove();
}
function getFaveSuccessMessage(title) {
var choice = Math.random();
var lower_title = title.toLowerCase();
if (lower_title.indexOf("red") >= 0 && lower_title.indexOf("panda") >= 0) {
return "Yay red pandas! :D"
}
if (choice < .01) {
return "Faved... but would you want your mother to know you're into " + title + "?"
}
if (choice < .1) {
return "Ah, we see " + title + " pleases your plums!"
}
if (choice < .25) {
return "We've noted that " + title + " tickles your fancy!";
}
if (choice < .5) {
return title + " is the bees knees!";
}
if (choice < .75) {
return "Gotcha, " + title + " floats your boat!";
}
return 'Successfully faved ' + title;
}
function onFaveSuccess(pnotify, submission_title) {
pnotify.pnotify({
title: 'Image Faved!',
text: getFaveSuccessMessage(submission_title),
type: 'success'
});
pnotify.pnotify_queue_remove();
}
function id(o) {
return o;
}
function popup(img, e) {
px = e.pageX;
py = e.pageY;
var popup_img, loading_img;
popup_div.height(1).width(1)
.empty()
.append(loading_img = $('<img id="loading_img" />'))
.show();
// Give the image 100ms to load before showing the loading gif since it can
// be a bit jarring to have show and immediately disappear
setTimeout(function () {
if (popup_div.children("#loading_img").length > 0) {
loading_img
.attr('src', chrome.extension.getURL("loading.gif"))
.data('width', loading_img.attr('width'))
.data('height', loading_img.attr('height'));
}
}, 100);
popup_img = $('<img id="popup_img"></img>')
maybeShowFaveFeatureNotif();
getFullImageSrc(img, function (sid, img_src) {
popup_div.append(
popup_img.attr({src: img_src})
.load(function() {
hoveredImgSrc = img_src;
hoveredSid = sid;
popup_img.data('width', popup_img.width()).data('height', popup_img.height());
popup_div.children("#loading_img").remove();
move_image($(this), px, py);
$(this).show("fade", {}, 300);
})
.error(function() {
$(popup_div.children("#loading_img")[0]).remove();
popup_div.append($('<img id="error_img" src="'
+ chrome.extension.getURL("error.png") + '"/>'));
})
.hide()
)
});
}
function mouse_move(e) {
px = e.pageX;
py = e.pageY;
// Check if we're still loading
if (popup_div.children('#loading_img').length > 0) {
popup_div.offset({left: e.pageX - 63, top: e.pageY - 28});
} else {
move_image(popup_div.children("#popup_img").first(), e.pageX, e.pageY);
}
}
function move_image(img, pageX, pageY)
{
var vp = viewport();
var owidth = img.data('width') || 0;
var oheight = img.data('height') || 0;
var show_left = (pageX > vp.x + (vp.cx / 2));
var max_height = vp.cy - 55;
if (oheight && oheight != 0) {
max_height = Math.min(max_height, oheight);
}
var max_width = show_left ? pageX - vp.x - 45 : vp.cx - (pageX - vp.x) - 45;
if (owidth && owidth != 0) {
max_width = Math.min(max_width, owidth);
}
var ratio = Math.min( max_width / (1.0 * owidth), max_height / (1.0 * oheight));
var width = ratio * owidth;
var height = ratio * oheight;
var x, y;
if (pageY + 75 - oheight > vp.y + 25) {
y = pageY + 75 - height;
} else {
y = vp.y + 25;
}
if (show_left) {
x = pageX - 20 - width;
} else {
x = pageX + 20;
}
dbg && console.log("x: " + x + ", y: " + y + ", width: " + width + ", height: " + height + ", max_width: " + max_width + ", max_height: " + max_height + ", owidth: " + owidth + ",oheight: " + oheight);
popup_div.offset({left: x, top: y});
img.css({"max-height": max_height, "max-width": max_width});
img.height(height).width(width);
popup_div.height(height).width(width);
dbg && popup_div.css("border", "3px solid red");
}
function viewport() {
return {
x: $(window).scrollLeft(),
y: $(window).scrollTop(),
cx: $(window).width(),
cy: $(window).height()
};
}
function maybeShowFaveFeatureNotif() {
if (!localStorage["faveFeatureNotifShown"]) {
$.pnotify({
title: 'New FA Previewer Feature',
text: 'Press the \'F\' key while hovering over an image to fave it. Murrtastic!<br/><br/><3 Sero',
type: 'info'
});
localStorage["faveFeatureNotifShown"] = true;
}
}
function downloadImage(image_src) {
var a = document.createElement('a');
a.href = image_src;
a.download = image_src.split('/').pop().split('?')[0]; // Prettify the download name
if (!a.download) { a.download = 'fa' + Date.now() + '.jpg'; }
var clickEvent = document.createEvent('MouseEvent');
clickEvent.initEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(clickEvent);
}