3

I'm writing a very simple Chrome extension to automatically clear my history as this isn't a built in feature. I've got it functional but just want to confirm I've done things that make sense before I wrap it up.

First - is there a reason to do a chrome.history.deleteAll? This doesn't actually appear to interface with URL browsing history, instead you need to use chrome.browsingData.* for that. Should I do the former anyway?

Next - after searching and searching, there appears to be no way to execute a function on shutdown. The closest I got to real info on this was someone saying when Chrome quits it terminates all extensions without prejudice. Am I missing a way to clean up history on shutdown?

Finally - as I can't cleanup on shutdown, I'm cleaning up on startup, using window.onload in the background page. chrome.runtime.onStartup did not reliably work for me while window.onload did - is this an okay way to do things?

Thanks in advance for any help.

Mark
  • 183
  • 5

2 Answers2

4

you are right, there's no way to execute anything on Chrome shutdown. Chrome doesn't have an onClose event. Not only literally, but metaphorically speaking: When Chrome closes, it doesn't wait for any extension to close.

You can hang from chrome.windows.onRemoved and wait for the last window to close, but if Chrome is shutting down, it's not guaranteed your extension will have time to run it. Same thing with onSuspend, or whatever you find.

So, if your implementation works, it won't be guaranteed to work in other Chrome.

About chrome.history.deleteAll, I have the same problem, and will try with chrome.browsingData, a more modern and complete way to deal with browsing data, including history.

Alejandro Silvestri
  • 3,706
  • 31
  • 43
1

This document lists an onSuspend event:

Clean-up before app closes

The app runtime sends the onSuspend() event to the event page before unloading it. Your event page can listen out for this event and do clean-up tasks before the app closes.

Once this event is fired, the app runtime starts the process of closing the app: all events stop firing and JavaScript execution is halted. Any asynchronous operations started while handling this event are not guaranteed to complete. Keep the clean-up tasks synchronous and simple.

chrome.runtime.onSuspend.addListener(function() { // Do some simple clean-up tasks. });

This document says the onSuspend is also available for extensions

Andrew Hall
  • 3,058
  • 21
  • 30
  • 2
    I tried the onSuspend event and it apparently is only usable for event pages which can be suspended - the background page never experiences an onSuspend. This is when I went searching for more info and discovered someone saying extensions are terminated without notice. That's a post by someone though, not official Chrome documentation so I was hoping someone with experience actually doing this would be able to confirm one way or the other. In any case I'll re-attempt using onSuspend to see if I can get it working more reliably. – Mark Aug 30 '13 at 16:43