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

相关推荐
咖啡の猫8 小时前
Shell脚本-影响shell程序的内置命令
前端·chrome·bash
hfd19901 天前
Chrome 插件开发实战:从入门到进阶
前端·chrome
John_ToDebug2 天前
Chrome 内置扩展 vs WebUI:浏览器内核开发中的选择与实践
前端·c++·chrome
紫金龙腾2 天前
EDGE 、chrome、浏览器显示“由你的组织管理”
前端·chrome·edge
时间裂缝里的猫-O-2 天前
@Linux问题 :bash fork Cannot allocate memory 错误分析与解决方案
linux·chrome·bash
吴楷鹏2 天前
如何同时打开多个 Chrome 呢?
前端·chrome
HaushoLin2 天前
Chrome和Edge如何开启暗黑模式
chrome·edge
不是光头 强3 天前
axure chrome 浏览器插件的使用
java·chrome
zzywxc7874 天前
AI 行业应用:金融、医疗、教育、制造业领域的落地案例与技术实现
android·前端·人工智能·chrome·金融·rxjava
一涯4 天前
写一个Chrome插件
前端·chrome