Event onBrowserClose for Google Chrome Extension? - javascript

I am developing an extension for Google Chrome. My background script, everytime, authorizes on a server that uses the XMPP API, and subscribes for a PubSub node. I need to unsubscribe on the exit, otherwise the dummy subscriptions will remain on the server. Is There any onBrowserClose event in the Google Chrome Extension APIs?

There is no such event in the Chrome Extension API.
There is however a chrome.windows.onRemoved event that fires each time a window closes. I figured you could check in this event if you closed the last window, but unfortunately due to the asynchronous nature of Chrome this doesn't work.
What I tried was running a simple AJAX request in the onRemoved event handler. The AJAX request never got to the server, as Chrome had already closed before running the event (or just disregarded it).
Making the final answer be: No, currently you can't, as far as I know. You might want to star the following bug report at http://crbug.com/30885 to get noticed on updates.

If you catch the case when the number of open tabs is 0, you can treat that as a Chrome onClose event. In my case, I have to cancel a desktop notification before Chrome closes because it was crashing otherwise.
This is how I did it:
1. Initialize a variable num_tabs by using the following:
chrome.tabs.getAllInWindow( null, function( tabs ){
console.log("Initial tab count: " + tabs.length);
num_tabs = tabs.length;
});
2. Increment num_tabs when a tab is created:
chrome.tabs.onCreated.addListener(function(tab){
num_tabs++;
console.log("Tab created event caught. Open tabs #: " + num_tabs);
});
3. Decrement num_tabs when a tab is removed and run your browser onclose event handler if num_tabs = 0
chrome.tabs.onRemoved.addListener(function(tabId){
num_tabs--;
console.log("Tab removed event caught. Open tabs #: " + num_tabs);
if( num_tabs == 0 )
notification.cancel();
});

Adding a browser close event is a pretty frequent request. Star http://crbug.com/30885 for updates. And read the bug report for a clever hack to detect when the browser is shut down via a key press.

This one works for me:
chrome.windows.onRemoved.addListener(function(windowId){
alert("!! Exiting the Browser !!");
});
It takes chrome.windows rather than chrome.tabs.

TL;DR: Try window.onunload event, it works for some cases.
As it was mentioned before we generally can't handle something like onBrowserClose event and prevent browser from closing. But for some cases we can use window.onunload event for doing something synchronously, and it does work if it really synchronously.
From my experience you can at least:
Save some information in (for example logs) in HTML5 localStorage (which is synchronous).
Call some asynchronous chrome extension API functions but you can't get a result. (It works for me! Yay!)
Perform synchronous XMLHTTPRequest (yeah, sometimes it works).

Related

BroadcastChannel.onmessage not working in firefox

I need to share some information between 2 tabs of the same browser pointing at the same site. I'm using the BroadcastChannel.onmessage event handler as detailed here:
https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/onmessage
In the sender tab javascript code I defined:
var bc = new BroadcastChannel('my_bc_channel');
bc.postMessage(i); //where i is simply the line number i want to share
And in the receiving tab:
var bc = new BroadcastChannel('my_bc_channel');
//then use this to receive the incoming messages:
bc.onmessage = function (ev) {
last_line = ev.data
}
My code works fine in Chrome, but it does not do anything in Firefox (latest version 70 freshly installed under windows). The compatibility chart in the link I gave above says it should be working from Firefox version 38.
The thing is, I'm not sure how to debug this. I don't have any error messages in the console. I don't know if it's the sender code that does not send anything. But clearly the receiving code is not triggered so I guess the .onmessage event is not detected. Where can I see in the javascript console if the message is sent ?
I opened a bug report:
https://bugzilla.mozilla.org/show_bug.cgi?id=1600512
Seems BroadcastChannel.onmessage does not work properly when you host your files locally on your hard drive, as I did in my example above. It works otherwise.
It might be that the channel "in the receiving tab" has it's onmessage handler set only after the other tab has broadcast the message, and the message is lost.
You might want the "sending" tab to also set a message handler, and wait for the other tab to broadcast a message that it is ready to receive, and only then start the broadcast.

Inconsistent execution of executeScript in Cordova inAppBrowser on iOS

I'm having some trouble with some inAppBrowser behavior in my cordova app. Here's the code:
var codePass = fooCode;
var executeScriptFunc = function(event) {
ref.executeScript({
code: codePass
}, function (value) {});
ref.removeEventListener('loadstop', executeScriptFunc);
};
var ref = cordova.InAppBrowser.open(fooObject.link, "_blank", "location=yes,enableViewportScale=yes");
ref.addEventListener('loadstop', executeScriptFunc)
The strange thing here is that the code works perfectly every time when emulated. It opens the browser and executes the script no problem. But when I try it on my actual iPhone device, it doesn't always work. The script executes maybe every other time. But it's never even that consistent.
Both the emulator and iPhone are using iOS 9.3.4. Any ideas?
If the site inside the inAppBrowser happens to be served via HTTPS, the callback for executeScript() will not work if the site employs a Content-Security-Policy HTTP response header that does not contain the gap: or gap-iab: schemes for the default-src directive. These are needed because execution of the callback function on iOS relies on an iframe that gets added to the page.
You can check whether this is the root cause for the problem by opening Safari’s Web Inspector for the inAppBrowser—it has a separate Web Inspector instance, independent of the parent application that opened it—and look out for a corresponding error message in the console. Note that you should open the console before executeScript() is run, or otherwise you might not get the error message.
Make sure also that you don't have other event handlers firing at the same time during your polling.
I had multiple pollers all firing every second and that's when I ran into the issue.
After changing the polling time so they all fired at different times, the issue went away.

Chrome Extension API not firing onChanged event

I am using chrome.downloads.onChanged.addListener to find out when a download completes.
The callback does not fire sometimes, and there is no apparent reason for it.
The code looks like this
chrome.downloads.onChanged.addListener(function (downloadDelta) {...});
and it is in background.js and file is mentioned in background section of manifest. I added console.log(downloadDelta); at the very begining of callback, but it is not fired.
The API is mentioned here
EDIT: Test extension
Most probably this has to do with peculiarities of Event pages - something not working properly when your page gets unloaded.
This is easy to test - remove "persistent": "false" from the manifest.
That said, your test extension does not violate any Event page recommendations and chrome.downloads API does not list any incompatibilities. Therefore, 2 scenarios are possible:
It's quite possible that you are misinterpreting results due to the page being unloaded. For instance, if the page gets unloaded between the callback executing and you opening the devtools for it - the console and all local state will be wiped.
To test for that, make sure to write your diagnostics to persistent storage - chrome.storage API is one option for this.
If you are 100% sure the above is not the case, there may be a bug related to event pages and chrome.downloads. In that case, it should be reported.

AppCache events firing late - WebkIt (Chrome and Safari)

In my web app, I use the applicationCache feature. I register to the appCache events like
appCache.addEventListener(<event_name>, handleCacheEvent, false);
function handleCacheEvent(e) {
console.log('handleCacheEvent appcache event: '+ e.type+' appCache.status: '+appCache.status);
}
(in event_name I register to all events - 'cached', 'checking', 'downloading', etc).
However, in Chrome logs, I see these logs:
Application Cache Progress event (1 of 15)
...
Application Cache Progress event (15 of 15)
at the very beginning of the run, then some of my JS business logic, and after few seconds I see my logs
appcache event: progress appCache.status: 1...
It seems that the event listener are not triggered right when the status of the appCache changes, but rather later at some point. Is it so?
Yes, I've seen the same thing. When I add event listeners to those events and log them, my log message always appears after all of the browser's log message. I think this is because the browser starts loading the appcache before the contents of the page are done loading, since the appcache does its work in the background. This still gives you a chance to handle appcache events programmatically while letting the browser do its thing to honor the specification.

beforeunload event on page refresh in firefox

I am currently using faye for pubsub and am disconnecting the client on the beforeunload event.While it disconnects during tab close during page refresh it throws the following error :
The connection was interrupted while the page was loading
The code is
window.addEventListener('beforeunload',function(event){
fayeClient.disconnect();
event.preventDefault();});
Is there a way to stop firefox from closing the connection before the call completes.The above code works perfectly in chrome
How can I prevent a page unload with jQuery?
way down in the comments it says:
event.preventDefault() doesn't work in this case, presumably because modern browsers don't want malicious coders to hijack the window and make it un-closable? – yochannah May 9 '13 at 8:45
I dont believe it is possible to delay it longer then it takes for that code to execute, excluding any async returns and timeouts.
So, to hack this. Call your disconnect, then make a synchronous call to a file which does
<?php
sleep(1);

Categories