-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
110 lines (93 loc) · 2.99 KB
/
background.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
chrome.runtime.onStartup.addListener(update);
chrome.runtime.onInstalled.addListener(installed);
var apiUrl = 'https://www.googleapis.com/drive/v2/';
function installed (details) {
if (details.reason == 'install') {
console.log('install');
chrome.storage.local.set({
'files': {}
});
}
update();
}
function update (callback) {
chrome.identity.getAuthToken({interactive: true}, function (token) {
var xhr = new XMLHttpRequest();
var query = 'title = "Excess" and mimeType = "application/vnd.google-apps.folder"';
xhr.open('GET', apiUrl + 'files?q=' + query);
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.onload = function () {
var data = JSON.parse(xhr.responseText);
var folder = data.items[0];
if (!folder) {
var fxhr = new XMLHttpRequest();
fxhr.open('POST', apiUrl + 'files');
fxhr.setRequestHeader('Authorization', 'Bearer ' + token);
fxhr.setRequestHeader('Content-Type', 'application/json');
fxhr.onload = function () {
folder = JSON.parse(fxhr.responseText).items.filter(function (i) {
return (i.mimeType == 'application/vnd.google-apps.folder'
&& i.title == 'Excess');
});
}
var folderData = {
'title': 'Excess',
'mimeType': 'application/vnd.google-apps.folder'
}
fxhr.send(JSON.stringify(folderData));
}
chrome.storage.local.set({'folder': folder});
var query = '"' + folder.id + '" in parents and trashed = false';
var cxhr = new XMLHttpRequest();
cxhr.open('GET', apiUrl + 'files?q=' + query);
cxhr.setRequestHeader('Authorization', 'Bearer ' + token);
cxhr.onload = function () {
var files = JSON.parse(cxhr.responseText);
var local;
chrome.storage.local.get('files', function (result) {
var local = result.files;
Object.keys(local).filter(function (id) {
return files.items.filter(function (f) {
return f.id == id;
}).length == 0;
}).forEach(function (id) {
delete local[id];
});
var incoming = files.items.filter(function (item ) {
return !local[item.id] || local[item.id].version != item.version;
});
if (incoming.length == 0) {
chrome.storage.local.set({files: local});
if (callback) callback(local);
} else {
var num = incoming.length;
incoming.forEach(function (item) {
var dxhr = new XMLHttpRequest();
dxhr.open('GET', item.downloadUrl);
dxhr.setRequestHeader('Authorization', 'Bearer ' + token);
dxhr.onload = function () {
var raw = dxhr.responseText;
item.raw = raw.trim();
num--;
if (num == 0) {
chrome.storage.local.set({files: local});
if (callback) callback(local);
}
};
dxhr.send();
local[item.id] = item;
});
}
});
}
cxhr.send();
}
xhr.send();
});
}
chrome.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(function (msg) {
if (msg == 'update')
update(function (files) {port.postMessage(files)});
});
});