Chrome - detect closing a browser or closing a tab

I use the following two listeners on my page when a user closes a tab or window in chrome, but they don't seem to work

chrome.tabs.onRemoved.addListener(function (integer tabId, object removeInfo) {alert("Haooooo")});

chrome.windows.onRemoved.addListener(function (integer windowId) {alert("Haooooo")});

But the following function detects closing a window or closing a tab, but also starts when updating. Does anyone have a way to detect a browser / tab only for Chrome. I am not looking for this to work in any other browser. Finding a solution only in chrome

window.addEventListener("beforeunload", function (e) {          
          var confirmationMessage = "See you later" ;
          (e || window.event).returnValue = confirmationMessage;
          return confirmationMessage;
}
);
+4
source share
1 answer

Your syntax is invalid. It should be

chrome.tabs.onRemoved.addListener(function(tabid, removed) {
 alert("tab closed")
})

chrome.windows.onRemoved.addListener(function(windowid) {
 alert("window closed")
})

However, these apis will not work on regular web pages, only extensions.

+4
source

Source: https://habr.com/ru/post/1625396/


All Articles