Idle.js icon indicating copy to clipboard operation
Idle.js copied to clipboard

Overriding of existing listeners

Open n-sviridenko opened this issue 7 years ago • 5 comments

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

n-sviridenko avatar Jul 22 '17 18:07 n-sviridenko

Thanks for picking that up. Do you have any suggestion on how to attach to an existing event?

shawnmclean avatar Jul 22 '17 19:07 shawnmclean

@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/

n-sviridenko avatar Jul 22 '17 20:07 n-sviridenko

Using the moment, I would like to ask did you thought about https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/idle support?

n-sviridenko avatar Jul 22 '17 20:07 n-sviridenko

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.)

shawnmclean avatar Jul 23 '17 01:07 shawnmclean

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 }.

n-sviridenko avatar Jul 23 '17 06:07 n-sviridenko