How to run a function on close in a Chrome App? - javascript

I'm having an issue where closing my Chrome App too quickly after completing a stage corrupts the save data. This is because it's in the middle of saving some data.
Is there any way to keep the Chrome App open for a few seconds after the user presses close or alt+f4? Or another solution that maybe has a popup telling the user that the app is saving and will close automatically?

You can use the chrome.runtime.onSuspend.addListener( function ) method to specify some code to run when the user quits the app. As stated in the chrome documentation, any code running or triggered in the background/event page will keep the program alive for a short time to allow these processes to end. Remember to register this event listener method in your background/event page before the user has a chance to quit.
Additional information here:
https://developer.chrome.com/extensions/runtime#event-onSuspend

Related

Detecting wake from sleep on phone browser with Javascript

I'm working on a Ruby on Rails application with Action Cable. Watching the logs, I've noticed that when I put my phone to sleep that the WebSocket is closed automatically, so further updates are not received. However, on wake (directly to the page, or opening the browser again and giving the tab focus) the connection is immediately re-established.
I want to refresh the page at this moment so that it's up-to-date. The "focus" event is not useful, as it will trigger whenever desktop users bounce between windows or tabs.
What event can I use to reload the page on wake? Something must be available, as Action Cable is reconnecting (though I realize it may be using the focus event as well and simply checking if a connection exists or not).

Communication between application windows and single popup window

I have a web application running in one or more browser tabs/windows. I also have a SharedWorker, which is connected to every single one tab/window and is also communicating with some server over websocket. To this point everything works as expected and without any problems.
There, I also have a single popup window (opened automatically or with user interaction, it doesn't matter) with some SIP/WebRTC plugin. After it's opened, I can communicate with it over the same SharedWorker as everything else and that's OK.
SharedWorker also keeps (or, at least, is trying) information about every connected tab/window - based on connect event in worker and beforeUnload in every other tab/window. Not the best solution, but only one I found working.
Everything runs on the same domain, in the same location.
Now, I can't find any solution to:
get the popup window handle from other tab/window than its opener, nor it's not possible to transfer handle to SharedWorker
react to close of popup window every time, cause in some point that window worker port gets lost
focus popup window from every other tab/window, cause there's no handle to it

Browser Close event in IE 7 and IE 8

I know there are many posts in the forum on browser close event. But still thought of posting it. How do I get to fire the onbeforeunload event correctly all the time. As I see, in some client browser it works and in some it does not.
When I used the onbeforeunload event, I used to get IE warning, saying do you want to run ActiveX scripts. To avoid that I used the following code
<!--doctype html>
<!-- saved from url=(0014)about:internet -->
Can anyone let me know the standard method to be used. All I need to do is when user closes the browser without clicking on sign out button, I need to update the logout field in DB for that users login entry
Any help is appreciated.
That's not a reliable (or safe) way to end an authenticated user's session.
What if they don't close the browser? The user is still logged in and
if someone else uses that browser they could use that session?
What if a user opens multiple tabs then closes one of them? They
would be logged out incorrectly.
The correct way to end an authentication session is to let it expire after several minutes of inactivity. On each new request you reset the timer and, independently, a recurrent service/process ends those sessions that have exceeded the activity timeout.

Refreshing same browser window from windows run command

I have one web application. When client enters the address and after successfully logging in. He gets the home page with some data over the page. Now when any third party tool or from run command if i give the same url on which the client is with changed parameter values, i want the same browser window to be refreshed with updated/changed values without opening the new browser window.
Whats happening now is that when i'm triggering the url from different source, its opening in new browser window. Plz help me out with few suggestions.
Ars.
The best solution would be for the web application to poll the server and refresh itself when there is an update.
Your command line tool can then contact the server and update the parameters directly, and the application in the user's browser will pick up those changes automatically.
Attaching to a running Internet Explorer instance is also possible, for example see this article: http://www.codeproject.com/Articles/9683/Automating-Internet-Explorer, or the Selenium project at http://seleniumhq.org/. But you're almost certainly going to make it easier for yourself by changing your application to refresh automatically.

Call onunload function from iframe

I am working on a multiplayer chess game as a Facebook app.
If one player leaves the game by closing the browser the other player should get a notification. So if one player closes the Browser, a unlink function should be called to unlink the player. This works fine with onunload outside Facebook.
The problem is, that the Facebook apps are loaded in iframes and the onunload event doesn't work there.
So I need a way to call a function inside an iframe when a user is closing the browser.
This is probably not the answer you are looking for but "logging off" on unload will never work reliably. For an extreme example, consider the case where the browser crashes or is killed via the Windows task manager. So you better implement an additional mechanism to detect whether a user left. Typically this is done by sending a request to the server periodically. If this request isn't received for a significant time (meaning something that cannot be caused by a slow connection or other hiccups) you unlink the user.
That said, I tested Firefox 4 and MSIE 8 and both correctly fire unload event on the frame if the tab or the browser is closed. Chrome 12 doesn't do that, that's probably the browser you have been testing with. I consider it a Chrome bug.

Categories

Resources