简体   繁体   中英

chrome access browser history

I'm trying to access my browser history in chrome, using a browser action with a popup page.

var histories = [];
var visits = [];

chrome.history.search({'text':'', 'maxResults':0}, function(historyItems){
    for(var h in historyItems){
        histories.push({'id': h.id, 'url':h.url});
    }
});

for(var h in histories){
    chrome.history.getVisits({'url': h.url, function(visitItems){
        for(var v in visitItems){
            var id = v.id;
            var visitId = v.visitId;
            var visitTime = v.visitTime;
            var referringVisitId = v.referringVisitId;
            var transition = v.transition;
            visits.push({'id': v.id, 'visitId': v.visitId, 'visitTime': v.visitTime, 'referringVisitId': v.referringVisitId, 'transition':v.transition});
        }
    });
}

console.log(histories.length + ' histories');
console.log(visits.length + ' visits');

I get 234 histories and 0 visits as a result. How can I have a bunch of pages with no visits? What am I doing wrong?

The reason why your code doesn't work is the asynchronous nature of the calls to chrome.history.

You are actually attempting to iterate through histories[] BEFORE chrome.history.search returns. This means that histories[] at that point is still an empty array. Does the console.log at the end indeed print out more than 0 histories? Anyway, it's a racing condition at this point.

What you need to do is call chrome.history.getVisits inside the callback function from chrome.history.search.

Here's one way to do it:

var histories = [];
var visits = [];

chrome.history.search({text:'', maxResults:0}, function(historyItems) {
    var historiesProcessed = 0;
    for (var i = 0; i < historyItems.length; i++) {
        histories.push(historyItems[i]);
        chrome.history.getVisits({url: historyItems[i].url}, function(visitItems) {
            for (var i = 0; i < visitItems.length; i++) {
                visits.push(visitItems[i]);
            }
            historiesProcessed++;
            if (historiesProcessed === historyItems.length) {
                console.log(visits.length + ' visits');
            }
        });
    }
    console.log(histories.length + ' histories');
});

It's not the prettiest code but it works and you can refactor it to look nicer.

I'm not sure you can access chrome.history inside a popup page (let me know when you test it), but it'll definitely work from the background page and you can pass the results to the popup.

The problem is, this does not work anymore inside webkit (chrome, safari ea). It's absence (traversing the visits) fixes a security-bug which exposes the visited links to any website. With some random guessing it was possible to track and profile visitors, which was considered a privacy-breach.

There's an extension called ClickHint that presents statistics about visited sites and pages, including the number of visits and the amount of time spent on each page. As far as I know, it takes a from-this-point-forward approach: It compiles the stats after you've installed the extension and only while it is enabled. Perhaps this method will work for you as well.

chrome.history.search({
'text': '',              // Return every history item....
'startTime': oneWeekAgo  // that was accessed less than one week ago.
   },
   function(historyItems) {
       console.log(historyItems);
       document.write(JSON.stringify(historyItems))
  });

This will just give the visitCount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM