Chrome加载扩展时打开vs我手动重新加载扩展

Chrome loading extension when opened vs I reloading manually an extension

本文关键字:加载 扩展 新加载 vs Chrome      更新时间:2023-09-26

我想清除我的历史记录和缓存自动只有当我打开我的浏览器,但不是当我从chrome重新加载扩展://扩展。我该怎么做呢?我说的是Chrome JavaScript API。我用的是最新版本的Google Chrome, Ubuntu 16.04。

LE:我做了一个扩展,在许多事情中清除了我的历史和缓存。在清单。我有:

"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事件

当你更新/手动加载一个扩展,它的onInstalled事件触发,而不是onStartup。另一方面,在每次浏览器启动时,您都会得到onStartup事件。

// background script
chrome.runtime.onStartup.addListener(function() {
  // Nuke things here, probably with chrome.browsingData API
});

请注意,如果Chrome继续在后台运行时,最后一个窗口关闭(例如,一个Chrome应用程序仍在运行,或一些扩展请求"background"权限),重新打开该窗口不会注册为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"
    }
  });
})