web_extensions/
history.rs

1//! Wrapper for the [`chrome.history` API](https://developer.chrome.com/docs/extensions/reference/history/).
2
3use crate::{util::*, Error};
4use serde::{Deserialize, Serialize};
5use web_extensions_sys as sys;
6
7/// <https://developer.chrome.com/docs/extensions/reference/history/#method-search>
8pub async fn search(query: &Query<'_>) -> Result<Vec<HistoryItem>, Error> {
9    let js_query = js_from_serde(query)?;
10    let js_value = sys::chrome()
11        .history()
12        .search(object_from_js(&js_query)?)
13        .await;
14    serde_from_js(js_value)
15}
16
17/// <https://developer.chrome.com/docs/extensions/reference/history/#type-search-query>
18#[derive(Default, Debug, Clone, Serialize)]
19#[serde(rename_all = "camelCase")]
20pub struct Query<'a> {
21    /// A free-text query to the history service.
22    ///
23    /// Leave empty to retrieve all pages.
24    pub text: &'a str,
25
26    /// Limit results to those visited before this date,
27    /// represented in milliseconds since the epoch.
28    pub end_time: Option<i64>,
29
30    /// The maximum number of results to retrieve.
31    ///
32    /// Defaults to 100.
33    pub max_results: Option<usize>,
34
35    /// Limit results to those visited after this date,
36    /// represented in milliseconds since the epoch.
37    ///
38    /// If not specified, this defaults to 24 hours in the past.
39    pub start_time: Option<i64>,
40}
41
42impl<'a> From<&'a str> for Query<'a> {
43    fn from(q: &'a str) -> Self {
44        Self {
45            text: q,
46            ..Default::default()
47        }
48    }
49}
50
51/// <https://developer.chrome.com/docs/extensions/reference/history/#type-HistoryItem>
52///
53/// An object encapsulating one result of a history query.
54
55#[derive(Debug, Clone, Deserialize)]
56#[serde(rename_all = "camelCase")]
57pub struct HistoryItem {
58    /// Unique identifier.
59    pub id: String,
60
61    /// When this page was last loaded, represented in milliseconds since the epoch.
62    // NOTE: chrome returns floating point values, so i64 does not work here.
63    pub last_visit_time: Option<f64>,
64
65    /// The title of the page when it was last loaded.
66    pub title: Option<String>,
67
68    /// The number of times the user has navigated to this page by typing in the address.
69    pub typed_count: Option<usize>,
70
71    /// The URL of the page.
72    pub url: Option<String>,
73
74    /// The number of times the user has visited the page.
75    pub visit_count: Option<usize>,
76}