Reputation: 215
I am trying to make a chrome extension which asks for conformation when chrome is being closed, and when depending on the response, will either close Chrome, or keep it open, but am unable to find a suitable API. I tried the chrome.windows.onRemoved.addListener
method, but that does not stop Chrome from closing. How should I implement this? Below are my attached files:-
manifest.json
{
"manifest_version":2,
"version":"1.0",
"name":"STFU",
"description":"Asks before closing ;)",
"browser_action":
{
"default_icon":"icon.png"
},
"background":{
"scripts":["background.js"]
},
"permissions":[
"tabs"
]
}
background.js
function yo(){
chrome.windows.onRemoved.addListener(function(windowID){
var answer = confirm("Are you sure you want to leave ?");
});
}
chrome.browserAction.onClicked.addListener(yo);
I even tried the window.onbeforeunload
function, but that does not do anything.
Upvotes: 2
Views: 2348
Reputation: 77571
You can't do it from the background page. In fact, there is no event that you can process when the browser is closed, and no way to cancel it. Extensions are not supposed to be able to prevent it.
You could potentially inject onbeforeunload
handlers into all tabs, but that's clunky at best and may not work depending on whether the user interacted with the tab's content.
Upvotes: 4