skip to Main Content

I’m currently developing an extension that will provide a better alternative to standard Edge history page. For accessing the history I used chrome.history, which, as I previously thought, solved my problem. However, as I increased maxResults property of the query, at some point it just stopped returning more results (around a 100, I definitely have more than 100 history items), from which I concluded that it only returns the items that were added after the last launch of the browser. I comfirmed that by restarting the browser and saw an empty result. Currently, I’m using this piece of code to get history items:

function displayHistory(items, filter, main) {
    items.processingDone = false;
    chrome.history.search(
        {
            text: filter,
            maxResults: 1000
        },
        (history) => {
            undisplayAll(items, main);
            for (const i of history) {
                displayItem(items, i, main);
            }
            items.processingDone = true;
        }
    );
}

The question is: How to access the whole history?

2

Answers


  1. You could try promisifying the API call:

    function fetchHistoryPage(startTime, endTime, pageSize) {
      return new Promise((resolve, reject) => {
        chrome.history.search({
          text: '',
          startTime: startTime,
          endTime: endTime,
          maxResults: pageSize
        }, function(results) {
          if (chrome.runtime.lastError) {
            reject(chrome.runtime.lastError);
          } else {
            resolve(results);
          }
        });
      });
    }
    

    And then implement a paging call:

    const MILLIS_PER_DAY = 1_000 * 60 * 60 * 24;
    
    let pageSize = 100; // items per page
    let currentPage = 0;
    
    async function getNextPage() {
      let now = Date.now();
      let startTime = now - (currentPage + 1) * MILLIS_PER_DAY; // 1 day earlier
      let endTime = now - currentPage * MILLIS_PER_DAY;
    
      try {
        let results = await fetchHistoryPage(startTime, endTime, pageSize);
        if (results.length) {
          currentPage++; // Only when there are results
          return results;
        } else {
          console.log('No more history items.');
        }
      } catch (error) {
        console.error('Error fetching history:', error);
      }
      return null;
    }
    

    Here is how you can use it:

    (async () => {
      const allResults = [];
      let results;
      
      do {
        results = await getNextPage();
        if (results) {
          allResults.push(...results);
        }
      } while(results);
    
      // Do something with allResults
    })();
    
    Login or Signup to reply.
  2. Even though I can get more history items than the number from current session, chrome.history seems to retrieve far less items than my actual counts of entire browsing history. According to https://issues.chromium.org/issues/40229042#comment16, it seems to be a problem that hasn’t been fixed yet, but they didn’t mention anything about "current session".

    I suggest you create a new post there to let the Dev Team know about the problem, both the old one from back in the day and the new one you just found.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search