简体   繁体   中英

Clear temporary storage data for a Chrome extension when browser closes

I am building an extension which every time the browser opens asks user for a strong password. Its purpose is that it uses that password to derive and generate strong passwords for new websites upon registration and it tries to regenerate same passwords next time a user visits an already visited website. I am using below method to store user's masterpassword(used for password generation) which is sensitive information:

window.sessionStorage.setItem(varName)

And I use below method to get it whenever it is needed.

sessionStorage.getItem(varName)

My problem is that I want this data which is stored in browsers data to be valid as long as Chrome open. This master password needs to be cleared every time user closes the browser and to be asked every time it gets reopened. I read that session storage is temporary and it gets cleared but it does not work for my extension. I also know that there is nothing to add in order to detect browser getting closed as it stops running your script. Can you please help me with it? Is there such method that keeps data for a short time?

Since Manifest V3 removed the notion of persistent background pages. You can imitate this with chrome.storage.local . The only caveat regarding this is that it stores the variable in the extensions local storage which is still okay for that user.

One way to imitate a browser closing is by creating a chrome.runtime.port that is opened and then when the browser closes or the extension gets reloaded it will call onDisconnect for that port, and you can clear your chrome.storage.local.clear() :

chrome.runtime.onConnect.addListener(port => port.onDisconnect.addListener(() => chrome.storage.local.clear()))

When the browser is launched, just connect:

chrome.runtime.connect(null, {})

That might unload itself when background script goes back to sleep, unfortunately, the only way to get passed that is to keep your own managed extension window that pops up. But that might be overkill for user experience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM