最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - chrome access browser history - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Sep 7, 2012 at 21:28 lowerkey asked Dec 20, 2010 at 1:09 lowerkeylowerkey 8,34518 gold badges70 silver badges102 bronze badges 3
  • What is going wrong? What doesn't work? – Pekka Commented Dec 20, 2010 at 1:12
  • To start, your first 2 lines need terminating semicolons – Yahel Commented Dec 20, 2010 at 1:15
  • I get 234 histories and 0 visits. I was expecting a lot more visits than histories. – lowerkey Commented Dec 20, 2010 at 1:15
Add a ment  | 

4 Answers 4

Reset to default 7

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 e.a). 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 piles 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

发布评论

评论列表(0)

  1. 暂无评论