-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject.js
87 lines (71 loc) · 2.7 KB
/
inject.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
var current_taps_count = 0; // The number of taps in current pattern
var current_taps_time = new Array(); // For keeping timing of current tap pattern
var record_taps = false; // True when taps are in a single textfield
var last_tapped_element = null; // Last tapped element
var timer = null; // For measuring period after the last tap.
// Capture tap timing
window.addEventListener("mousedown", function(event) {
console.log("mouse target: " + event.target.id);
// Check if same TEXT elements is being clicked. If not, reset tap recording.
if(last_tapped_element == event.target && last_tapped_element.type == "text"){
console.log("same object");
record_taps = true;
// Set timer to check if tap pattern is finished.
if(timer != null) window.clearTimeout(timer); // Remove existing timer
timer = window.setTimeout("processPattern()",2000); // Set 2sec timeout
last_tapped_element.style.backgroundColor = "red";
}else{
console.log("diff object");
resetTapRecord();
}
last_tapped_element = event.target;
// Recording the tap time
var now = new Date();
current_taps_time[current_taps_count] = now.getTime();
current_taps_count++;
}, false);
// Restart tap record when key pressed
window.addEventListener("keyup", function(event) {
resetTapRecord();
}, false);
// This function called when tap input is finished.
function processPattern(){
console.log("pattern ended");
if(record_taps){
var tap_intervals = new Array();
var i;
for(i=0; i<current_taps_count-1; i++){
tap_intervals[i] = (current_taps_time[i+1]-current_taps_time[i]);
}
console.log("tap length: " + tap_intervals.length);
if(tap_intervals.length > 0){
//Send the current tap pattern to background page
chrome.extension.sendRequest({pattern: tap_intervals,value: last_tapped_element.value}, function(response) {
//Do stuff on successful response
});
}
}
resetTapRecord();
}
// Reset tap recording
function resetTapRecord(){
if(timer != null) window.clearTimeout(timer); // Remove timer
current_taps_count = 0;
record_taps = false;
if(last_tapped_element != null) last_tapped_element.style.backgroundColor = null;
}
// For inserting a phrase corresponding to the tap pattern
chrome.extension.onRequest.addListener(onRequest);
function onRequest(request, sender, sendResponse) {
console.log("pattern value:" + request.pattern_value);
if(last_tapped_element != null){
if(request.pattern_value != null){
last_tapped_element.value = request.pattern_value;
}
last_tapped_element.focus();
}
// if (request. == "hello")
// sendResponse({farewell: "goodbye"});
// else
// sendResponse({}); // snub them.
}