Trigger javascript even if tab closed - javascript

I know how HTML5 notifications work. And I know how to use them in production. But what I can't understand is, the behaviour of Facebook: Even if you close all tabs of facebook, when you receive message, desktop notification appears. Doesn't matter if you use Mac or PC.
How is it possible to execute javascript when there is no open tab (session)?
How does facebook send html5 notifications even if tab closed?

Related

Google Chrome Extension, closing Persistent background page

I have a chrome extension which has a persistent background page in order to stream audio even when the user closes chrome. What I would like is to implement an option for the user to disable audio streaming after chrome has been closed. Right now I am detecting when all chrome windows have been closed and pausing audio, however chrome remains 'open' in the task manager and system tray (note that running in background chrome setting is enabled). This stops audio playback but the chrome process remains open in task manger and the icon remains in the system tray. By contrast if I completely disable the extension and then close chrome, the application closes completely and no system tray icon remains.
This Question suggests that only a user can explicitly close chrome if there is a persistent background page, My goal isn't to close chrome explicitly but simply prevent MY extension from being the one keeping chrome open (depending on user specified options), therefore an event page doesn't seem to be a good fit.
Is there a function call or some other programmatic way I can close my persistent background page once all windows have been closed, if that option has been enabled? Or am I stuck with the chrome application staying open because the background page is persistent?
I've found an answer to my own question so I'll post it here. In order to have a background page you do not need the "Background" permission declared in your manifest. This permission is what allows the extension to stay open after chrome has closed (and potentially open before chrome?) rather than being necessary to have a background page. So a persistent background page will close with chrome if this permission is not set, and will stay open even after chrome is closed if it is set.
Bearing this in mind, the solution for me was to set the "background" permission to optional, and enable it if I wanted my extension to stay open after close, then remove the permission if I didn't want it staying open after close (the user now has an option to toggle this).

Tab/Browser closing only eventlistener (no refresh, page unloads)

I am making a website with a user system and a login system.
I am using simple PHP scripts with mysqli functions to manage the users.
I can log in and I can log out.
My only problem seems to be that if a user closes the tab/browser, the column in the database describing if the user is logged in or not (I need that to prevent double logins because we have multiple GUIs apart from the website) is not getting reset to 0 (meaning not logged in).
I have a logout button that send me to the logout script in which I do reset the userSignedIn column, but i cannot find a solution to how to react to the tab closing.
Browser: I am using Google Chrome but would like it to work on most modern browsers.
PHP: Version 7.3.12
I have already seen this question and it's answers: Detect browser or tab closing
I have tried this solution, but apparently it is an outdated one or I am just not getting it right. I also heard it just works if there is only one tab open.
https://stackoverflow.com/a/5593734/12614408
Is there a way in 2020 to have an eventlistener that listens to tab/browser closing and not at refreshes or page unloads?

Detect tab close on chrome mobile android browser

I tried beforeunload and onunload events but they get called only when user navigate to a different page via address bar not when user close the chrome android browser tab.
I want to send analytics code only when user has closed the page via tab also.

Is there any way to avoid the chrome extension popup from fading out when the user clicks anywhere else on page or changes the tab?

I am currently working on developing a chrome extension using AngularJs. I am beginner to this development. I wanted to know if there is any way using which i can avoid the chrome extension popup from getting closed when user clicks anywhere else on webpage or user switches between the tabs. I understand that the extension gets opened only when it gets focused but i needed it to be persistent and not fade out.
Please help!

Detect hide/leave event of mobile phone browser

I've surveyed most of the questions on the web but this still gets me confused....
My question is, how to detect hide/leave event of a website tab in mobile phone browser, my situation is NOT about closing the tab(like pressing the X button at the up-right or up-left corner), and also NOT about closing the whole mobile browser app directly.
(Previously, I have a webpage with a websocket connected to receive the encoded audio content, and then use AudioContext to play the sound)
Below are the two situations I've encountered.
When I press the HOME button and the mobile browser app hides.
When I click a url in the Messenger app or Facebook app of my iPhone, and press the Back button to go back.
the url link is opened in the in-app browser which made by Facebook directly, and when I press the back button on the left-up corner, the view jumps back to the Messenger window or the Facebook timeline.
In these two cases, I have listened beforeunload (and also pagehide event for iOS), but neither one is catched. And I have found that the websocket connection is still connecting and the audio is still playing. It seems like the whole page is still running somewhere(but you cannot see). I also tried listening to a div :visible, but it is still giving me true when I pressed the home button or leave the facebook in-app browser.
Is there still any way to detect these two kinds of situation? I want to turn off the streaming sound and also close the websocket connection.
Just found the answer....
Page Visibility API https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

Categories

Resources