Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding of existing listeners #28

Open
n-sviridenko opened this issue Jul 22, 2017 · 5 comments
Open

Overriding of existing listeners #28

n-sviridenko opened this issue Jul 22, 2017 · 5 comments
Assignees

Comments

@n-sviridenko
Copy link

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
Copy link
Owner

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

@n-sviridenko
Copy link
Author

n-sviridenko commented Jul 22, 2017

@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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

n-sviridenko commented Jul 23, 2017

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

@shawnmclean shawnmclean self-assigned this Jul 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants