forked from antimatter15/cloudsave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudsave.js
411 lines (370 loc) · 9.99 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
var Hosts = {};
var root = chrome.contextMenus.create({
"title" : "Cloud Save",
"contexts" : ["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": {
box: 'Box.net',
sugarsync: 'SugarSync',
dropbox: 'Dropbox',
gdocs: 'Google Docs',
minus: 'Min.us',
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_r(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;
}
//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: 'http://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']: ['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']: ['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, arg1, arg2){
function main(){
var wins = chrome.extension.getViews({type:"notification"})
var matches = wins.filter(function(win) {
return win.location.search.substr(1) == id
});
if(id == 42) matches = wins; //please coding gods dont kill me
if(matches.length){
if(typeof arg1 == 'number' || arg1 == INDETERMINATE){
matches[0].document.getElementById('progress').style.display = '';
matches[0].document.getElementById('progress').value = arg1 == INDETERMINATE ? null : arg1;
}else if(arg2){
matches[0].document.getElementById('status').innerHTML = arg2;
matches[0].document.body.style.backgroundImage = 'url('+arg1+')';
matches[0].document.getElementById('progress').style.display = 'none'
}else{
matches[0].document.getElementById('status').innerHTML = arg1;
}
}else{
return false
}
return true
}
if(!main()){
console.log('Error! Could not locate notification', id, arg1, arg2);
var count = 0;
function looper(){
if(!main() && count++ < 100) setTimeout(looper, 10);
}
looper();
}
}
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, event){
updateNotification(urlid[url], event.loaded/event.total/2 + 0.5);
}
function downloadProgress(url, event){
updateNotification(urlid[url], event.loaded/event.total/2);
}
function upload(host, url, name){
var id = Math.random().toString(36).substr(3);
var notification = webkitNotifications.createHTMLNotification('popup.html?'+id);
notification.ondisplay = function(){
updateNotification(id, 'icon/throbber.gif',
"The file '"+wbr(name,8)+"' is being saved to "+title_map[host]+"...");
updateNotification(id, INDETERMINATE);
}
var has_uploaded = false;
var upload_callback = function(){};
notification.onclick = function(){
if(has_uploaded){
openFile()
}else{
updateNotification(id, "Opening file '"+wbr(name,8)+"' on "+title_map[host] +" in a few seconds...");
upload_callback = openFile;
}
}
notification.onclose = function(){
delete urlid[url];
}
function openFile(){
chrome.tabs.create({url: has_uploaded})
}
notification.show();
urlid[url] = id;
Hosts[host]({
url: url,
name: name
}, function(e){
has_uploaded = e && e.url;
setTimeout(upload_callback, 200);
console.log('uploaded file yay', e);
if(e && typeof e == "string" && e.indexOf('error:') != -1){
updateNotification(id, 'icon/64sad.png',
"The file '"+wbr(name,8)+"' could not be uploaded to "+
title_map[host]+". "+e.substr(6));
}else{
updateNotification(id, 'icon/64.png',
"The file '"+wbr(name,8)+"' has been uploaded to "+title_map[host]+"."
);
setTimeout(function(){
notification.cancel();
}, 5.4 * 1000) //May the fourth be with you.
}
})
}
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_r(original);
}
updateMenus();
}
if(localStorage.additional == 'yes'){
install_additional(true);
}else{
updateMenus();
}