-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
147 lines (122 loc) · 4.21 KB
/
background.js
File metadata and controls
147 lines (122 loc) · 4.21 KB
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
/* global chrome listings dirEntries clobber patching ExtractionMap
jsyaml fetch */
var themeColor = '#dd5500';
// Tell Chrome to open the app when the app launches
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
id: 'pulltool-main-window',
frame: {color: themeColor}
});
});
function singleMessageListener(message, sender, respond) {
// TODO: version pinging
if (message == 'ping') return respond('pong');
}
chrome.runtime.onMessage.addListener(singleMessageListener);
chrome.runtime.onMessageExternal.addListener(singleMessageListener);
function connectionListener(port) {
function die(err) {
port.postMessage({type: 'error', error: err});
return port.disconnect();
}
var totalSources = 0;
var finishedSources = 0;
function progressMessage() {
if (finishedSources < totalSources) {
if (totalSources > 1) {
return 'Downloading archives ('
+ finishedSources + '/' + totalSources + ')';
} else {
return 'Downloading archive';
}
} else {
return 'Processing ' + (totalSources > 1 ? 'archives' : 'archive');
}
}
function postDownloadProgress() {
port.postMessage({type: 'status', message: progressMessage()});
}
function pullArchive(source) {
return fetch(source.url).then(function(response){
// JSZip requires ArrayBuffer
if (source.type == "zip") {
return response.arrayBuffer();
// Everything else (blob) wants blob
} else {
return response.blob();
}
}).then(function(data) {
++finishedSources;
postDownloadProgress();
return data;
});
}
var messageHandlers;
function pull(listing) {
messageHandlers = null;
listings.setLastUsed(listing).then(function(){
var config = jsyaml.safeLoad(listing.config);
if (config.sources) {
totalSources = config.sources.length;
}
if (totalSources == 0) {
return die(new Error('No recognized source to pull from'));
}
var exMap = new ExtractionMap();
postDownloadProgress();
// TODO: enter state where pull can be aborted
return Promise.all(config.sources.map(pullArchive))
.then(function(archives){
for (var i = 0; i < archives.length; i++) {
exMap.addArchive(config.sources[i], archives[i]);
}
return dirEntries.restoreEntry(listing.retainedDirId);
}).then(function (dirEntry) {
port.postMessage({type: 'status', message: 'Extracting archive'});
return clobber.extractPatchedToDir(exMap,
patching.pathFilePatcher(config.patch), dirEntry)
.then(function () {
port.postMessage({type: 'finish'});
return port.disconnect();
});
}).catch(die);
});
}
var starters = {
startPull: function startPull(message) {
if (message.listingId) {
return listings.getById(message.listingId)
.then(function (listing) {
if (listing) {
return pull(listing);
} else return die(new Error(
'Listing "' + message.listingId + '" not found'));
});
} else return die(new Error('No listingId to pull specified'));
},
repeatLastPull: function repeatLastPull(message) {
if (message.pullType) {
return listings.getLastUsed(message.pullType)
.then(function (listing) {
if (listing) {
return pull(listing);
} else return die(new Error(
'No last "' + message.pullType + '" pull listed'));
});
} else return die(new Error('No pullType to repeat specified'));
}
};
messageHandlers = starters;
function receiveMessage(message) {
if (messageHandlers) {
var handler = messageHandlers[message.type];
if (handler) {
return handler(message);
} else return port.postMessage({type: 'reject', message:
'Message type "' + message.type + '" unrecognized or invalid'});
} else return port.postMessage({type: 'reject', message:
'Current state may not be messaged'});
}
port.onMessage.addListener(receiveMessage);
}
chrome.runtime.onConnect.addListener(connectionListener);