I wanna clear my history and cache automatically only when I open my browser, but not when I reload the extension from chrome://extensions. How do I do it? I'm talking about Chrome JavaScript API. I use the latest version of Google Chrome, on Ubuntu 16.04.
LE: I made an extension that among many things clears my history and cache. In manifest.json, I have:
"background": {"scripts": ["SessionManager.js"],"persistent":true},
In SessionManager.js, I have:
function init(){
setTimeout(clean,1);
}
function clean(){
chrome.browsingData.remove({},{'appcache':true,'cache':true,'cookies':true,'downloads':true,'fileSystems':true,'formData':true,'history':true,'indexedDB':true,'localStorage':true,'serverBoundCertificates':true,'passwords':true,'pluginData':true,'serviceWorkers':true,'webSQL':true});
}
init();
You need the chrome.runtime.onStartup
event .
When you update / manually reload an extension, its onInstalled
event triggers, but not onStartup
. On the other hand, on every browser start you get onStartup
event.
// background script
chrome.runtime.onStartup.addListener(function() {
// Nuke things here, probably with chrome.browsingData API
});
Note that in case Chrome continues to run in the background when the last window is closed (eg a Chrome App is still running, or some extension requested the "background"
permission), re-opening that window won't register as onStartup
. A workaround is to use chrome.windows.onCreated
to see if the newly opened window is the only one:
chrome.windows.onCreated.addListener(function() {
chrome.windows.getAll(function(windows) {
if (windows.length == 1) {
// Chrome was running, but in background: it now "opened"
}
});
})
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.