【问题标题】:Chrome loading extension when opened vs I reloading manually an extension打开时 Chrome 加载扩展与我手动重新加载扩展
【发布时间】:2016-09-08 20:44:00
【问题描述】:

我想仅在打开浏览器时自动清除历史记录和缓存,而不是在我从 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();

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension


    【解决方案1】:

    您需要chrome.runtime.onStartup event

    当您更新/手动重新加载扩展时,它的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"
        }
      });
    })
    

    【讨论】:

    • 这不是一个有用的描述,为什么它不是你想要的。
    • 好吧,既然你编辑了你的问题,我不明白为什么我的解决方案不适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2017-04-01
    • 1970-01-01
    • 2012-01-30
    • 2022-06-21
    • 1970-01-01
    相关资源
    最近更新 更多