2 * Content script for Chrome Sounds.
3 * Tracks in-page events and notifies the background page.
6 function sendEvent(event, value) {
7 console.log("sendEvent: " + event + "," + value);
8 chrome.extension.sendRequest({eventName: event, eventValue: value});
11 // Timers to trigger "stopEvent" for coalescing events.
14 function stopEvent(type) {
16 sendEvent(type, "stopped");
19 // Automatically coalesces repeating events into a start and a stop event.
20 // |validator| is a function which should return true if the event is
21 // considered to be a valid event of this type.
22 function handleEvent(event, type, validator) {
24 if (!validator(event)) {
28 var timerId = timers[type];
29 var eventInProgress = (timerId > 0);
30 if (eventInProgress) {
31 clearTimeout(timerId);
34 sendEvent(type, "started");
36 timers[type] = setTimeout(stopEvent, 300, type);
39 function listenAndCoalesce(target, type, validator) {
40 target.addEventListener(type, function(event) {
41 handleEvent(event, type, validator);
45 listenAndCoalesce(document, "scroll");
47 // For some reason, "resize" doesn't seem to work with addEventListener.
48 if ((window == window.top) && document.body && !document.body.onresize) {
49 document.body.onresize = function(event) {
50 sendEvent("resize", "");
54 listenAndCoalesce(document, "keypress", function(event) {
55 if (event.charCode == 13)
58 // TODO(erikkay) This doesn't work in gmail's rich text compose window.
59 return event.target.tagName == "TEXTAREA" ||
60 event.target.tagName == "INPUT" ||
61 event.target.isContentEditable;