Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

Sharing localStorage between web app and extension

I have a "Notifications" system which, with user permission, pops up a new Notification complete with optional sound effect to inform users of things happening.

However there is a limitation that means that notifications won't appear when there are no browser tabs open, since there are no instances of the "check for notifications" JavaScript code running.

To remedy this, I want to make a browser extension, first for Chrome and then potentially for other browsers.

Multiple tabs are already handled by using localStorage to negotiate mutexes and pass data across tabs. The result is that they all update simultaneously and only one of them triggers the desktop notification / sound effect.

Is it possible for the browser extension (again, Chrome first, other browsers are considered less important for this) to read from the same localStorage as the web app, and thereby join in the communications?

Upvotes: 2

Views: 952

Answers (1)

Xan
Xan

Reputation: 77523

Is it possible for the browser extension (again, Chrome first, other browsers are considered less important for this) to read from the same localStorage as the web app, and thereby join in the communications?

Yes, but only while the page is open. A content script will share its localStorage with the page, and there is no API to access it otherwise.


That said, I agree with the comment from Patrick Evans: an extension is a cumbersome solution ONLY for notifications. The new Push API is a great alternative (also works on mobile!), and this Google codelab is a great introduction.

Upvotes: 1

Related Questions