21

How can I get the URLs of recently visited tabs using chrome.history API, specifically, the last 10 URLs visited?

2 Answers 2

32

Pass an empty string as your query to the search() method of the chrome.history API. For example, this will log the 10 most recently visited URLs to the console:

chrome.history.search({text: '', maxResults: 10}, function(data) {
    data.forEach(function(page) {
        console.log(page.url);
    });
});
3
  • 5
    chrome.history now returns undefined.
    – Rob
    Jan 16, 2020 at 23:18
  • Yes, hence the question title "How to get browsing history using history API in Chrome extension"
    – Chris
    May 22, 2020 at 2:25
  • 4
    Rob, I think you have to state the "permissions": ["history" ], in the manifest.json file of the extension to get access to the history API. Nov 12, 2020 at 7:44
6

You have to put:

"permissions": [
      "history"
    ],

in you manifest.json file of the extension, and then your code can look like this:

chrome.history.search({
  'text': '',               // Return every history item....
  'startTime': oneWeekAgo,  // that was accessed less than one week ago.
  'maxResults': 100         // Optionally state a limit
},
function(historyItems) {
  // For each history item, get details on all visits.
  for (var i = 0; i < historyItems.length; ++i) {
    var url = historyItems[i].url;
     // do whatever you want with this visited url
  }
 }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.