0

I just want to write a small script that does Foo() whenever I receive a push notification from my browser(chrome). I tried to find some stuff out there but all I found was how to send said notifications, but I only want to receive them. Anybody can lead me in the right direction?

  1. I do not own the backend that sends the notifications
  2. There is no frontend, its notification from the browser
  3. I am currently trying with a browser extension, but cant access the notification. I do not know if browser extension is the way to go, that should be clear from my initial post.
E_net4
  • 27,810
  • 13
  • 101
  • 139
mehlichmeyer
  • 154
  • 1
  • 18
  • 1
    What's the actual problem? What works and what doesn't? Do you own the frontend that receives the notifications and the backend that sends them? – Guerric P Apr 13 '21 at 19:45
  • "just want to write a small script" Where? IN browser extension? your webpage? You want to listen for notification sent from other sources? .... And in mid time go over Notifications_API https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API – ikiK Apr 13 '21 at 20:35
  • @ikiK I wrote a browser extension to test but I can do either, but couldnt find anything to catch the notification. I want to catch notifications from other websites. I do not want to send notifications myself. WebsiteX sends a notification -> My extension catched said notification and shows me the json or whatever. Thats it. I already looked at notifications api, but that wasnt really helpful since it focuses on pushing notifications. – mehlichmeyer Apr 14 '21 at 09:59
  • @GuerricP There is no frontend, its a notification from the BROWSER, and no I do not own the backend. The problem is that I cant figure out how to access the notification from my browser extension. I dont even know if browser extension is the right place to catch it. – mehlichmeyer Apr 14 '21 at 10:00
  • @mehlichmeyer does this answer your question? https://stackoverflow.com/questions/57523910/intercept-html5-web-notifications-in-a-browser-environment/57572671#57572671 – Guerric P Apr 14 '21 at 10:02
  • @GuerricP that looks promising, I will try it tonight and let you know if it works. Thanks a lot! – mehlichmeyer Apr 14 '21 at 10:04
  • @GuerricP that didnt work out. I think the key point here is that the notifications comes from another backend. Maybe its not even possible with Javascript, but there must be a way for me to intercept these notifications.. – mehlichmeyer Apr 15 '21 at 07:56
  • That probably means the Notifications you want to intercept are actual push notifications, then what you try do to is impossible because you can't hook into the service worker, and you can't register your own service worker without overriding the existing one. But here I'm making assumptions because your question lacks details, you should edit it to add some. *There is no frontend, its notification from the browser* that doesn't make sense, the browser doesn't send notifications on its own, only web pages do. – Guerric P Apr 15 '21 at 09:19
  • @GuerricP honestly I dont know how to clarify the question anymore, it makes sense to me. Other backends send the notification but the browser shows them. But it doesn't matter, I think you are right and what I want to do is not possible. If you want you can post that as the answer and ill accept it! Thanks for you time and help. – mehlichmeyer Apr 15 '21 at 13:44
  • @mehlichmeyer I've posted an answer, I hope it's complete and comprehensive enough – Guerric P Apr 15 '21 at 17:16

1 Answers1

5

If the question is about intercepting notifications that are generated on the web page with the Notification API, this answer explains how: Intercept HTML5 Web Notifications in a browser environment

To sum it up, it consists in the creation of a Proxy as a wrapper of the native Notification in order to hook into its constructor and execute arbitrary code.

If the question is about intercepting Push Notifications then it is impossible because they are based on the Service worker. You can't hook into the service worker, and you can't register your own service worker without overriding the existing one (which will break the web page), as stated in the documentation:

If there is an existing service worker available, the new version is installed in the background, but not yet activated — at this point it is called the worker in waiting. It is only activated when there are no longer any pages loaded that are still using the old service worker. As soon as there are no more pages to be loaded, the new service worker activates (becoming the active worker).

Guerric P
  • 30,447
  • 6
  • 48
  • 86