Reputation: 8067
I have created a couple of Custom URI Schemes namely start and stop for launching and stopping desktop applications (executable JAR files) from browsers. I want to execute the start script on browser start-up and stop script on browser close.
1) For running the start script on browser starting, I have planned to set the browsers default homepage to the start script. Please advice if there are any better way/alternative way to achieve this.
2) I am not sure how to handle the browser close scenario: I need to run the stop script on browser closure. Please suggest on how to achieve this.
Upvotes: 1
Views: 285
Reputation: 766
You can do the following:
write an java app, which first executes your start script, then launches an new process starting your browser
try {
Process p = Runtime.getRuntime().exec("firefox")
} catch (IOException e) {
e.printStackTrace();
}
(at least this works on ubuntu, maybe you need to write down the path to firefox.exe (or chrome.exe) if you are on windows)
then add a kind of "process-exit-listener" to the process (check this: Detecting Process exit)
and when your exit listener triggers run your exit script
a listener is much better (if you think about performance) than polling the state every (e.g.) second. imagine your browser is open for ~2h and you check every second → 7200 checks if its closed.
Upvotes: 1
Reputation: 8783
Though I don't know how to catch the browser's closing in real time, you could do it using the polling technique.
For example, through a RSS/ATOM: Suppose you public a RSS/ATOM source from your own server with a TTL of 60 seconds. Once started, your server will be polled every 60 seconds while the browser is active. But if your server detects that more time has passed since the last poll (let's say 70 seconds), that would mean that the browser has gone off.
Of corse, you'll have to assume a 70 secs delay since the browser is closed and the server realizes, but I don't think that would be a great deal.
Upvotes: 1