shawnmclean / Idle.js

Javascript activity library for the browser. (away, idle, etc)
Other
402 stars 59 forks source link

Overriding of existing listeners #28

Open n-sviridenko opened 7 years ago

n-sviridenko commented 7 years ago

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

shawnmclean commented 7 years ago

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

n-sviridenko commented 7 years ago

@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 commented 7 years ago

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

shawnmclean commented 7 years ago

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

n-sviridenko commented 7 years ago

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