forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
62 lines (53 loc) · 1.68 KB
/
content.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
/*
* Content script for Chrome Sounds.
* Tracks in-page events and notifies the background page.
*/
function sendEvent(event, value) {
console.log("sendEvent: " + event + "," + value);
chrome.extension.sendRequest({eventName: event, eventValue: value});
}
// Timers to trigger "stopEvent" for coalescing events.
var timers = {};
function stopEvent(type) {
timers[type] = 0;
sendEvent(type, "stopped");
}
// Automatically coalesces repeating events into a start and a stop event.
// |validator| is a function which should return true if the event is
// considered to be a valid event of this type.
function handleEvent(event, type, validator) {
if (validator) {
if (!validator(event)) {
return;
}
}
var timerId = timers[type];
var eventInProgress = (timerId > 0);
if (eventInProgress) {
clearTimeout(timerId);
timers[type] = 0;
} else {
sendEvent(type, "started");
}
timers[type] = setTimeout(stopEvent, 300, type);
}
function listenAndCoalesce(target, type, validator) {
target.addEventListener(type, function(event) {
handleEvent(event, type, validator);
}, true);
}
listenAndCoalesce(document, "scroll");
// For some reason, "resize" doesn't seem to work with addEventListener.
if ((window == window.top) && document.body && !document.body.onresize) {
document.body.onresize = function(event) {
sendEvent("resize", "");
};
}
listenAndCoalesce(document, "keypress", function(event) {
if (event.charCode == 13)
return false;
// TODO(erikkay) This doesn't work in gmail's rich text compose window.
return event.target.tagName == "TEXTAREA" ||
event.target.tagName == "INPUT" ||
event.target.isContentEditable;
});