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...

相关推荐
微wx笑8 小时前
chrome扩展程序如何实现国际化
前端·chrome
CUIYD_19898 小时前
Chrome 浏览器(版本号49之后)‌解决跨域问题
前端·chrome
Dontla1 天前
华为昇腾服务器(固件版本查询、驱动版本查询、CANN版本查询)
运维·服务器·chrome
JsenLong1 天前
ubuntu 守护进程
linux·chrome·ubuntu
前端大全1 天前
Chrome 推出全新的 DOM API,彻底革新 DOM 操作!
前端·chrome
林的快手1 天前
CSS文本属性
前端·javascript·css·chrome·node.js·css3·html5
码农君莫笑1 天前
Linux系统上同时打印到物理打印机并生成PDF副本方法研究
linux·前端·chrome·打印·信管通
代码轨迹2 天前
青龙面板运行selenium启动Chrome报错
chrome·python·selenium
三月七(爱看动漫的程序员)2 天前
与本地电脑PDF文档对话的PDF问答程序
前端·人工智能·chrome·gpt·搜索引擎·pdf·知识图谱
码界领航2 天前
【2025最新版】Chrome谷歌浏览器如何能恢复到之前的旧版本
前端·chrome