2014-03-06 101 views
1

我正在構建一個擴展程序,通過Chrome歷史記錄來讀取並分析關鍵字的鏈接。如何使用chrome.history.search獲取我的整個歷史記錄?

我使用chrome.history.search方法來檢索瀏覽器的歷史,像這樣:

chrome.history.search({ 
     'text': '', 
     'maxResults': 500, 
    }, function(historyItems){ 
    }); 

在這一點上,我存儲在數組中檢索到的網址,並開始通過他們閱讀。

但我沒有得到一切。檢索到的URL數量因不同的運行而有所不同。我試着用搜索方法中的參數進行試驗,但是我無法影響返回鏈接的數量。

任何人都可以幫助我理解這一點嗎?

編輯:當我說我沒有收到所有東西時,我的意思是與我的瀏覽器歷史記錄進行比較,我可以看到,通過擴展名獲得的瀏覽器更受限制。

回答

1

下面是我寫的一些代碼,用於嘗試使用搜索來檢索所有歷史記錄。給它一個嘗試,看看是否有所幫助:

var nextEndTimeToUse = 0; 

var allItems = []; 
var itemIdToIndex = {}; 

function getMoreHistory(callback) { 
    var params = {text:"", maxResults:500}; 
    params.startTime = 0; 
    if (nextEndTimeToUse > 0) 
    params.endTime = nextEndTimeToUse; 

    chrome.history.search(params, function(items) { 
    var newCount = 0; 
    for (var i = 0; i < items.length; i++) { 
     var item = items[i]; 
     if (item.id in itemIdToIndex) 
     continue; 
     newCount += 1; 
     allItems.push(item); 
     itemIdToIndex[item.id] = allItems.length - 1; 
    } 
    if (items && items.length > 0) { 
     nextEndTimeToUse = items[items.length-1].lastVisitTime; 
    } 
    callback(newCount); 
    }); 
} 

function go() { 
    getMoreHistory(function(cnt) { 
    console.log("got " + cnt); 
    if (cnt > 0) 
     go(); 
    }); 
} 
+0

'if(item.id in itemIdToIndex)continue;'bit is key。我對結果中重複出現的重複部分感到困惑。他們的信息似乎是相同的,所以不清楚爲什麼API返回它們,但是可以通過構建ID索引來過濾它們。 – jdunning

0

有趣的是,Justaman在他answer提到Chromium bug表明,經過maxResults: 0實際上將返回全部歷史項目。所以,如果你真的想要的整個歷史,你可以這樣做:

chrome.history.search({ text: "", startTime: 0, maxResults: 0 }, 
    items => console.log(items)); 

我還沒有試過,因爲我希望我的加載數以千計的歷史項幾十到內存中就採取了鍍鉻(?)。但是我在幾天前嘗試了startTime,並且返回了645個項目。

如果你碰巧使用得心應手chrome-promise庫,直到歷史項目所需的數量被發現這裏的安東尼answer,通過API調用使用的承諾,而不是回調循環的一個版本:

import ChromePromise from 'chrome-promise'; 

const chromep = new ChromePromise(); 

function loop(fn) 
{ 
    return fn().then(val => (val === true && loop(fn)) || val); 
} 

function getHistory(requestedCount) 
{ 
    var history = [], 
     ids = {}; 

    return loop(() => { 
     var endTime = history.length && 
       history[history.length - 1].lastVisitTime || Date.now(); 

     return chromep.history.search({ 
      text: "", 
      startTime: 0, 
      endTime: endTime, 
      maxResults: 1000 
     }) 
      .then(historyItems => { 
       var initialHistoryLength = history.length; 

       historyItems.forEach(item => { 
        var id = item.id; 

         // history will often return duplicate items 
        if (!ids[id] && history.length < requestedCount) { 
         addURLs(item); 
         history.push(item); 
         ids[id] = true; 
        } 
       }); 

        // only loop if we found some new items in the last call 
        // and we haven't reached the limit yet 
       if (history.length > initialHistoryLength && 
         history.length < requestedCount) { 
        return true; 
       } else { 
        return history; 
       } 
      }); 
    }); 
} 

你可以像這樣使用這個函數:

getHistory(2000).then(items => console.log(items)); 
相關問題