网站首页 > 博客文章 正文
引言:
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)
参数可以指定数据类型(如历史记录、缓存等)、时间范围等
示例
jsvar 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)
示例
jsvar 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)
示例
jsvar 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)
示例
jsfunction 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);
}
引用
https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/api-samples/browsingData
猜你喜欢
- 2025-01-06 JS实现各种日期操作方法汇总
- 2025-01-06 50个好用的前端框架,千万收好以留备用
- 2025-01-06 前端实现最佳截图方案(下)
- 2025-01-06 NSW新财年州担利好!190会计、工程、IT多个热门回归
- 2025-01-06 前端实现最佳截图方案(上)
- 2025-01-06 多线程 Web 并非不可能。 而 WASM 就是 AWSM
- 2025-01-06 深入探索Chrome开发者工具:开发者的利器
- 2025-01-06 Mozilla火狐39.0正式版增加Emoji支持
- 2025-01-06 由浅入深,66条JavaScript面试知识点(七)
- 2025-01-06 苹果为macOS推出第50个Safari技术预览版 旧TP用户亦可升级
你 发表评论:
欢迎- 368℃用AI Agent治理微服务的复杂性问题|QCon
- 365℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 358℃初次使用IntelliJ IDEA新建Maven项目
- 351℃Maven技术方案最全手册(mavena)
- 348℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 347℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 345℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 343℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)