Google chrome extension 如何使用chrome.history.search获取我的全部历史记录?

Google chrome extension 如何使用chrome.history.search获取我的全部历史记录?,google-chrome-extension,browser-history,Google Chrome Extension,Browser History,我正在构建一个扩展,它可以读取Chrome历史记录并分析关键字链接 我正在使用chrome.history.search方法检索浏览器历史记录,如下所示: chrome.history.search({ 'text': '', 'maxResults': 500, }, function(historyItems){ }); 此时,我将检索到的URL存储在一个数组中,并开始读取它们 但我并不是什么都能得到。检索到的URL数量因运行的不同而不同。我

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

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

chrome.history.search({
        'text': '',
        'maxResults': 500,
    }, function(historyItems){
    });
此时,我将检索到的URL存储在一个数组中,并开始读取它们

但我并不是什么都能得到。检索到的URL数量因运行的不同而不同。我尝试在搜索方法中使用参数,但我无法影响返回的链接数

有人能帮我理解吗


编辑:当我说我没有得到所有信息时,我的意思是,与我可以看到的浏览器历史记录相比,通过扩展获取的信息要有限得多。

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

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();
  });
}
var-nextendtimeouse=0;
var allItems=[];
var itemIdToIndex={};
函数getMoreHistory(回调){
var params={text:,maxResults:500};
params.startTime=0;
如果(nextEndTimeToUse>0)
params.endTime=nextendtimeouse;
chrome.history.search(参数、函数(项){
var newCount=0;
对于(变量i=0;i0){
nextEndTimeToUse=items[items.length-1].lastVisitTime;
}
回调(newCount);
});
}
函数go(){
getMoreHistory(函数(cnt){
console.log(“got”+cnt);
如果(cnt>0)
go();
});
}

您需要添加startime

  var microsecondsBack = 1000 * 60 * 60 * 24 * days;

  var startTime = (new Date).getTime() - microsecondsBack;
有趣的是,Justaman在他的文章中提到的方法揭示了传递
maxResults:0
实际上将返回所有历史记录项。因此,如果你真的想了解整个历史,你可以:

chrome.history.search({ text: "", startTime: 0, maxResults: 0 }, 
    items => console.log(items));
我还没有试过,因为我想把我数万条历史记录载入内存会让Chrome崩溃。但我确实在几天前用
startTime
尝试过,结果返回了645个项目

如果您碰巧正在使用handy库,下面是Antony的一个版本,它使用承诺而不是回调来循环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;
                }
            });
    });
}
由于OP需要整个历史记录,
startTime
应设置为0。如果(itemIdToIndex中的item.id)继续,则
位是关键。我被结果中重复的内容弄糊涂了。它们的信息似乎是相同的,因此不清楚API返回它们的原因,但是可以通过构建ID索引来过滤掉它们。
getHistory(2000).then(items => console.log(items));