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

相关推荐
集成显卡11 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端掘金者H18 小时前
Chrome插件实现WebPush推送通知的功能
前端·chrome
HEX9CF18 小时前
【Linux】awk 命令详解及使用示例:结构化文本数据处理工具
linux·chrome·算法
会飞的鱼先生19 小时前
javascript中Cookie、BOM、DOM的使用
前端·javascript·chrome
全栈陈序员1 天前
前端文件下载常用方式详解
前端·javascript·chrome·ajax·css3·html5·safari
qq_336411751 天前
【笔记】Chrome浏览器的性能调试(performance)工具使用和代码优化
前端·chrome·笔记
Sapphire~1 天前
Linux-07 ubuntu 的 chrome 启动不了
linux·chrome·ubuntu
去旅行、在路上2 天前
chrome使用手机调试触屏web
前端·chrome
waterHBO3 天前
修改 vscode 左侧导航栏的文字大小 (更新版)
ide·chrome·vscode·编辑器
鱼与宇3 天前
WebSphere(WAS)
前端·chrome