【问题标题】:How can I get my entire history with chrome.history.search?如何使用 chrome.history.search 获取我的全部历史记录?
【发布时间】:2014-03-06 13:25:21
【问题描述】:

我正在构建一个扩展程序,它可以读取 Chrome 历史记录并分析关键字的链接。

我正在使用chrome.history.search 方法来检索浏览器历史记录,如下所示:

chrome.history.search({
        'text': '',
        'maxResults': 500,
    }, function(historyItems){
    });

此时,我将检索到的 URL 存储在一个数组中并开始阅读它们。

但我并没有得到一切。检索到的 URL 数量因运行不同而异。我尝试使用搜索方法中的参数进行试验,但无法影响返回的链接数。

谁能帮我理解这个?

编辑:当我说我没有得到所有东西时,我的意思是与我可以看到的浏览器历史记录相比,通过扩展程序提取的内容要有限得多。

【问题讨论】:

    标签: google-chrome-extension browser-history


    【解决方案1】:

    这是我编写的一些代码,用于尝试使用搜索检索所有历史记录项。试试看是否有帮助:

    var nextEndTimeToUse = 0;
    
    var allItems = [];
    var itemIdToIndex = {};
    
    function getMoreHistory(callback) {
      var params = {text:"", maxResults:500};
      params.startTime = 0;
      if (nextEndTimeToUse > 0)
        params.endTime = nextEndTimeToUse;
    
      chrome.history.search(params, function(items) {
        var newCount = 0;
        for (var i = 0; i < items.length; i++) {
          var item = items[i];
          if (item.id in itemIdToIndex)
            continue;
          newCount += 1;
          allItems.push(item);
          itemIdToIndex[item.id] = allItems.length - 1;
        }
        if (items && items.length > 0) {
          nextEndTimeToUse = items[items.length-1].lastVisitTime;
        }
        callback(newCount);
      });
    }
    
    function go() {
      getMoreHistory(function(cnt) { 
        console.log("got " + cnt);
        if (cnt > 0)
          go();
      });
    }
    

    【讨论】:

    • if (item.id in itemIdToIndex) continue; 位是关键。我对我在结果中得到的重复感到困惑。它们的信息似乎相同,因此不清楚 API 为何返回它们,但可以通过构建 ID 索引将它们过滤掉。
    【解决方案2】:

    https://bugs.chromium.org/p/chromium/issues/detail?id=73812

    您需要添加开始时间

      var microsecondsBack = 1000 * 60 * 60 * 24 * days;
    
      var startTime = (new Date).getTime() - microsecondsBack;
    

    【讨论】:

    • 由于 OP 想要 整个 历史,startTime 应该设置为 0。
    【解决方案3】:

    有趣的是,Justaman 在其answer 中提到的Chromium bug 表明,传递maxResults: 0 实际上会返回所有 历史项目。所以如果你真的想要整个历史,你可以这样做:

    chrome.history.search({ text: "", startTime: 0, maxResults: 0 }, 
        items => console.log(items));
    

    我还没有尝试过,因为我预计将我的数万(?)历史项目加载到内存中会导致 Chrome 崩溃。但我确实在几天前使用startTime 进行了尝试,结果返回了 645 个项目。

    如果您碰巧使用方便的 chrome-promise 库,这里是 Antony 的 answer 的一个版本,它使用承诺而不是回调来循环 API 调用,直到找到所需数量的历史记录项:

    import ChromePromise from 'chrome-promise';
    
    const chromep = new ChromePromise();
    
    function loop(fn)
    {
        return fn().then(val => (val === true && loop(fn)) || val);
    }
    
    function getHistory(requestedCount)
    {
        var history = [],
            ids = {};
    
        return loop(() => {
            var endTime = history.length &&
                    history[history.length - 1].lastVisitTime || Date.now();
    
            return chromep.history.search({
                text: "",
                startTime: 0,
                endTime: endTime,
                maxResults: 1000
            })
                .then(historyItems => {
                    var initialHistoryLength = history.length;
    
                    historyItems.forEach(item => {
                        var id = item.id;
    
                            // history will often return duplicate items
                        if (!ids[id] && history.length < requestedCount) {
                            addURLs(item);
                            history.push(item);
                            ids[id] = true;
                        }
                    });
    
                        // only loop if we found some new items in the last call
                        // and we haven't reached the limit yet
                    if (history.length > initialHistoryLength && 
                            history.length < requestedCount) {
                        return true;
                    } else {
                        return history;
                    }
                });
        });
    }
    

    你可以这样使用这个函数:

    getHistory(2000).then(items => console.log(items));
    

    【讨论】:

    • 10k 历史记录对浏览器来说不是问题。
    猜你喜欢
    • 2020-10-06
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多