-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
78 lines (70 loc) · 2.73 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
/*global chrome:false, FileError: false, window: false , console:false */
(function () {
'use strict';
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
}
window.alert('Error: ' + msg);
console.log('Error: ' + msg);
}
var fs = null,
writeMessage = function (extensionid, msg) {
if (!fs) {
window.alert("Error! fs is null");
return;
}
fs.root.getFile(extensionid + '.txt', {
create: true
}, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onerror = function (e) {
window.alert("Error writing" + msg + "\t" + e);
};
fileWriter.seek(fileWriter.length); // Start write position at EOF.
// Create a new Blob and write it to log.txt.
var bb = new window.WebKitBlobBuilder();
bb.append("\n" + JSON.stringify(msg) + "\n");
fileWriter.write(bb.getBlob('text/plain'));
}, errorHandler); //end createWriter callback
}, errorHandler); //end of getFile Callback
},
onInitFs = function (f) {
fs = f;
chrome.extension.onRequestExternal.addListener(function (request, sender) {
writeMessage(sender.id, request);
});
chrome.extension.onConnectExternal.addListener(
function (port) {
port.onMessage.addListener(
function (msg) {
writeMessage(port.sender.id, msg);
}); //end onMessage Listener
}); //end onconnectexternal listener
}; //end on InitFs
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.webkitStorageInfo.requestQuota(window.PERSISTENT, 1024 * 1024, function (grantedBytes) {
window.requestFileSystem(window.PERSISTENT, grantedBytes, onInitFs, errorHandler);
}, function (e) {
window.alert('Request Quota Error');
console.log('Error', e);
});
}());