Close event for chrome.app.window - google-chrome-extension

When a chrome app window is closed, is it possible to detect the event? and do an action before the window is closed?

chrome.app.window.current().onClosed allows you to register an event listener for when a window is closed.

Vincent's answer will work in some cases, but the documentation warns that some chrome api functionality will be lost by the time the onClosed event is fired.
Note, this should be listened to from a window other than the window
being closed, for example from the background page
To listen for this event from the background page, do something like this:
chrome.app.window.get(windowIDUsedToCreateChildWindow).onClosed.addListener(function(){...})

Related

Is there any way to detect javascript postmessages using chrome extension?

Does anybody sees a way to listen to cross iframe/window communication that is being done using the javascript postMessages ?
Best would be using the chrome.debugger in the background page of an extension. I see that I can listen to HTTP-Traffic using the "Network.requestWillBeSent" and "Network.responseReceived" events. But I would like also to listen to the internal communication on the page.
If my question isn't clear enough, please let me know, I'll try to clarify it more.
Thank you in advance !
Run a content script and bind a message event listener. This event listener will then be invoked postMessage is called.

Chrome Extension Event Page communicating with external native host

I'm building a Chrome Extension that, through a native host, should simulate a key press. I can connect to my native host to check that it's there etc in the popup I've created, but in my event page script, should I just connect to the native host? It says on the Chrome Extension Developer Page that:
Event pages are loaded only when they are needed. When the event page is not actively doing something, it is unloaded, freeing memory and other system resources.
So if I want it to run "forever", i.e. listen to the native host and simulating a key press whenever it gets a "ping", how should I do that? The page says I should create events for that, but do I just listen to the port then?
Thanks,
Johan
As long as there is an open port opened with connect(), an Event page will not shut down.
If you think it's going to be like that most of the time, don't bother with Event pages and put "persistent": true (or nothing) to make a normal background one.

Relying on 'disconnect' event in socket.io/express.io when using Angular's single-page app engine

How is it possible to add a 'disconnect' event whenever I leave a page that is intercepted by my single page application engine?
I have certain events set up (kind of like streaming events on YouTube) and each requires its own socket connection. I now set up a single-page engine on Angular that allows switching between these events.
I used to rely on the 'disconnect' event to let me know when someone has left the event, but they don't work anymore - the socket never shuts off if I leave via my single-page navigation.
Is there any way to configure the socket to still disconnect?
My less-than-ideal approach was to create a new instance of the socket connection on a controller of every page to which I can navigate from the event page, and forbid multiple connections.
If anybody has a better idea, I would love to hear it.
Thanks a lot.
You may call it manually since you are in control of page navigation - whenever client is navigating away from one page, call
socket.disconnect();
socket.close();
This will ensure that when client is navigating away from one page the socket gets disconnected before that.

Registering Websocket connections with Event Pages

In the initialization for my Event Page, I'd like to create a WebSockets connection via Socket.IO and wake up the page whenever data comes in on that connection.
I suspect this isn't possible and I'll have to use a Background Page, but is there any clever way I can use Event Pages instead so I don't make my users incur the perf hit?
I'm pretty sure you can't do this simply because Event Pages are designed to unload when not in use. Whenever the page unloads, it will close any WebSocket connections. If it never unloads, then you effectively have a persistent background page.

Pusher disconnect & timeout

will my server be notified about disconnect on the client side?
I assume the answer is yes if the disconnect happens explictly like below.
pusher.disconnect()
however what happens if the user simply closes the browser?
Another thing is there a way to notify the server that a certain channel has not been in use by the client(s) for some while?
The connection states documentation shows how to bind to connection state changes.
however what happens if the user simply closes the browser?
This really depends on if the browser calls webSocketInstance.onclose so the Pusher JavaScript library is informed before the browser is closed. You could always detect this yourself using window.onbeforeunload, window.onunload or the addEventListener versions.
Another thing is there a way to notify the server that a certain channel has not been in use by the client(s) for some while?
You can use WebHooks so that when a channel becomes vacated your app server will be informed.

Resources