Is there a way for an extension to add a listener to browser notifications, and access it content? I'm trying to use Google Calendar's notifications to fire a custom function.
3 Answers
You can hook the webkitNotifications.createNotification
function so that whenever a notification is created you run some particular code.
Create a file called notifhook.js:
(function() { // save the original function var origCreateNotif = webkitNotifications.createNotification; // overwrite createNotification with a new function webkitNotifications.createNotification = function(img, title, body) { // call the original notification function var result = origCreateNotif.apply(this, arguments); // bind a listener for when the notification is displayed result.addEventListener("display", function() { // do something when the notification is displayed // use img, title, and body to read the notification // YOUR TRIGGERED CODE HERE! }); return result; } })();
Next, include
notifhook.js
in yourweb_accessible_resources
list in your manifest.Finally, in a content script, inject a
<script>
element into the page withnotifhook.js
as itssrc
:var s = document.createElement("script"); s.src = chrome.extension.getURL("notifhook.js"); document.documentElement.appendChild(s);
You might be tempted to just use notifhook.js
as your content script, but that won't work because the content script and the web page have separate execution environments. Overwriting the content script's version of webkitNotifications.createNotification
won't affect the Google Calendar page at all. Thus, you need to inject it via <script>
tag, which will affect both the page and the content script.
-
Thank you. So, there is no API to do that. But this hack will do it. – Alejandro Silvestri Apr 11 '13 at 21:17
The above code is quite old and won't work anymore. However the method author has approached to intercept remains same and very useful trick that can be applied at multiple pages.
I have update on how we can access: How to capture or listen to browser notifications?

- 56
- 4
Not if what you want is a Chrome extension that sends notifications capture Google Calendar. If I'm not mistaken, Google's Calendar sends mail and SMS notifications. Also, I think there is no API function to which you could ask to see if there are pending notifications. The only thing that gets in the documentation is that the Google+ event notifications are sent to Google+ and perhaps access to the API can capture.

- 356
- 3
- 11