【问题标题】:How to delete a specific browser history item based on URL and time range如何根据 URL 和时间范围删除特定的浏览器历史记录项
【发布时间】:2020-08-07 07:24:39
【问题描述】:

我正在尝试使用chrome.history 删除我的扩展程序在后台窗口中自动访问的一些特定访问。

我发现至少有一个couple ways可以做到这一点:

  1. chrome.history.deleteUrl(url): 需要一个 URL 并删除所有出现的 URL。

  2. chrome.history.deleteRange(range):需要startTime 和endTime,并删除该范围内的所有网址。

如何将两者结合起来删除具有特定 URL 和时间范围的浏览器历史访问?

或者还有另一种可能更好的方法来完全解决这个问题。比如主动设置监听器删除网址,同时结合上述功能和chrome.history.onVisited.addListener自动上网。

谢谢!

【问题讨论】:

    标签: javascript google-chrome browser google-chrome-extension browser-history


    【解决方案1】:

    Chrome API 不提供按 URL 和日期删除历史记录项的方法。您只能通过chrome.history 或chrome. browsingData 访问历史记录,并且两者都无法通过这种方式查询历史记录项。

    我认为您最好的选择是使用您提到的chrome.history.onVisited.addListener 方法。由于您的扩展程序正在访问网站,因此您知道要检查哪些网址。假设您只需要删除在扩展程序运行时创建的历史记录项,您可以使用类似...

    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
            });
        }
    })
    

    【讨论】:

    • 谢谢!与我想出的答案类似。
    【解决方案2】:

    这是我想出的,您可以在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 对我不起作用。不过,这似乎不太可能删除比新条目更多的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 2012-04-20
      • 2015-06-14
      • 2011-02-03
      • 2010-10-20
      • 2023-03-13
      相关资源
      最近更新 更多