history.search()

根據給定的條件搜尋瀏覽器歷史記錄中的 history.HistoryItem 物件。

這是一個非同步函式,返回一個 Promise

語法

js
let searching = browser.history.search(
  query                  // object
)

引數

query

一個指示在瀏覽器歷史記錄中查詢內容的 cont. 包含以下欄位的物件。

文字

string。透過 URL 和標題搜尋歷史記錄項。字串在空格邊界處被分割成單獨的搜尋詞。每個搜尋詞都會不區分大小寫地與歷史記錄項的 URL 和標題進行匹配。如果所有搜尋詞都匹配,則返回該歷史記錄項。

例如,考慮以下條目:

URL: "http://example.org"

標題: "Example Domain"

"http"              -> matches
"domain"            -> matches
"MAIN ample"        -> matches
"main tt"           -> matches
"main https"        -> does not match

指定一個空字串 ("") 以檢索符合所有其他條件的 history.HistoryItem 物件。

startTime 可選

numberstringobject。指示日期和時間的值。這可以表示為:Date 物件、ISO 8601 日期字串 或自紀元以來的毫秒數。如果提供了此選項,則排除 lastVisitTime 早於此時間的搜尋結果。如果省略此選項,則搜尋將限制在最近 24 小時內。

endTime 可選

numberstringobject。指示日期和時間的值。這可以表示為:Date 物件、ISO 8601 日期字串 或自紀元以來的毫秒數。如果提供了此選項,則將搜尋結果限制為在此日期之前訪問過的結果。如果省略此選項,則從開始時間開始考慮所有條目。

maxResults 可選

number。要檢索的最大結果數。預設為 100,最小值為 1。如果您傳遞的 maxResults 值小於 1,該函式將丟擲錯誤。

返回值

一個 Promise 將會得到一個 history.HistoryItem 型別物件的陣列,每個物件描述一個匹配的歷史記錄項。條目按時間倒序排列。

示例

記錄最近 24 小時內訪問過的所有歷史記錄項的 URL 和最後訪問時間。

js
function onGot(historyItems) {
  for (const item of historyItems) {
    console.log(item.url);
    console.log(new Date(item.lastVisitTime));
  }
}

browser.history.search({ text: "" }).then(onGot);

記錄所有曾訪問過的歷史記錄項的 URL 和最後訪問時間。

js
function onGot(historyItems) {
  for (const item of historyItems) {
    console.log(item.url);
    console.log(new Date(item.lastVisitTime));
  }
}

browser.history
  .search({
    text: "",
    startTime: 0,
  })
  .then(onGot);

記錄包含字串 "mozilla" 的頁面的最近一次訪問的 URL 和最後訪問時間。

js
function onGot(historyItems) {
  for (const item of historyItems) {
    console.log(item.url);
    console.log(new Date(item.lastVisitTime));
  }
}

browser.history
  .search({
    text: "mozilla",
    startTime: 0,
    maxResults: 1,
  })
  .then(onGot);

擴充套件程式示例

瀏覽器相容性

注意:此 API 基於 Chromium 的 chrome.history API。本文件摘自 Chromium 程式碼中的 history.json