Skip to content

Instantly share code, notes, and snippets.

@RByers
Forked from majido/detectProgrammaticScroll.js
Last active August 15, 2017 10:34
Show Gist options
  • Save RByers/688d8b0ca8d03e17053a to your computer and use it in GitHub Desktop.
Save RByers/688d8b0ca8d03e17053a to your computer and use it in GitHub Desktop.
Script to be pasted into developer tools to log calls to programmatic scrolling APIs. Currently tested just on Chrome and Firefox.
(function(){
// Override function in given prototype to add a log statement
function logMethod(prototype, fname) {
var name = prototype.constructor.name + '.' + fname;
if (!(fname in prototype)) {
console.warn("Warning: can't instrument " + name);
return;
}
console.log("Instrumenting " + name);
var original = prototype[fname];
prototype[fname] = function() {
// Want single line output but with optional callstack - abuse 'error' for this.
// console.trace is also good but expands the callstack by default.'
console.error(name + ' invoked with ' + JSON.stringify(Array.prototype.slice.call(arguments)));
// debugger;
original.apply(this, arguments);
};
}
// Override property setter to add a log statement
function logSetter(prototype, property, errContext) {
var name = prototype.constructor.name + '.' + property;
var original = Object.getOwnPropertyDescriptor(prototype, property);
if (!original) {
console.warn("Warning: can't instrument " + name + '. ' + errContext);
return;
}
console.log("Instrumenting " + name);
Object.defineProperty(prototype, property, {
set: function(v) {
var changed = v !== original.get.call(this);
console.error(name + ' set to:' + v + (changed?' (changed)':' (not changed)')); // add changed indicator
// debugger;
original.set.call(this, v);
},
get: original.get
});
}
var win = window;
// Work around for http://crbug.com/475587
if ('scrollBy' in window && !window.hasOwnProperty('scrollBy'))
win = Window.prototype;
for (var m of ['scrollBy', 'scrollTo', 'scroll'])
logMethod(win, m);
for (var m of ['scrollIntoView', 'scrollIntoViewIfNeeded', 'focus'])
if (m in Element.prototype)
logMethod(Element.prototype, m);
for (var p of ['scrollTop', 'scrollLeft'])
logSetter(Element.prototype, p, "Chrome 43+ required (http://crbug.com/43394)");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment