Idle.js
Idle.js copied to clipboard
Overriding of existing listeners
I think, it will be better to check if the window
object already have a value in onclick
, onmousemove
etc. to not override them:
https://github.com/shawnmclean/Idle.js/blob/master/src/idle.coffee#L52
Thanks for picking that up. Do you have any suggestion on how to attach to an existing event?
@shawnmclean Hi. Thanks for fast reaction. I think it's possible to do something like:
function proxyMethod(object, propName, method) {
const existingMethod = object[propName];
let finalMethod = method;
if (typeof existingMethod === 'function') {
finalMethod = (...args) => {
existingMethod(...args);
return method(...args)
};
}
object[propName] = finalMethod;
}
window.onclick = () => console.log('attached earier');
proxyMethod(window, 'onclick', () => console.log('click'));
proxyMethod(window, 'onclick', () => console.log('click #2'));
https://jsfiddle.net/d9dvfajb/
Using the moment, I would like to ask did you thought about https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/idle support?
Thanks for the code suggestion.
Hey! If they now have an idle feature in the browser's api, why use this one? (I haven't dug deeper into it to see what constitutes an Idle
state.)
They have it. But only recent browsers support it. And edge, for example, doesn't. So, it's nice to have some if (supports) { use native } else { use the library }
.