history.addUrl()
向瀏覽器的歷史記錄中新增一次訪問給定 URL 的記錄。此次訪問的時間將記錄為呼叫時的時間,並且 TransitionType 將被記錄為 "link"。
這是一個非同步函式,返回一個 Promise。
語法
js
let addingUrl = browser.history.addUrl(
details // object
)
引數
details-
object. 包含要新增的 URL 的物件。url-
string. 要新增的 URL。 title可選-
string: 頁面的標題。如果未提供此項,則標題將記錄為
null。 transition可選-
history.TransitionType. 描述了此次頁面導航的方式。如果未提供此項,則將記錄 "link" 作為過渡型別。 visitTime可選-
number或string或object. 指示日期和時間的數值。它可以表示為:一個Date物件,一個 ISO 8601 日期字串,或者自紀元以來的毫秒數。將訪問時間設定為此值。如果未提供此項,則將記錄當前時間。
返回值
一個 Promise,當專案新增成功後,將以無引數的形式 fulfilled。
示例
新增一次訪問 "https://example.org/" 的記錄,然後透過搜尋歷史記錄中的最新專案並列印日誌來驗證新記錄是否已儲存。
js
function onGot(results) {
if (results.length) {
console.log(results[0].url);
console.log(new Date(results[0].lastVisitTime));
}
}
browser.history
.addUrl({ url: "https://example.org/" })
.then(() =>
browser.history.search({
text: "https://example.org/",
startTime: 0,
maxResults: 1,
}),
)
.then(onGot);
新增一次訪問 "https://example.org" 的記錄,但將其 visitTime 設定為 24 小時前,並將 transition 設定為 "typed"。
js
const DAY = 24 * 60 * 60 * 1000;
function oneDayAgo() {
return Date.now() - DAY;
}
function onGot(visits) {
for (const visit of visits) {
console.log(new Date(visit.visitTime));
console.log(visit.transition);
}
}
browser.history
.addUrl({
url: "https://example.org/",
visitTime: oneDayAgo(),
transition: "typed",
})
.then(() =>
browser.history.getVisits({
url: "https://example.org/",
}),
)
.then(onGot);
瀏覽器相容性
載入中…
注意: 此 API 基於 Chromium 的 chrome.history API。本文件源自 Chromium 程式碼中的 history.json。