dobarkod / cookie-banner

JavaScript based cookie-info banner for complying with EU cookie law
MIT License
425 stars 85 forks source link

Added Event-Handler Arguments for Inserted & Closed #65

Closed wottpal closed 6 years ago

wottpal commented 6 years ago

Hey there, I just added two event-handler arguments. Hope this is fine! Only adds ~8 small lines of code. I also described them in the README.

Regards from Germany, Dennis

wottpal commented 6 years ago

Example:

var options = {
  insertedHandler: function (el) {
      console.log("Hey there I got added.", el);
  }, 
  closedHandler: function () {
      console.log("I got closed");
  }
};
var cb = new Cookiebanner(options); cb.run();
zytzagoo commented 6 years ago

Hey, thanks for the contribution!

Here's a few quick thoughts based on a cursory look:

Let's see what others think!

wottpal commented 6 years ago

Hey, thanks for reviewing my PR! I improved the following things:

Dennis :)

wottpal commented 6 years ago
bildschirmfoto 2018-05-24 um 23 28 41
zytzagoo commented 6 years ago

Love the changes and the tests, great job!

We must resolve conflicts tho, can you rebase this against latest master?

And there's a breaking change in there with regards to old IE (due to String.prototype.startsWith), but I can take care of that later.

wottpal commented 6 years ago

I hope I've done this right, never done this before 😬

zytzagoo commented 6 years ago

That's one way of doing it, sure. Thanks.

Could you also take care of the linting errors as reported by travis?

wottpal commented 6 years ago

Yeah I fixed most of them. Only one about eval() can be harmful is still there but imo there is no other way to parse code from data-attributes. So either we ignore the warning and let the user assure no harmful code is inserted into their data-attributes or we make handlers configurable via JS only.

zytzagoo commented 6 years ago

We can add a trailing comment instructing jshint to ignore that line if eval is to stay?

    ...; // jshint ignore:line

Or, further investigate the use of new Function to create handlers (instead of eval), but it would complicate things some more (and not really change things too much, apart from preventing handler's access to lexical scope? We'd still have to instruct jshint to ignore the line if I remember correctly)

I think having handlers defined in HTML could be handy for certain cases, even if it brings some caveats due to eval usage...

Pinging @senko to hear his thoughts...

wottpal commented 6 years ago

We can add a trailing comment instructing jshint to ignore that line if eval is to stay? ✅

Travis is still seeing a failing test but for me in Chrome all tests are just fine 🤷‍♂️

zytzagoo commented 6 years ago

Yeah, restarted the travis build quickly to see if it'll happen again, and it did.

Not sure what's going on in there now... let me pull down and checkout the PR locally to dig in...

zytzagoo commented 6 years ago

Tests were failing due to modern ES syntax.

I also spotted a few minor details that I tweaked:

I think we can merge this now, and then take care of String.startsWith replacement for older browsers (old IE is still supported kindof, since it worked so far) + changelog entries and whatnot...

Thanks a ton for your work on this!