Chrome开发工具中的“清除网站数据”等效于什么?

时间:2017-03-13 19:36:23

标签: javascript google-chrome caching cookies browser-cache

我正在寻找一种方法来触发与Chrome开发工具中的“清除网站数据”相同的行为,或者尽可能接近相同的行为。我知道有some things that are not possible i.e. clearing browser cache,这不需要在这个问题的范围内。我不确定清除网站数据是否除了清除cookie,Web SQL / IndexedDB和取消注册服务工作者之外还做了一些特殊的事情。

我在我工作的一些项目中使用localForage所以清理IndexedDB / localStorage非常简单,并确保调用clear但我不熟悉其他部分的幕后故事或如果有什么我可能会失踪。对于上下文,在我的应用程序中有时候我会看到尴尬的回归(我做了一些更新但他们没有更新)。我有一个清除localStorage和cookies的编程触发器,但它不能修复这种情况,但点击“清除站点数据”并检查所有内容。

另外注意我还发现了关于clear site data headers的W3C规范草案?我不确定这是否是我们可能采取的方向,以触发明确的网站数据?任何信息都将非常受欢迎。

1 个答案:

答案 0 :(得分:2)

因此,在没有得到任何答案之后,决定研究一些源代码。我找到了Chrome Devtools的存储库,以及他们从UI (can be found in resources/ClearStorageView.js)实际调用Most Recent Score = VAR EmpID = 'Master'[Employee ID] VAR tblScores = FILTER ('Master', 'Master'[Employee ID] = EmpID && NOT ( ISBLANK ( 'Master'[Score] ) ) ) VAR mrsDate = CALCULATE ( MAX ( [Date] ), tblScores ) RETURN CALCULATE ( MAX ( 'Master'[Score] ), FILTER ( tblScores, 'Master'[Date] = mrsDate ) ) 的代码。

this._clear

我认为tl; dr自然是他们的“清除站点”数据挂钩到表单上具有的复选框选项中,这些选项可以通过并清除这些选项。在我最初的问题中,我个人想知道是否还有其他功能,而且无论您是否检查了这些功能,它们看起来确实都在最顶层调用:

  _clear() {
    if (!this._securityOrigin)
      return;
    const storageTypes = [];
    for (const type of this._settings.keys()) {
      if (this._settings.get(type).get())
        storageTypes.push(type);
    }

    this._target.storageAgent().clearDataForOrigin(this._securityOrigin, storageTypes.join(','));

    const set = new Set(storageTypes);
    const hasAll = set.has(Protocol.Storage.StorageType.All);
    if (set.has(Protocol.Storage.StorageType.Cookies) || hasAll) {
      const cookieModel = this._target.model(SDK.CookieModel);
      if (cookieModel)
        cookieModel.clear();
    }

    if (set.has(Protocol.Storage.StorageType.Indexeddb) || hasAll) {
      for (const target of SDK.targetManager.targets()) {
        const indexedDBModel = target.model(Resources.IndexedDBModel);
        if (indexedDBModel)
          indexedDBModel.clearForOrigin(this._securityOrigin);
      }
    }

    if (set.has(Protocol.Storage.StorageType.Local_storage) || hasAll) {
      const storageModel = this._target.model(Resources.DOMStorageModel);
      if (storageModel)
        storageModel.clearForOrigin(this._securityOrigin);
    }

    if (set.has(Protocol.Storage.StorageType.Websql) || hasAll) {
      const databaseModel = this._target.model(Resources.DatabaseModel);
      if (databaseModel) {
        databaseModel.disable();
        databaseModel.enable();
      }
    }

    if (set.has(Protocol.Storage.StorageType.Cache_storage) || hasAll) {
      const target = SDK.targetManager.mainTarget();
      const model = target && target.model(SDK.ServiceWorkerCacheModel);
      if (model)
        model.clearForOrigin(this._securityOrigin);
    }

    if (set.has(Protocol.Storage.StorageType.Appcache) || hasAll) {
      const appcacheModel = this._target.model(Resources.ApplicationCacheModel);
      if (appcacheModel)
        appcacheModel.reset();
    }

    this._clearButton.disabled = true;
    const label = this._clearButton.textContent;
    this._clearButton.textContent = Common.UIString('Clearing...');
    setTimeout(() => {
      this._clearButton.disabled = false;
      this._clearButton.textContent = label;
    }, 500);
  }

不确定这是什么,但进行一些挖掘,看起来可能是experimental API for clearing data from origin for Lighthouse apps。我个人不确定或有资格说这是正确的。希望这对人们有帮助。

如果我可以制作等效的JavaScript脚本,我会尝试,但从技术上讲,所有的Chrome Devtools都在JavaScript中:P