我想用 chrome.history
来删除我的分机在后台窗口中自动访问的一些特定访问。
我发现至少有一个 几条路 来实现这一目的。
chrome.history.deleteUrl(url)
: 需要一个URL并删除所有出现的URL.
chrome.history.deleteRange(range)
需要一个 startTime
和 endTime
并删除该范围内的所有URL。
如何将两者结合起来,删除特定URL和时间范围内的浏览器历史访问?
或者说有没有另一种或许更好的方法来完全解决这个问题。比如主动设置一个监听器,在自动浏览URL的同时,将上述功能和的一些结合起来,删除URL。chrome.history.onVisited.addListener
.
谢谢!我正试图使用chrome.history来删除我的扩展程序在后台窗口中自动访问的一些特定访问。
Chrome API并没有提供一种按URL和日期删除历史项目的方法。你只能通过 chrome.history
或 chrome. browsingData
而这两种方式都不具备查询历史项目的能力。
我想你最好的选择是使用 chrome.history.onVisited.addListener
像你提到的方法。因为你的扩展正在访问网站,你知道要检查哪些URL。假设你只需要删除在你的扩展运行时创建的历史项目,你可以使用类似...
chrome.history.onVisited.addListener((res) => {
if (res.url === 'someurl') {
const t = res.lastTimeVisisted;
// You might need to play with the end time
chrome.history.deleteRange({
startTime: t,
endTime: t
});
}
})
这是我想出的办法,你可以在下面的程序中运行 background
控制台来测试。它设置了一个监听器,打开一个新的标签页,监听器检查url是否匹配,然后删除新的历史项目。最后,它进行历史搜索以确认该项目被删除。
var url = 'http://stackoverflow.com'
// Get current window
chrome.windows.getCurrent(function(win) {
// Add temporary listener for new history visits
chrome.history.onVisited.addListener(function listener(historyItem) {
// Check if new history item matches URL
if (historyItem.url.indexOf(url) > -1) {
console.log('Found matching new history item', historyItem)
var visit_time = historyItem.lastVisitTime
// Delete range 0.5ms before and after history item
chrome.history.deleteRange({
startTime: visit_time - 0.5,
endTime: visit_time + 0.5
}, function() {
console.log('Removed Visit', historyItem)
// Get history with URL and see if last item is listed
chrome.history.search(
{text:'stackoverflow'},
function(result) {
console.log('Show History is deleted:', result)
} )
});
chrome.history.onVisited.removeListener(listener)
}
});
// Create new tab
chrome.tabs.create({
windowId: win.id,
url: url,
active: false
}, function(tab) {
console.log('Created tab')
})
});
只用 visit_time
既是 startTime
和 endTime
对我不起作用。不过这样做似乎不太可能删除比新条目更多的内容。