Working with webkit notifications on Chrome has presented a challenge. The
window.webkitNotifications.requestPermission
method must be called from a user action, such as a click. Attempting to call it at any other time will not have any effect and will not produce an error.
Despite the restriction, there are situations where I need to run this method later on, triggered by another event that was originally generated by an authentic click.
I attempted to simulate a real click event in order to run the method like this:
var a = document.createElement('a');
a.addEventListener('click', function () {
window.webkitNotifications.requestPermission()
});
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0,
false, false, false, false, 0, null);
a.dispatchEvent(evt);
As anticipated, this simulated click did not trigger the method. This raises questions about how Chrome distinguishes genuine user actions from those that are generated programmatically.
I also tried keeping a reference to the original click event and passing it back when attempting to call the method again, but it proved ineffective.
To better illustrate my dilemma, I've created a fiddle: http://jsfiddle.net/arnaudbreton/B38yJ/1/