-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.html
259 lines (225 loc) · 7.7 KB
/
background.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
<!DOCTYPE html>
<html>
<head>
<title>Taps</title>
<style>
* {
margin:0px;
padding:0px;
}
</style>
</head>
<body>
<script>
create_or_clear_data();
// Listen for the content script to send a message to the background page.
chrome.extension.onRequest.addListener(onRequest);
var input_pattern = null;
var input_value = null;
function getInputPattern(){
return input_pattern;
}
function getInputValue(){
return input_value;
}
function setInputPattern(p){
input_pattern = p;
}
function setInputValue(v){
input_value = v;
}
// Called when a tap pattern is passed from a user.
function onRequest(request, sender, sendResponse) {
input_pattern = request.pattern;
input_value = request.value;
// Match the tap pattern to existing patterns
get_data(patternMatching);
// Return nothing to let the connection be cleaned up.
sendResponse({});
};
// Calculate the distance between two tap patterns.
function getTapPatternsDistance(pattern1, pattern2, length){
var distance = 0;
var i;
for(i=0; i<length; i++){
distance += Math.abs(pattern1[i] - pattern2[i]);
}
return 1.0 * distance / length;
}
function patternMatching(data){
var threshold = 200;
var min_distance = 10000;
var pattern_id;
if(data != null){
for(i=0; i<data.length; i++){
if(data[i].seconds.length == input_pattern.length){
var distance = getTapPatternsDistance(input_pattern,data[i].seconds,input_pattern.length);
if(distance < min_distance){
min_distance = distance;
pattern_id = i;
}
}
}
}
console.log("min distance: " + min_distance);
if(min_distance < threshold){
// Found the same pattern in the dictionary
console.log("Found pattern id: " + pattern_id);
input_pattern = null; // Need to clear this
input_value = null; // Need to clear this
}else{
// It is a unidentified pattern. When user add it to the dictionary,
// input_pattern and input_value will be used.
console.log("unidentified pattern");
// Will not add to the dictionary here
pattern_id = null;
chrome.browserAction.setIcon({path:"icon2.png"});
}
var value = null;
if(pattern_id != null) value = data[pattern_id].inputtext;
// Send the pattern value to content script, which will insert the value to focused textfield.
// Need to call even if the pattern is unidentified, because this call sets focus.
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {pattern_value: value}, function(response) {
//console.log(response.farewell);
});
});
}
// File API -----------------------------------------------------------------
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;
};
console.log("error:" + msg);
}
function to_json(milliseconds, inputtext) {
var json = "{ seconds: [";
for (var index in milliseconds) {
if (index > 0) json += ', ';
json += milliseconds[index];
}
json += '], inputtext: "' + inputtext + '" }';
console.log(json);
return json;
}
function add_write(fs, existing, milliseconds, inputtext, callback) {
fs.root.getFile('tapfile.txt', {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onerror = function(e) {
errorHandler(e.currentTarget.error);
};
console.log("truncating");
fileWriter.truncate(0);
console.log("truncated");
}, errorHandler);
}, errorHandler);
var bb = new (window.BlobBuilder || window.WebKitBlobBuilder)();
var evaled = eval(existing);
console.log(evaled);
bb.append('{\n entries: [\n');
for (var ent in evaled) {
bb.append(' ' + to_json(evaled[ent]["seconds"], evaled[ent]["inputtext"]) + ',\n');
}
bb.append(' ' + to_json(milliseconds, inputtext) + ',\n ]\n}');
var blob = bb.getBlob();
console.log("blob made");
fs.root.getFile('tapfile.txt', {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onerror = function(e) {
errorHandler(e.currentTarget.error);
};
console.log("writing");
fileWriter.write(blob);
console.log("written");
if (callback != undefined && callback != null)
setTimeout(callback, 10);
}, errorHandler);
}, errorHandler);
chrome.extension.sendRequest({"type" : "init"});
}
function create_or_clear_fs_init(fs, callback) {
fs.root.getFile('tapfile.txt', {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onerror = function(e) {
errorHandler(e.currentTarget.error);
};
fileWriter.truncate(0);
if (callback != undefined && callback != null)
setTimeout(callback, 10);
}, errorHandler);
}, errorHandler);
}
function add_data_fs_init(fs, milliseconds, inputtext, callback) {
console.log("Init");
fs.root.getFile('tapfile.txt', {create: false, exclusive: false}, function(fileEntry) {
fileEntry.file(function(file) {
console.log("reading");
var reader = new FileReader();
reader.onloadend = function(e) {
console.log("read");
console.log(this.result);
add_write(fs, this.result, milliseconds, inputtext, callback);
};
reader.readAsText(file);
}, function(e) { console.log("Read1:"); errorHandler(e); add_write(fs, "", [931, 49], "Add 2", callback); } )
}, function(e) { console.log("Read2:"); errorHandler(e); add_write(fs, "", [294], "Add 3", callback); } );
}
function get_data_fs_init(fs, callback) {
console.log("Init");
fs.root.getFile('tapfile.txt', {create: false, exclusive: false}, function(fileEntry) {
fileEntry.file(function(file) {
console.log("reading");
var reader = new FileReader();
reader.onloadend = function(e) {
console.log("read");
console.log(this.result);
callback(eval(this.result));
};
reader.readAsText(file);
}, function(e) { console.log("Read1:"); errorHandler(e); callback([]);} )
}, function(e) { console.log("Read2:"); errorHandler(e); callback([]); } );
}
function fs_error(e) {
console.log("fs_error:" + e);
}
function get_data(callback) {
console.log("getting data");
window.webkitRequestFileSystem(window.TEMPORARY,
1024 * 1024,
function(fs) { get_data_fs_init(fs, callback); },
fs_error);
console.log("got data");
}
function create_or_clear_data(callback) {
window.webkitRequestFileSystem(window.TEMPORARY,
1024 * 1024,
function(fs) { create_or_clear_fs_init(fs, callback); },
fs_error);
}
function add_data(milliseconds, inputtext, callback) {
window.webkitRequestFileSystem(window.TEMPORARY,
1024 * 1024,
function(fs) { add_data_fs_init(fs, milliseconds, inputtext, callback); },
fs_error);
}
</script>
</body>
</html>