Access new Chrome(ium) Notifications programatically

46 views
Skip to first unread message

Kristofer Johansson

unread,
Jul 27, 2016, 1:30:06 PM7/27/16
to Chromium-Extensions-Announce

Hi, Everone!


My question was originally intended for Google Chrome and I posted it on Stackoverflow but thus far it has not yielded any answers. I'm not sure Stackoverflow (or this place) is the right place to ask but I figured you guys are competent extension programmers with vast experience using the APIs so you should know if what I want to accomplish can be done on Google Chrome or if I would need to build my own Chromium to make it happen. If I am in the wrong place I apologize and ask you to point me in the right direction.


Here is my original post from Stackoverflow:

****************************

I have no previous experience with programming Google Chrome plugins which is why I am starting here to see if what I want to accomplish is possible/reasonable. I do however have a pretty broad experience in programming in general.


What I want: I want some kind of "trigger" to go off when a new Chrome Notification (you know these little pop ups above the system tray) is popping up. I want to execute some script/code depending on what information the notification contains so that I for example could have an alarm go off if I receive an email from a certain user with a certain key word in the subject and get this in a pop up from my Gmail Notifier extension. This is however just an example and I have a bunch of ideas for different notifications from different extensions and websites so don't get caught up on that particular example.


When I look at the Chrome Notification API I see that there is a getAll method that supposedly is getting all the "notifications in the system" but I do not find any Event for new notifications. I suppose a possibility would be to poll with getAll a couple of times per second (it needs to be really fast for some implementations I have in mind) but it feels very tacky.


Is there any way to easily access new Notifications programatically in Chrome? (I'm open to all solutions, programming languages and such...)

******************************


What do you guys say?


Thank You,

/Kris



Antony Sargent

unread,
Jul 27, 2016, 4:30:06 PM7/27/16
to Kristofer Johansson, Chromium-Extensions-Announce
The notifications API you linked to is for an app to create its own notifications, and I think (but am not positive off the top of my head) that the getAll method just provides access to the app's own notifications, not those generated by other apps / webpages. 

Your gmail new mail notifier example could be fulfilled by using the gmail javascript API.

For other specific websites using the web notifications API (but not other chrome apps/extensions), you could possibly write an extension with a contentScript with a run_at value of document_start to override and intercept (ie "monkeypatching") the built-in Notification object. Obviously doing this on every web page isn't great (introduces a little delay), so you'd want to have a reasonable "matches" value containing match patterns just for the sites you're interested in.




--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/76520c67-e7ac-4afb-b8cb-5f0f41c86951%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Kristofer Johansson

unread,
Jul 28, 2016, 2:30:10 AM7/28/16
to Chromium-Extensions-Announce, kristo...@gmail.com
Thank you, Antony.

I suspected the getAll would only get messages generated by the extension it was being called from but since it said something like "returns all messages currently in the system" I wasn't sure. (If someone knows the answer 100%, please chime in so we can put this idea to the side.)

I have never dealt with contentScripts so I'll study up on them and see if I can get it to do the trick for me. The implementation I'll try first will be on notification from a specific website so matching should be easy.

You mention a little delay:
1. Is that on the notifications or on the loading of the page? 
2. Are we talking like a quarter of a second here, or more?

Thank you again for your answer!
Sincerely,
/Kris


On Wednesday, July 27, 2016 at 10:30:06 PM UTC+2, Antony Sargent wrote:
The notifications API you linked to is for an app to create its own notifications, and I think (but am not positive off the top of my head) that the getAll method just provides access to the app's own notifications, not those generated by other apps / webpages. 

Your gmail new mail notifier example could be fulfilled by using the gmail javascript API.

For other specific websites using the web notifications API (but not other chrome apps/extensions), you could possibly write an extension with a contentScript with a run_at value of document_start to override and intercept (ie "monkeypatching") the built-in Notification object. Obviously doing this on every web page isn't great (introduces a little delay), so you'd want to have a reasonable "matches" value containing match patterns just for the sites you're interested in.



On Wed, Jul 27, 2016 at 10:30 AM, Kristofer Johansson <kristo...@gmail.com> wrote:

Hi, Everone!


My question was originally intended for Google Chrome and I posted it on Stackoverflow but thus far it has not yielded any answers. I'm not sure Stackoverflow (or this place) is the right place to ask but I figured you guys are competent extension programmers with vast experience using the APIs so you should know if what I want to accomplish can be done on Google Chrome or if I would need to build my own Chromium to make it happen. If I am in the wrong place I apologize and ask you to point me in the right direction.


Here is my original post from Stackoverflow:

****************************

I have no previous experience with programming Google Chrome plugins which is why I am starting here to see if what I want to accomplish is possible/reasonable. I do however have a pretty broad experience in programming in general.


What I want: I want some kind of "trigger" to go off when a new Chrome Notification (you know these little pop ups above the system tray) is popping up. I want to execute some script/code depending on what information the notification contains so that I for example could have an alarm go off if I receive an email from a certain user with a certain key word in the subject and get this in a pop up from my Gmail Notifier extension. This is however just an example and I have a bunch of ideas for different notifications from different extensions and websites so don't get caught up on that particular example.


When I look at the Chrome Notification API I see that there is a getAll method that supposedly is getting all the "notifications in the system" but I do not find any Event for new notifications. I suppose a possibility would be to poll with getAll a couple of times per second (it needs to be really fast for some implementations I have in mind) but it feels very tacky.


Is there any way to easily access new Notifications programatically in Chrome? (I'm open to all solutions, programming languages and such...)

******************************


What do you guys say?


Thank You,

/Kris



--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Antony Sargent

unread,
Jul 28, 2016, 12:29:50 PM7/28/16
to Kristofer Johansson, Chromium-Extensions-Announce
Injecting content scripts comes with a small minimum overhead cost in terms of time and memory usage even if the content script doesn't actually do anything. In most cases it's probably not very noticeable, probably less than a few tens of milliseconds, but you still want to make sure that you don't pay that cost if you don't need to by setting the "matches" entry for your content scripts so that they only run on the pages you actually care about. 


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Kristofer Johansson

unread,
Jul 28, 2016, 12:35:23 PM7/28/16
to Chromium-Extensions-Announce, kristo...@gmail.com
OK, I suspected as much and yes of course I'll make sure it's only running where need be. You wouldn't happen to have any code example of how to intercept (or more like 'tap into') notifications to jumpstart the project, would you? (Or monkeypatching of some similar object for that matter...)
Thanks!
/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Kristofer Johansson

unread,
Aug 1, 2016, 3:38:04 PM8/1/16
to Chromium-Extensions-Announce, kristo...@gmail.com
I have been messing around experimenting with this for a while now and have really reached to the point of understanding that I at least can question if it is even possible to accomplish this.

The reason is the "isolated world" execution style of the JavaScript.
Antony (or anyone else that might know): Is it actually possible to override for example the Notification ctor with a content script? From what I understand I can't really affect anything happening in the webpages Java environment but rather only manipulate the elements on the webpage and add JavaScript functionality to execute seperately from the web pages'. In fact, I don't even know how one possibly could detect any new objects at all being created dynamically by the webpage without active polling.

I'm new to this and very open to the fact that I may be way off here. Any input guys?
/Kris

PhistucK

unread,
Aug 1, 2016, 4:00:18 PM8/1/16
to Kristofer Johansson, Chromium-Extensions-Announce
Yes, you can, it just looks a bit hacky.
1. Define your override script in a web_accessible_resources section in your manifest.
2. Add your override script to the page (document.createElement("script") and friends).


PhistucK

/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Kristofer Johansson

unread,
Aug 2, 2016, 3:54:26 PM8/2/16
to Chromium-Extensions-Announce, kristo...@gmail.com
Thank you for your response!
Now I am certainly one step closer. I get it to work on a simple test page that I made but not on any other pages I try it on online.

Like PhistucK suggested I put the new override constructor in a .js-file that was declared in web_accessible_resources, then I initiated that from a content_script (that is being run at document_start) like this:
*************
var s = document.createElement("script");
s.src = chrome.extension.getURL("overlay.js");
(document.head||document.documentElement).appendChild(s);
*******************
Then I made an override constructor as such (in overlay.js):
*********************
var bind = Function.bind;
var unbind = bind.bind(bind);

function instantiate(constructor, args) {
return new (unbind(constructor, null).apply(null, args));
}
Notification = function (Notification) {
MyNotification.prototype = Notification.prototype;

return MyNotification;

function MyNotification() {
console.log('notification override working from overlay');
//chrome.runtime.sendMessage("testing from overlay");
var note = instantiate(Notification, arguments);
var args = arguments.length;
var arg = arguments[0];
mySendMsg(note.title, note.body, note.timestamp, note.data);

return note;
}
}(Notification);
**********************
This works perfectly like I want it (Notification is displayed as normal plus mySendMsg is being called every time the webpage tries to send a Notification) when I try it on a simple test page that I store locally. BUT when I go to any other webpage to try to inject it, it seems as though my override ctor is not being used by the webpage.
Let's take https://goroost.com/try-web-push as an example.
Naturally I have made sure that the 'matches' parameters are correct and via console messages I can see that indeed my injected .js is being invoked but when I try to actually send the notification, my override ctor is apparently not being called.

So: Is there possibly any other way that these pages can send messages without calling the Notification constructor? Or could it be that they have some kind of protection of their code that prohibits ctor override? Any Ideas`?

Thank you for your time!
/Kris



PhistucK

/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

PhistucK

unread,
Aug 2, 2016, 3:58:47 PM8/2/16
to Kristofer Johansson, Chromium-Extensions-Announce
Yes, this one is indeed tricky, because it is not the web page that creates the notifications - it is a service worker (push notifications are handled by service workers).

I guess the current content script API is currently lacking in terms of service worker injection.
Generally, for feature requests, you can search crbug.com for an existing issue for this feature request and star it. If you cannot find one, you can file a new issue at new.crbug.com.
(If you do find one, do not add comment like +1 or "me, too", it only hinders the development and it does not make the engineers implement the feature more quickly.)



PhistucK



PhistucK

/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Kristofer Johansson

unread,
Aug 3, 2016, 5:40:44 AM8/3/16
to Chromium-Extensions-Announce, kristo...@gmail.com

Thanks, well that was certainly a brick wall. 
How do you determine if the page uses a service worker for notifications?
/Kris


PhistucK



PhistucK

/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

PhistucK

unread,
Aug 3, 2016, 7:40:20 AM8/3/16
to Kristofer Johansson, Chromium-Extensions-Announce
I guess your can intercept the service worker registration code as well and execute your own script that loads their script...
navigator.serviceWorker.register, I think. Just search the web for the API and override it as well.

This indeed gets overly complicated at this point. :)


PhistucK



PhistucK



PhistucK

/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Kristofer Johansson

unread,
Aug 4, 2016, 5:10:50 AM8/4/16
to Chromium-Extensions-Announce, kristo...@gmail.com
Meh... This feels like it is getting overly complicated. 
The reason I went down this path (interception/override) was that it might be easier then getting the output from the actual Notification. Since it has not yielded the results i was looking for so far I am now inclined to drop this ball and pick up the one I first had my eyes on. I'm trying to modify the chromium source and make my own custom build instead.

If I change my mind again and try intercepting the serviceWorker instead I'll be sure to post my results back here.

Thank you for all your help,
/KrisJ


PhistucK



PhistucK



PhistucK

/Kris
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
Reply all
Reply to author
Forward
0 new messages