stackexchange-notifications
stackexchange-notifications copied to clipboard
Add option to use native notifications instead of Chrome's notifications
A user wanted native notifications in Chrome, so I offered the following advice:
Open chrome://extensions, enable developer mode. Click on Inspect background page at my extension. Click on the sources tab, select "using-websocket.js" and set a breakpoint at the first line. Then run
delete chrome.notifications;in the console. Then copy-paste https://gist.github.com/Rob--W/5926727 in the console and run it. Then continue the breakpoint, and generate a notification (e.g. by callingsetUnreadCount(1);). Does that give the desired result?
This seems to give the desired result, so let's properly implement this:
- [ ] Add new checkbox option to options.html
- [ ] Bind an event listener to the checkbox in options.js that toggles a value in localStorage.
- [ ] Modify using-websockets.js to detect whether this preference is set, and if so, use the
Notificationconstructor instead ofchrome.notificationsto display the notification.
Hullo,
I did notice a bugz with the above.
The notifications do pop up, but when clicking on them, stackoverflow doesn't open. I.e, nothing happens when you click on the notification.
Normally when clicking on a notification (ex Facebook msg): if FB tab exists, -- it activates that tab and loads message else -- creates new tab and loads page with message remove notification from notification tray.
Any ideas on how to add click hander?
Have you tried assigning a click handler to the onclick property? https://github.com/Rob--W/stackexchange-notifications/blob/c16a3df5c9b25139b7e25acb1fdfbf857319f6e0/Chrome/using-websocket.js#L241 should be changed to this:
var useNativeNotifications = localStorage.getItem('name of the preference') === '1';
if (window.Notification && useNativeNotifications) {
// See https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification
_notification = new Notification(...);
_notification.onclick = function() {
openTab(getLink() || generateDefaultLink());
};
return;
}
if (!window.webkitNotifications) {
....
Also https://github.com/Rob--W/stackexchange-notifications/blob/c16a3df5c9b25139b7e25acb1fdfbf857319f6e0/Chrome/using-websocket.js#L243 should be changed to also account for both ways of closing the notification (the old webkitNotifications API used cancel, the new one uses close()).
Thank you for suggestion. I have my final university exam on Thursday. Then I'm done (with Uni ^_^). I'll try the above Friday/weekend and will get back to you.
@LeoUfimtsev Best of luck with your exams!