javascript - Chrome Extension how to get notified that background script is to be closed? -
let me explain. google chrome extension saves state in chrome.storage.local persistent storage. amount of data can quite substantial (up several mb in json layout) , not calling chrome.storage.local.set() every time change occurs minimize impact of extension on system runs on. instead saving on timer, in background, every 30 seconds.
this background saving process happens in background script stays in memory long extension running. (i set "background": {"persistent": true} in manifest.json file.)
the issue happens when extension re-installed or unloaded, of state in between 30 second saves lost.
so wondering if there's way me track moment when background script closed? (so can save app's state.)
you can intercept update listener chrome.runtime.onupdateavailable event:
fired when update available, isn't installed because app running. if nothing, update installed next time background page gets unloaded, if want installed sooner can explicitly call chrome.runtime.reload(). if extension using persistent background page, background page of course never gets unloaded, unless call chrome.runtime.reload() manually in response event update not installed until next time chrome restarts. if no handlers listening event, , extension has persistent background page, behaves if chrome.runtime.reload() called in response event.
as unloading.. if mean being disabled user, there's no way cleanup happen, don't quote me on that.
you can try listening chrome.runtime.onsuspend if use persistent page, or window.onunload. don't know, throwing out ideas. in either of cases you'll have limited time clean up.
if want clean upon browser being closed, can try playing "background" permission , listening windows being created , closed. way extend lifetime beyond browser closing, unless user explicitly disables background apps.
Comments
Post a Comment