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