-
Notifications
You must be signed in to change notification settings - Fork 53
/
cloudsave.js
434 lines (386 loc) · 9.79 KB
/
cloudsave.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
var Hosts = {};
var root = chrome.contextMenus.create({
"title" : "Cloud Save",
"contexts" : ["all"] //["page", "image", "link", "selection"]
});
var save_as = chrome.contextMenus.create({
"title": "Save As...",
"contexts": ["all"],
"parentId": root
});
chrome.contextMenus.create({
"type": "separator",
"contexts": ["all"],
"parentId": root
});
//todo: add more
var original = {
"image": {
picasa: 'Picasa',
twitpic: 'TwitPic',
flickr: 'Flickr',
posterous: 'Posterous',
twitrpix: 'Twitrpix',
twitgoo: 'Twitgoo',
facebook: 'Facebook',
imgly: 'Imgly'
},
"all": {
cx: 'Cx',
box: 'Box.com',
sugarsync: 'SugarSync',
dropbox: 'Dropbox',
gdocs: 'Google Docs/Drive',
minus: 'Minus',
cloudapp: 'CloudApp',
clouddrive: 'Amazon Cloud',
droplr: 'Droplr',
skydrive: 'SkyDrive',
webdav: 'WebDAV'
},
"link": {
dropdo: 'Dropdo' //this one is peculiar because it works differently from all the other hosts
}
};
var additional = {
"image": {
imageshack: 'Imageshack',
imgur: 'Imgur',
immio: 'Imm.io'
},
"all": {
hotfile: 'Hotfile'
}
};
var classes = clone(original);
/*
function clone(obj){ //very shallow cloning
var n = {};
for(var i in obj) n[i] = obj[i]; //we are the knights who say ni!
return n;
}
function clone_r(obj){ //not so shallow cloning
if(typeof obj != 'object') return obj;
var n = {};
for(var i in obj) n[i] = clone(obj[i]); //we are the knights who say ni!
return n;
}
*/
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;
var temp = obj.constructor(); // changed
for(var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
//an order which shoudl theoretically work, but isnt optimal
//in any stretch of the imagination
/*
general idea:
1. quantity (2 > 1)
2. position (end > beginning)
*/
try {
recent = JSON.parse(localStorage.cloudsave_recent);
}catch(err){
recent = [
'gdocs',
'facebook',
'dropbox',
'flickr',
'box',
'clouddrive',
'picasa',
'gdocs',
'dropbox'
];
}
function handle_click(info, tab){
console.log(arguments);
var url = info.srcUrl || info.linkUrl || info.pageUrl;
console.log('Source URL', url);
var name = unescape(decodeURIComponent(
unescape(unescape(unescape(url)))
.replace(/\s/g, '+')
.replace(/^.*\/|\?.*$|\#.*$|\&.*$/g,'') ||
url.replace(/.*\/\/|www./g,'')
.replace(/[^\w]+/g,'_')
.replace(/^_*|_*$/g,''))
).replace(/\+/g, ' ');
console.log('Processed name', name);
if(info.selectionText){
url = 'data:text/plain,'+encodeURIComponent(info.selectionText);
}
var host = menu_ids[info.menuItemId];
if(Hosts[host]){
if(host == 'dropdo'){
chrome.tabs.create({
url: 'https://dropdo.com/upload?link='+url
})
}else{
if(host == 'dropbox' && localStorage.folder_prefix){
name = localStorage.folder_prefix + name;
}
if(info.parentMenuItemId == save_as){
//woot save as stuff
console.log('save as');
name = prompt('Save file as...', name);
if(!name) return;
};
if(name.indexOf('/') != -1){
localStorage.folder_prefix = name.replace(/[^\/]+$/,'');
}
upload(host, url, name);
}
console.log(host, url, name);
recent.push(host);
recent.shift();
localStorage.cloudsave_recent = JSON.stringify(recent);
updateMenus();
}else{
alert("Could not find host "+host);
}
}
var title_map = {};
var menu_ids = {};
function updateMenus(){
title_map = {};
for(var i in classes){
for(var h in classes[i]){
title_map[h] = classes[i][h]; //flatten it out
}
}
Object.keys(menu_ids).reverse().forEach(function(item){
chrome.contextMenus.remove(parseInt(item));
});
menu_ids = {};
for(var unique = [], freqmap = {}, i = 0; i < recent.length;i++){
if(title_map[recent[i]]){
if(!freqmap[recent[i]]){
freqmap[recent[i]] = 1;
unique.push(recent[i]);
}
freqmap[recent[i]]++;
}
}
var dilation_factor = 100;
function grade(result){
return freqmap[result] + recent.lastIndexOf(result) / dilation_factor;
}
var sorted = unique.sort(function(a,b){
return grade(b) - grade(a);
});
console.log(recent);
console.log(unique.map(function(a){
return a + ' ' + grade(a)
}))
for(var i = 0; i < sorted.length; i++){
var prop = {
"title": title_map[sorted[i]],
"onclick": handle_click
};
prop.contexts = classes.image[sorted[i]] ?
['image'] :
(classes.link[sorted[i]]?
['image', 'link', 'selection']: ['all'/*'page', 'link', 'image', 'selection'*/]);
prop.parentId = root;
menu_ids[chrome.contextMenus.create(clone(prop))] = sorted[i];
prop.parentId = save_as;
menu_ids[chrome.contextMenus.create(clone(prop))] = sorted[i];
}
var others = Object.keys(title_map).sort().filter(function(x){
return unique.indexOf(x) == -1;
});
/*
menu_ids[chrome.contextMenus.create({
"type": "separator",
"contexts": ["all"],
"parentId": root
})] = 42;
menu_ids[chrome.contextMenus.create({
"type": "separator",
"contexts": ["all"],
"parentId": save_as
})] = 42;
//*/
var save_as_more = chrome.contextMenus.create({
"title": "More",
"parentId": save_as,
"contexts": ["all"]
});
menu_ids[save_as_more] = 'save_as_more';
var root_more = chrome.contextMenus.create({
"title": "More",
"parentId": root,
"contexts": ["all"]
});
menu_ids[root_more] = 'root_more';
function add_more(host){
var prop = {
"title": title_map[host],
"onclick": handle_click
};
prop.contexts = classes.image[host] ?
['image'] :
(classes.link[host]?
['image', 'link']: ['all'/*'page', 'link', 'image'*/]);
prop.parentId = root_more;
menu_ids[chrome.contextMenus.create(clone(prop))] = host;
prop.parentId = save_as_more;
menu_ids[chrome.contextMenus.create(clone(prop))] = host;
}
for(var i = 0; i < others.length; i++){
if(classes.image[others[i]]){
add_more(others[i])
}
}
menu_ids[chrome.contextMenus.create({
"type": "separator",
"contexts": ["image"],
"parentId": root_more
})] = 42;
menu_ids[chrome.contextMenus.create({
"type": "separator",
"contexts": ["image"],
"parentId": save_as_more
})] = 42;
for(var i = 0; i < others.length; i++){
if(!classes.image[others[i]]){
add_more(others[i])
}
}
//*
menu_ids[chrome.contextMenus.create({
"type": "separator",
"contexts": ["all"],
"parentId": root_more
})] = 42;
menu_ids[chrome.contextMenus.create({
"title": "Add/Remove",
"contexts": ["all"],
"parentId": root_more,
"onclick": open_settings
})] = 'add_remove'; //*/
}
function open_settings(){
chrome.tabs.create({url: "settings.html"})
}
//shamelessly stolen from john resig.
function wbr(str, num) {
return str.replace(RegExp("(\\w{" + num + "})(\\w)", "g"), function(all,text,char){
return text + "<wbr>" + char;
});
}
var INDETERMINATE = {};
function updateNotification(id,opt){
chrome.notifications.update(id, opt,function(wasUpdated){
if(!wasUpdated){
console.log('Error! Could not locate notification', id,opt.title, opt.message);
}
});
}
var urlid = {
'todo_fix_this': 42
//this is a sort of hack. it uses the file download urls
//as a sort of state callback whatnot stuff.
};
function uploadProgress(url, evt){
var pr=Math.round(evt.loaded * 100 / evt.total);
var upload={
type: 'progress',
title:'Upload',
message:'Uploading...',
iconUrl:'icon/48.png',
progress:pr
}
updateNotification(urlid[url],upload);
}
function downloadProgress(url, evt){
var pr=Math.round(evt.loaded * 100 / evt.total);
var download={
type: 'progress',
title:'Download',
message:'Downloading...',
iconUrl:'icon/48.png',
progress:pr
}
updateNotification(urlid[url],download);
}
function upload(host, url, name){
var id = Math.random().toString(36).substr(3);
var opt = {
type: "basic",
title: "Saving",
message: 'The file is being saved to '+title_map[host],
iconUrl: "icon/throbber.gif"
}
chrome.notifications.create(id, opt,function(nid){
console.log("id="+nid);
})
var has_uploaded = false;
var upload_callback = function(){};
chrome.notifications.onClicked.addListener(function(notificationId){
if(has_uploaded){
openFile();
clearnotification(notificationId);
}else{
upload_callback = openFile;
}
})
chrome.notifications.onClosed.addListener(function(notificationId,byUser){
delete urlid[url];
})
function clearnotification(id){
chrome.notifications.clear(id, function(wasCleared){
if(wasCleared!=true)
console.log('clear notification failed');
})
}
function openFile(){
chrome.tabs.create({url: has_uploaded})
}
urlid[url] = id;
Hosts[host]({
url: url,
name: name
}, function(e){
has_uploaded = e && e.url;
var ntitle,nmsg,nicon;
setTimeout(upload_callback, 200);
console.log('uploaded file yay', e);
if(e && typeof e == "string" && e.indexOf('error:') != -1){
ntitle='Error';
nmsg="Could not be uploaded to "+title_map[host];
console.log('e.error'+e.substr(6));
nicon='icon/64sad.png';
}else{
ntitle="Success";
nmsg="The file has been uploaded to "+title_map[host]+", click me to view.";
nicon="icon/64.png";
}
var opt = {
type: 'basic',
title: ntitle,
message: nmsg,
iconUrl: nicon
};
updateNotification(id, opt);
})
}
function install_additional(state){
if(state){
for(var i in additional){
for(var ii in additional[i])
classes[i][ii] = additional[i][ii];
}
}else{
classes = clone(original);
}
updateMenus();
}
if(localStorage.additional == 'yes'){
install_additional(true);
}else{
updateMenus();
}