-
Notifications
You must be signed in to change notification settings - Fork 69
/
popup.html
436 lines (400 loc) · 15.6 KB
/
popup.html
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script src="./lib/jquery-1.4.2.min.js"></script>
<script src="./lib/jquery-ui-1.8.5.custom.min.js"></script>
<script src="./lib/jquery.innerfade.js"></script>
<script>
$(function () {
// Protect against XSRF attacks
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader('X-XSRF-Protection', 'true');
}
})
var getWindowName = function (name) {
var names = JSON.parse(localStorage.tempWindowNames)
return names[name];
};
var setWindowName = function (name, value) {
if (value.length == 0) {
value = 'Window';
}
var names = JSON.parse(localStorage.tempWindowNames)
names[name] = value;
localStorage.tempWindowNames = JSON.stringify(names);
};
var makeSortable = function () {
$(".tabs").sortable({
placeholder: 'tabplaceholder',
forcePlaceholderSize: true,
revert: 100,
connectWith: '.tabs',
scroll: false,
update: function (e, ui) {
var tab = $($(ui.item[0]).children()[0]).attr('id');
var oldWindow = $($(ui.item[0]).children()[0]).attr('windowid');
if ($(e.target).attr('id') === 'trash') {
// Dragged to trash
if (tab.substring(6,7) == 'l') {
chrome.tabs.remove(parseInt(tab.substring(7),10));
} else if (tab.substring(6,7) == 'r') {
// Remove from remote
// Remove from array
var data = TCWindows[oldWindow.substring(4)];
data.tabs.splice(tab.substring(7),1);
data.name = getWindowName(oldWindow);
// Send new array
$.post('https://chrometabcloud.appspot.com/update', {window: JSON.stringify(data), windowId: oldWindow.substring(4)});
}
$(ui.item[0]).detach();
} else {
// Dragged between windows
// Assume both local
var newWindow = $(e.target).parent().attr('id');
if (tab.substring(6,7) == 'l' && newWindow.substring(3,4) == 'l') {
// Local to local
// Move tab
chrome.tabs.move(parseInt(tab.substring(7),10), {
windowId: parseInt(newWindow.substring(4),10),
index: $(ui.item[0]).index()
});
// Update image
$($(ui.item[0]).children()[0]).attr('windowid', newWindow);
} else if (tab.substring(6,7) == 'l' && newWindow.substring(3,4) == 'r') {
// Local to remote
// Add to remote
// Add to array
var data = TCWindows[newWindow.substring(4)];
data.tabs.splice($(ui.item[0]).index(),0,{
url: $($(ui.item[0]).children()[0]).attr('url'),
title: $($(ui.item[0]).children()[0]).attr('title'),
favicon: ($($(ui.item[0]).children()[0]).attr('src') != 'chrome://favicon/'+$($(ui.item[0]).children()[0]).attr('url')) ? $($(ui.item[0]).children()[0]).attr('src') : ''
});
data.name = getWindowName(newWindow);
// Send new array
$.post('https://chrometabcloud.appspot.com/update', {window: JSON.stringify(data), windowId: newWindow.substring(4)});
// Remove from local
chrome.tabs.remove(parseInt(tab.substring(7),10));
// Update image
$($(ui.item[0]).children()[0]).removeClass('tabimglocal').attr('id','tabimgr'+$(ui.item[0]).index()).attr('windowid', newWindow);
$.each($(ui.item[0]).parent().children(), function (i, e) {
$($(e).children()[0]).attr('id','tabimgr'+i);
});
} else if (tab.substring(6,7) == 'r' && newWindow.substring(3,4) == 'l') {
// Remote to local
// Add to local
chrome.tabs.create({
windowId: parseInt(newWindow.substring(4),10),
url: $($(ui.item[0]).children()[0]).attr('url'),
index: $(ui.item[0]).index(),
selected: false
}, function (newTab) {
// Update image
$($(ui.item[0]).children()[0]).addClass('tabimglocal').attr('id','tabimgl'+newTab.id);
});
// Remove from remote
// Remove from array
var data = TCWindows[oldWindow.substring(4)];
data.tabs.splice(tab.substring(7),1);
data.name = getWindowName(oldWindow);
// Send new array
$.post('https://chrometabcloud.appspot.com/update', {window: JSON.stringify(data), windowId: oldWindow.substring(4)});
// Update image
$($(ui.item[0]).children()[0]).attr('windowid', newWindow);
// Update remote ids
$.each($('#'+oldWindow).find('.tabs').children(), function (i, e) {
$($(e).children()[0]).attr('id','tabimgr'+i);
});
} else if (tab.substring(6,7) == 'r' && newWindow.substring(3,4) == 'r') {
// Remote to remote
if (oldWindow === newWindow) {
// Move within window
// TODO
} else {
// Add to first window
// Add to array
var data = TCWindows[newWindow.substring(4)];
data.tabs.splice($(ui.item[0]).index(),0,{
url: $($(ui.item[0]).children()[0]).attr('url'),
title: $($(ui.item[0]).children()[0]).attr('title'),
favicon: ($($(ui.item[0]).children()[0]).attr('src') != 'chrome://favicon/'+$($(ui.item[0]).children()[0]).attr('url')) ? $($(ui.item[0]).children()[0]).attr('src') : ''
});
data.name = getWindowName(newWindow);
// Send new array
$.post('https://chrometabcloud.appspot.com/update', {window: JSON.stringify(data), windowId: newWindow.substring(4)});
// Remove from 2nd
// Remove from array
var data = TCWindows[oldWindow.substring(4)];
data.tabs.splice(tab.substring(7),1);
data.name = getWindowName(oldWindow);
// Send new array
$.post('https://chrometabcloud.appspot.com/update', {window: JSON.stringify(data), windowId: oldWindow.substring(4)});
// Update image
$($(ui.item[0]).children()[0]).attr('windowid', newWindow);
// Update image
$($(ui.item[0]).children()[0]).attr('windowid', newWindow);
$.each($('#'+oldWindow).find('.tabs').children(), function (i, e) {
$($(e).children()[0]).attr('id','tabimgr'+i);
});
$.each($(ui.item[0]).parent().children(), function (i, e) {
$($(e).children()[0]).attr('id','tabimgr'+i);
});
}
}
$('.tabslocal').each(function (e) {
if ($(this).children().length === 0) {
$(this).parent().detach();
}
});
}
}
});
};
chrome.windows.getAll({populate: true}, function (windows) {
windows.forEach(function (curWindow) {
if (getWindowName('winl'+curWindow.id) === undefined)
setWindowName('winl'+curWindow.id,'Click to name');
var winString = '<fieldset class="window" id="winl'+curWindow.id+'"><legend class="windowname">'+getWindowName('winl'+curWindow.id)+'</legend><span class="right"><img class="windowsave" src="images/disk.png" title="Save window" /></span><div class="tabs tabslocal">';
curWindow.tabs.forEach(function (curTab) {
if (curTab.pinned !== undefined) {
localStorage.supportPinned = 1;
}
var favicon = (curTab.favIconUrl != '' && curTab.favIconUrl !== undefined) ? curTab.favIconUrl : 'chrome://favicon/'+curTab.url;
winString += '<div style="float: left"><img id="tabimgl'+curTab.id+'" windowid="winl'+curWindow.id+'" class="tabimg tabimglocal" url="'+curTab.url+'" src="'+favicon+'" title="'+curTab.title.replace(/\"/g,"'")+'" /></div>';
});
winString += '</div></fieldset>';
$('#current').append(winString);
});
makeSortable();
});
$('.tabimg').live('mouseup', function (e) {
if (e.button === 1 || (e.button === 0 && e.ctrlKey === true)) {
chrome.tabs.create({
url: $(this).attr('url'),
selected: false
});
}
});
// Local options
$('.tabimglocal').live('click', function (e) {
var tabId = $(this).attr('id').substring(7);
chrome.tabs.update(parseInt(tabId,10), {selected: true});
});
$('.windowname').live('click', function (e) {
var windowId = $(this).parent().attr('id');
$(this).html('<input type="text" class="windowinput" value="'+getWindowName(windowId)+'" />').removeClass('windowname');
$(this).find('input').focus();
});
$('.windowinput').live('blur', function (e) {
var windowId = $(this).parent().parent().attr('id');
setWindowName(windowId, $(this).val());
$(this).parent().text(getWindowName(windowId)).addClass('windowname');
// Remote windows
if (windowId.substring(3,4) == 'r') {
var data = TCWindows[windowId.substring(4)];
data.name = getWindowName(windowId);
$.post('https://chrometabcloud.appspot.com/update', {window: JSON.stringify(data), windowId: windowId.substring(4)});
}
});
$('.windowinput').live('keypress', function (e) {
if (e.which === 13) {
$(this).blur();
} else {
return true;
}
})
$('.windowsave').live('click', function (e) {
var windowId = parseInt($(this).parent().parent().attr('id').substring(4),10);
var img = this;
$(img).attr('src','images/arrow_refresh.png').removeClass('windowsave');
chrome.tabs.getAllInWindow(windowId, function (tabs) {
var data = {};
if (getWindowName('winl'+windowId) == 'Click to name') {
var date = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
data.name = months[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear()+' - '+date.toLocaleTimeString();
} else {
data.name = getWindowName('winl'+windowId);
}
data.tabs = [];
tabs.forEach(function (tab) {
data.tabs.push({
url: tab.url,
title: tab.title,
favicon: (tab.favIconUrl != '' && tab.favIconUrl !== undefined) ? tab.favIconUrl : '',
pinned: (tab.pinned) ? true : false
});
});
$.post('https://chrometabcloud.appspot.com/add', {window: JSON.stringify(data)}, function () {
$(img).attr('src','images/accept.png');
updateTabs();
});
});
});
$('.windowopen').live('click', function (e) {
var windowId = parseInt($(this).parent().parent().attr('id').substring(4),10);
chrome.extension.sendRequest({tabs: TCWindows[windowId].tabs});
});
$('.windowdelete').live('click', function (e) {
if (localStorage.deleteconfirm === 'yes') {
$(this).parent().html('<span>Confirm: <img style="vertical-align: middle" class="windowreallydelete" src="images/delete.png" /></span>');
} else {
var windowId = parseInt($(this).parent().parent().attr('id').substring(4),10);
$(this).attr('src','images/arrow_refresh.png');
var self = this;
$.post('https://chrometabcloud.appspot.com/remove', {window: windowId}, function () {
updateTabs();
});
}
});
$('.windowreallydelete').live('click', function (e) {
var windowId = parseInt($(this).parent().parent().parent().attr('id').substring(4),10);
$(this).attr('src','images/arrow_refresh.png');
var self = this;
$.post('https://chrometabcloud.appspot.com/remove', {window: windowId}, function () {
updateTabs();
});
});
var setInfo = function (info) {
$('#saved').html('<div class="info">'+info+'</div>');
}
var TCWindows = [];
var updateTabs = function () {
setInfo('Loading...');
$.get('https://chrometabcloud.appspot.com/tabcloud', function (data) {
if (data.status !== undefined) {
if (data.status == 'loggedin') {
if (data.windows.length == 0) {
setInfo('You haven\'t saved any windows yet!');
} else {
$('#saved').html("");
TCWindows = data.windows;
var i = 0;
data.windows.forEach(function (curWindow) {
setWindowName('winr'+i,curWindow.name);
var winString = '<fieldset class="window" id="winr'+i+'"><legend class="windowname">'+curWindow.name+'</legend><span class="right"><img class="windowdelete" src="images/delete.png" title="Delete window"><img class="windowopen" src="images/add.png" title="Open window"></span><div class="tabs">';
var ti = 0;
curWindow.tabs.forEach(function (curTab) {
var favicon = (curTab.favicon != '' && curTab.favicon !== undefined) ? curTab.favicon : 'chrome://favicon/'+curTab.url;
winString += '<div style="float: left"><img id="tabimgr'+(ti++)+'" windowid="winr'+i+'" class="tabimg" src="'+favicon+'" url="'+curTab.url+'" title="'+curTab.title.replace(/\"/g,"'")+'" /></div>';
});
winString += '</div></fieldset>';
$('#saved').append(winString);
i++;
});
makeSortable();
}
} else {
setInfo('TabCloud requires you login to load your saved windows<br /><a target="_blank" href="https://chrometabcloud.appspot.com/login">Click here to login</a>');
}
} else {
setInfo('Server error, try again later.');
}
}, 'json');
}
setTimeout(updateTabs, 0);
// Extra links
$('#optionslink').click(function (e) {
chrome.tabs.create({
url: chrome.extension.getURL('options.html')
});
});
$('#logoutlink').click(function (e) {
chrome.tabs.create({
url: 'https://chrometabcloud.appspot.com/logout'
});
});
// Tips
$('#tips').innerfade({
speed: 'slow',
timeout: 4000,
type: 'random',
containerheight: '1em'
});
});
</script>
<style>
* {
margin: 0;
padding: 0;
}
.tabplaceholder {
float: left;
width: 20px;
background-color: #ccc;
border-radius: 2px;
}
#trash .tabplaceholder {
opacity: 0.6;
}
body {
width: 300px;
font-size: 0.9em;
font-family: sans-serif;
}
.window {
border: 1px solid #ccc;
border-radius: 5px;
margin: 5px;
padding: 5px;
}
.window legend {
padding: 0 2px;
background-color: white;
}
.tabs img {
margin: 2px;
width: 16px;
height: 16px;
}
.tabs {
padding: 0;
height: 20px;
}
#current {
border-bottom: 1px solid black;
}
.info {
border: 1px solid #ccc;
border-radius: 5px;
margin: 5px;
text-align: center;
padding: 10px;
}
.right {
position: absolute;
right: 15px;
margin-top: -22px;
background-color: white;
padding: 2px;
}
.decryptpass {
width: 150px !important;
}
#tips span {
width: 100%;
}
#tips {
margin-bottom: 3px;
}
</style>
</head>
<body>
<div id="current"></div>
<div id="saved"></div>
<div class="info" style="float: left; width: 220px; font-size: 0.7em;">
<div id="tips">
<span>Click on a window's name to rename it.</span>
<span>Middle click on an icon to open just that tab.</span>
<span>Drag an icon to re-arrange tabs.</span>
<span>Hover on an icon to view the title for that tab.</span>
<span>Click on an icon to jump to that tab.</span>
<span>Drag a tab to the trashcan icon to remove it.</span>
</div>
<a href="" id="optionslink">Options / Help</a> - <a href="" id="logoutlink">Logout</a>
</div>
<div class="tabs" id="trash" style="margin: 10px 5px; float: left; padding: 8px; height: 18px; width: 18px; background: url(images/bin_empty.png) no-repeat 50% 50%; border: 1px solid #ccc; border-radius: 5px;">
</div>
</body>
</html>