This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2cloud.chrome.js
84 lines (79 loc) · 2.29 KB
/
2cloud.chrome.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
twocloud.chrome = {};
twocloud.chrome.icon = {}
twocloud.chrome.devices = {};
twocloud.chrome.users = {};
twocloud.chrome.icon.auth_error = function() {
chrome.browserAction.setIcon({
"path": "img/error.png"
});
chrome.browserAction.setTitle({"title": "Please log in."});
chrome.browserAction.setPopup({"popup": "browser_actions/auth.html"});
};
twocloud.chrome.devices.sync = function(user, onSuccess, onError) {
if(onSuccess == null) {
onSuccess = function() {};
}
if(onError == null) {
onError = function(e) {
console.log(e);
};
}
twocloud.devices.list(user, function(data, textStatus, jqXHR) {
if(textStatus == "error") {
onError(data);
return
}
var devices = data.data;
twocloud.indexedDB.devices.clear(function() {
twocloud.context.clear();
for(var i=0; i < devices.length; i++) {
twocloud.indexedDB.devices.add(devices[i], function(device) {
var added_device = device;
if(i == devices.length) {
twocloud.indexedDB.devices.list(false, function(index_device) {
twocloud.context.addDevice(index_device);
if(index_device.slug == added_device.slug) {
return onSuccess();
}
}, onError);
}
}, onError);
}
}, onError);
});
};
twocloud.chrome.users.load = function(callback) {
if(localStorage["username"] == null || localStorage["username"] == "") {
bg = chrome.extension.getBackgroundPage();
bg.twocloud.auth.username = null;
bg.twocloud.auth.secret = null;
bg.twocloud.chrome.icon.auth_error();
return callback();
}
twocloud.indexedDB.users.get(localStorage["username"], function(user) {
bg = chrome.extension.getBackgroundPage();
bg.twocloud.auth.username = user.username;
bg.twocloud.auth.secret = user.secret;
return callback();
}, function(error) {
bg = chrome.extension.getBackgroundPage();
console.log(error);
bg.twocloud.chrome.icon.auth_error();
return callback();
});
};
twocloud.chrome.init = function() {
twocloud.chrome.users.load(function() {
if(twocloud.auth.username != null && twocloud.auth.secret != null) {
twocloud.chrome.devices.sync(twocloud.auth.username);
}
});
}
twocloud.indexedDB.init(function() {
twocloud.context.init();
if(localStorage["setup"]) {
twocloud.chrome.init();
} else {
chrome.tabs.create({"url": chrome.extension.getURL("auth.html")});
}
});