history
使用 history
API与浏览器历史记录进行交互。
注意:下载也被当做一个 HistoryItem
对象。因此,history.onVisited
等事件也会被下载所触发。
浏览器历史记录是对用户所访问的页面按时间顺序进行的记录和保存。history API 可以帮你实现以下功能:
然而,用户可能多次访问单个页面,因此API中有访问集合“visits”的概念。所以,该API还可以做如下使用:
使用该API之前,扩展程序必须在其 manifest.json
文件中获取history的许可。
类型
- history.TransitionType (en-US)
- 描述浏览器如何转到特定页面。
- history.HistoryItem (en-US)
-
提供浏览器历史记录中单个特定页面的详细信息。
- history.VisitItem (en-US)
-
描述对一个页面的单次访问。
方法
- history.search() (en-US)
- 在浏览器历史记录中查找符合给定条件的
history.HistoryItem
- history.getVisits() (en-US)
- 获取指定页面的访问集信息。
- history.addUrl() (en-US)
- 为浏览器历史记录添加一个指定页面的访问。
- history.deleteUrl() (en-US)
- 移除浏览器历史记录中所有对指定URL的访问。
- history.deleteRange() (en-US)
- 移除指定时间段内对用户指定页面的访问。
- history.deleteAll() (en-US)
- 移除浏览器历史记录中的所有访问。
事件
history.onTitleChanged
-
当用户访问的页面标题被记录时触发。
- history.onVisited (en-US)
- 每次用户访问页面时会触发,并提供该页面的 history.HistoryItem (en-US) 数据。
- history.onVisitRemoved (en-US)
-
当一个URL被彻底从浏览器历史记录中移除时触发。
浏览器兼容性
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Example extensions
致谢
该 API 基于Chromium的chrome.history
API。该文档由Chromium代码中的history.json
衍生而来。
微软 Edge 兼容性数据由微软公司提供并包含在如下证书中—— Creative Commons Attribution 3.0 United States License.