BrowsingData:探索浏览器数据操作的奥秘

引言:

BrowsingData 功能为开发者提供了强大的能力来与浏览器的浏览数据进行交互。它能够让我们有针对性地处理诸如浏览器历史记录、缓存数据、下载记录以及各种类型的存储数据等。

Manifest

在使用BrowsingData功能之前,我们需要在 manifest 文件中声明必要的权限。

js 复制代码
{
  "name": "BrowsingData API: Basics",
  "version": "1.2",
  "description": "Uses the chrome.browsingData API to clear the user's history without requiring the user to visit the history page.",
  "permissions": ["browsingData"],
  "action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "manifest_version": 3
}

查询历史记录数据

通过 chrome.browsingData.query 方法来查询历史记录浏览数据。

API

chrome.browsingData.query(object, callable)

参数可以指定数据类型(如历史记录、缓存等)、时间范围等

示例

js 复制代码
var queryParams = {
  // 只查询过去一天内的历史记录数据
  historyTypes: ['history'],
  startTime: new Date(Date.now() - 24 * 60 * 60 * 1000)
};
chrome.browsingData.query(queryParams, function(result) {
  // result 包含符合查询条件的数据信息
  console.log(result);
});

新增定期数据处理任务

通过 chrome.browsingData.setRemovalPeriodic 方法来设定特定的数据清理计划。

API

chrome.browsingData.setRemovalPeriodic(object, callable)

示例

js 复制代码
var clearCacheTask = {
  // 设定只清理缓存数据
  dataTypes: ['cache'],
  // 设定清理的时间间隔,这里假设为每周一次
  maxAge: 7 * 24 * 60 * 60 * 1000
};
chrome.browsingData.setRemovalPeriodic(clearCacheTask, function() {
  console.log('缓存数据清理任务设置成功');
});

修改定期数据处理任务

通过 chrome.browsingData.updateRemovalPeriodic 方法来修改数据清理计划。

API

chrome.browsingData.updateRemovalPeriodic(taskId, updatedTask, callable)

示例

js 复制代码
var updatedTask = {
  dataTypes: ['cache'],
  maxAge: 24 * 60 * 60 * 1000
};
// 假设之前设置任务时得到的任务 ID 为 taskId
chrome.browsingData.updateRemovalPeriodic(taskId, updatedTask, function() {
  console.log('缓存数据清理任务修改成功');
});

删除数据处理任务

若某个数据处理任务不再需要,我们可以使用 chrome.browsingData.removeRemovalPeriodic 方法将其删除。

API

chrome.browsingData.removeRemovalPeriodic(taskId, callable)

示例

js 复制代码
function buttonClicked() {
  const option = document.getElementById('timeframe');
  let selectedTimeframe = option.value;
  let removal_start = parseMilliseconds(selectedTimeframe);
  if (removal_start == undefined) {
    return null;
  }
  chrome.browsingData.remove(
    { since: removal_start },
    {
      appcache: true,
      cache: true,
      cacheStorage: true,
      cookies: true,
      downloads: true,
      fileSystems: true,
      formData: true,
      history: true,
      indexedDB: true,
      localStorage: true,
      serverBoundCertificates: true,
      serviceWorkers: true,
      pluginData: true,
      passwords: true,
      webSQL: true
    }
  );
  const success = document.createElement('div');
  success.classList.add('overlay');
  success.setAttribute('role', 'alert');
  success.textContent = 'Data has been cleared.';
  document.body.appendChild(success);

  setTimeout(function () {
    success.classList.add('visible');
  }, 10);
  setTimeout(function () {
    if (close === false) success.classList.remove('visible');
    else window.close();
  }, 4000);
}

引用

github.com/GoogleChrom...

相关推荐
一勺-_-2 小时前
Chrome浏览器和Microsoft Edge浏览器的导出收藏链接
前端·chrome·edge
Peter_chq4 小时前
selenium快速入门
linux·开发语言·chrome·python·selenium
ONE_Gua1 天前
魔改chromium源码——canvas指纹修改 第一节
前端·chrome·浏览器
alden_ygq1 天前
linux查询inode使用率
linux·运维·chrome
gqkmiss2 天前
Chrome 135 版本开发者工具(DevTools)更新内容
服务器·网络·chrome·浏览器·chrome devtools·开发者工具
漠月瑾-西安3 天前
CNVD-2025-06046:Google Chrome沙箱逃逸漏洞大揭秘与防护指南
chrome·网络安全·cve-2025-2783
指针满天飞3 天前
CondaError: Run ‘conda init‘ before ‘conda activate‘
前端·chrome·conda
niuniu_6664 天前
针对 Python 3.7.0,以下是 Selenium 版本的兼容性建议和安装步骤
开发语言·chrome·python·selenium·测试工具
知远同学5 天前
关闭Chrome提示更新失败的弹窗
前端·chrome
Re_Virtual5 天前
Talend API Tester
chrome·restful