Skip to content

Commit 149a6c9

Browse files
committed
First pass at keypress breadcrumbs
1 parent 521768a commit 149a6c9

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

example/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>Scratch Disk</title>
55
</head>
6-
<script src="../dist/raven.js"></script>
6+
<script src="../build/raven.js"></script>
77
<!-- <script src="scratch.min.js"></script> -->
88
<script src="scratch.js" crossorigin></script>
99
<script src="file.min.js" crossorigin></script>
@@ -39,6 +39,7 @@
3939
<button onclick="throwString()">throw string</button>
4040
<button onclick="showDialog()">show dialog</button>
4141
<button onclick="blobExample()">blob example</button>
42+
<input/>
4243

4344
</body>
4445
</html>

src/raven.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var urlencode = utils.urlencode;
1919
var uuid4 = utils.uuid4;
2020
var htmlTreeAsString = utils.htmlTreeAsString;
2121
var parseUrl = utils.parseUrl;
22+
var debounce = utils.debounce;
2223

2324
var dsnKeys = 'source protocol user pass host port path'.split(' '),
2425
dsnPattern = /^(?:(\w+):)?\/\/(?:(\w+)(:\w+)?@)?([\w\.-]+)(?::(\d+))?(\/.*)/;
@@ -618,7 +619,6 @@ Raven.prototype = {
618619
}
619620
},
620621

621-
622622
/**
623623
* Wraps addEventListener to capture UI breadcrumbs
624624
* @param evtName the event name (e.g. "click")
@@ -644,6 +644,17 @@ Raven.prototype = {
644644
};
645645
},
646646

647+
_keypressEventHandler: function() {
648+
var self = this;
649+
650+
// TODO: if somehow user switches keypress target before
651+
// debounce timeout is triggered, we will only capture
652+
// a single breadcrumb from the LAST target (acceptable?)
653+
return debounce(function (evt) {
654+
self._breadcrumbEventHandler('keypress')(evt);
655+
}, 500); // 500ms after last consecutive keypress, record breadcrumb
656+
},
657+
647658
/**
648659
* Captures a breadcrumb of type "navigation", normalizing input URLs
649660
* @param to the originating URL
@@ -727,8 +738,12 @@ Raven.prototype = {
727738

728739
// TODO: more than just click
729740
var before;
730-
if ((global === 'EventTarget' || global === 'Node') && evt === 'click') {
731-
before = self._breadcrumbEventHandler(evt, fn);
741+
if (global === 'EventTarget' || global === 'Node') {
742+
if (evt === 'click'){
743+
before = self._breadcrumbEventHandler(evt, fn);
744+
} else if (evt === 'keypress') {
745+
before = self._keypressEventHandler();
746+
}
732747
}
733748
return orig.call(this, evt, self.wrap(fn, undefined, before), capture, secure);
734749
};
@@ -764,6 +779,7 @@ Raven.prototype = {
764779
// to the document. Do this before we instrument addEventListener.
765780
if (this._hasDocument) {
766781
document.addEventListener('click', self._breadcrumbEventHandler('click'));
782+
document.addEventListener('keypress', self._keypressEventHandler());
767783
}
768784

769785
// event targets borrowed from bugsnag-js:

src/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ function uuid4() {
153153
}
154154
}
155155

156+
// Returns a function, that, as long as it continues to be invoked, will not
157+
// be triggered. The function will be called after it stops being called for
158+
// N milliseconds. If `immediate` is passed, trigger the function on the
159+
// leading edge, instead of the trailing.
160+
// https://davidwalsh.name/javascript-debounce-function
161+
function debounce(func, wait) {
162+
var timeout;
163+
return function() {
164+
var context = this,
165+
args = arguments;
166+
167+
var later = function() {
168+
timeout = null;
169+
func.apply(context, args);
170+
};
171+
clearTimeout(timeout);
172+
timeout = setTimeout(later, wait);
173+
};
174+
};
175+
156176
/**
157177
* Given a child DOM element, returns a query-selector statement describing that
158178
* and its ancestors
@@ -245,6 +265,7 @@ module.exports = {
245265
joinRegExp: joinRegExp,
246266
urlencode: urlencode,
247267
uuid4: uuid4,
268+
debounce: debounce,
248269
htmlTreeAsString: htmlTreeAsString,
249270
htmlElementAsString: htmlElementAsString,
250271
parseUrl: parseUrl

0 commit comments

Comments
 (0)