Chrome.history.getVisits returning empty set, even

2019-07-12 03:41发布

问题:

I'd like to collect my past N history items, and then find the time stamp of all times I've visited that page going back as far as my chrome history contains.

Chrome.history.search allows me to get the urls of my last N history items.

However, if I now call Chrome.history.getVisits on each of those urls, some return '[]', even though they exist in my history. Furthermore, searching for the URL in chrome://history returns the item properly, even though the getVisits call returns [].

One further tidbit, this appears to be a problem the further back in my history I go. My first 50 items or so all return a proper [Object], but after that, nearly all (looks like 90%+) are empty sets [].

The image below shows the API call returning [], while the history correctly shows a result.

回答1:

By looking a the extension samples here, the fix is as follows. You need a closure to bind the url into the callback's args (the callback for chrome.history.getVisits() function) to make it work.

function loadUrlVisits(url){
   var processVisitsWithUrl = function(url) {
   // We need the url of the visited item to process the visit.
   // Use a closure to bind the  url into the callback's args.
      return function(visitItems) {
            processVisits(url, visitItems);
      };
    };
    chrome.history.getVisits({url: url}, processVisitsWithUrl(url));

}

function processVisits(url, visitItems){
  //process visit items here...
}

The reference extension from the samples can be found here.