我想仅在打开浏览器时自动清除历史记录和缓存,而不是在从 chrome://extensions 重新加载扩展程序时清除。我该怎么做?我说的是 Chrome JavaScript API。我在 Ubuntu 16.04 上使用最新版本的 Google Chrome。
LE:我做了一个扩展,其中包括清除我的历史记录和缓存。在manifest.json中,我有:
"background": {"scripts": ["SessionManager.js"],"persistent":true},
在 SessionManager.js 中,我有:
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();
最佳答案
您需要chrome.runtime.onStartup
event .
当您更新/手动重新加载扩展时,其 onInstalled
事件会触发,但不会触发 onStartup
。另一方面,在每个浏览器启动时,您都会收到 onStartup
事件。
// background script
chrome.runtime.onStartup.addListener(function() {
// Nuke things here, probably with chrome.browsingData API
});
请注意,如果最后一个窗口关闭时 Chrome 继续在后台运行(例如 Chrome 应用仍在运行,或某些扩展程序请求了“后台”
权限),请重新打开该窗口不会注册为 onStartup
。一种解决方法是使用 chrome.windows.onCreated 来查看新打开的窗口是否是唯一的:
chrome.windows.onCreated.addListener(function() {
chrome.windows.getAll(function(windows) {
if (windows.length == 1) {
// Chrome was running, but in background: it now "opened"
}
});
})
关于javascript - Chrome 在打开时加载扩展程序与我手动重新加载扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39390111/