-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpopup_script.js
More file actions
305 lines (242 loc) · 6.18 KB
/
Copy pathpopup_script.js
File metadata and controls
305 lines (242 loc) · 6.18 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
(function() {
"use strict";
var apiUrl = "https://api.flickr.com/services/rest/?method=";
var apiKeyParam = "&api_key=07e391a86e66960a7449ea81d0d9cd48";
var perPage = "&per_page=30";
var page = 1;
var loadingCount = 0;
// Get all picture sizes and create thumbnail
function getPicture(id)
{
getPhotoSizes(id, function(id, thumbUrl, bigUrl) {
getPhotoTitle(id, bigUrl, function(id, title) {
renderThumb(id, thumbUrl, bigUrl, title);
stopLoading();
});
});
$("#actions, #imgContainer, #disclaimer").show();
startLoading();
}
function getPhotoTitle(id, url, callback) {
$.ajax({
url: apiUrl + "flickr.photos.getInfo&photo_id=" + id + apiKeyParam,
success: function(xml) {
var title = $(xml).find("title");
if( title != null && title.text() != "" ) {
var urlParts = url.split(".");
title = filenameSanitize((title.text() || "Untitled").trim(), "_") + "-" + id + "." + urlParts[urlParts.length - 1];
title = title.split("?")[0];
} else {
title = undefined;
}
callback(id, title);
},
error: function() {
callback(id, undefined);
}
});
}
function getPhotoSizes(id, callback) {
$.ajax({
url: apiUrl + "flickr.photos.getSizes&photo_id=" + id + apiKeyParam,
dataType: "xml",
success: function(xml) {
var canDownload = $(xml).find("sizes").first().attr("candownload");
if (canDownload == 0) {
$("#loading").hide();
displayMessage("Downloads are disabled for this photo");
return;
}
var size = $(xml).find("size");
var thumbUrl = size.first().attr("source");
var bigUrl = size.last().attr("source");
if( thumbUrl && bigUrl ) {
callback(id, thumbUrl, bigUrl);
}
},
error: function() {
stopLoading();
}
});
}
function renderThumb(id, thumbUrl, bigUrl, title) {
var img = $("<img/>").attr("src", thumbUrl);
var link = $("<a/>")
.on("click", function(e) {
// Used to, maybe, replace the URL when clicking with the download URL to allow changing with/without "_d"
link.attr( "href", getPhotoURL(bigUrl) );
// Let's ignore default behaviour and use our code now
openAllPhotos([{ url: getPhotoURL(bigUrl), filename: title }]);
e.preventDefault();
})
.attr({
"href" : bigUrl,
"title" : title,
"download" : title,
"target" : "_blank"
})
.append( img )
.appendTo( $("#imgContainer") );
}
// Get all pictures inside some Photoset
function getPhotoset(id)
{
$(".loadMore").remove();
$.ajax({
url: apiUrl + "flickr.photosets.getPhotos&media=photos&photoset_id=" + id + perPage + "&page=" + page + apiKeyParam,
dataType: "xml",
success: function(xml) {
$(xml).find("photo").each(function(){
getPicture( $(this).attr("id") );
});
page++;
$("#setOk").html("+30");
}
});
}
// Show loader
function startLoading() {
loadingCount++;
$("#loading").show();
}
// Hide loader
function stopLoading() {
loadingCount--;
if( loadingCount < 1 ) {
$("#loading").hide();
showLoadMore();
}
}
// Show button to load more photos
function showLoadMore() {
if( page > 1 ) {
$("<a/>")
.text("Load 30 more...")
.attr("href", "#")
.on("click", function() {
$("#setOk").click();
})
.addClass("loadMore")
.appendTo( $("#imgContainer") );
}
}
// display a message that can be hidden
function displayMessage(msg) {
$("<a/>")
.text(msg)
.attr("href", "#")
.on("click", function() {
$(this).remove();
})
.addClass("loadMore")
.appendTo( $("#imgContainer") );
}
// Check if Auto Download is active
function shouldAutoDownload() {
return $("#download").is(":checked");
}
// Get the URL to open or download the big photo
function getPhotoURL(url)
{
if( shouldAutoDownload() )
{
var urlArr = url.split(".");
urlArr[urlArr.length - 2] += "_d";
return urlArr.join(".");
}
return url;
}
function openAllPhotos(imgs) {
chrome.extension.sendRequest({
msg: "openAllPhotos",
imgs: imgs,
autoDownload: shouldAutoDownload()
});
}
// Proper filename sanitizer, borrowed from: https://github.com/parshap/node-sanitize-filename
var illegalRe = /[\/\?<>\\:\*\|":]/g;
var controlRe = /[\x00-\x1f\x80-\x9f]/g;
var reservedRe = /^\.+$/;
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
var multipleSpaceRe = /\s+/g;
function filenameSanitize(input, replacement) {
return input
.replace(illegalRe, replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)
.replace(windowsReservedRe, replacement)
.replace(multipleSpaceRe, " ");
}
// Button handlers
$(document).ready(function()
{
// Photo "Load" button
$("#photoOk").on("click", function()
{
if( $("#photoId").attr("value") )
{
getPicture( $("#photoId").attr("value") );
}
});
// Set "Load" button
$("#setOk").on("click", function()
{
if( $("#setId").attr("value") )
{
getPhotoset( $("#setId").attr("value") );
}
});
// Reset the page and the Set button
$("#setId").on("keydown", function() {
page = 1;
$("#setOk").html("Load");
});
// Open all photos
$("#openAll").on("click", function()
{
var imgs = [];
$("#imgContainer img").each(function() {
var img = $(this).parent();
imgs.push({
url: getPhotoURL( img.attr("href") ),
filename: img.attr("title")
});
});
if( imgs.length > 0 ) {
openAllPhotos(imgs);
window.close();
}
});
// Clear photos
$("#clear").on("click", function()
{
page = 1;
window.close();
// $("#imgContainer").empty();
// $("#setOk").html("Load");
});
// Change labels
$("#open, #download").on("change", function() {
$("#openAll").text( shouldAutoDownload() ? "Download all" : "Open all" )
})
});
// Get the ID from the URL, when possible
chrome.tabs.getSelected(null, function(tab) {
if( tab.url.indexOf("flickr.com\/photos") > -1 )
{
var urlParts = tab.url.split("/");
// Set
if( urlParts[5] === "sets")
{
$("#setId").attr("value", urlParts[6]);
$("#setOk").click();
}
// Photo
else if( urlParts.length >= 6 )
{
$("#photoId").attr("value", urlParts[5]);
$("#photoOk").click();
}
}
});
})();