places/history

Unstable

Access the user's browsing history.

Usage

This module exports a single function, search(), which synchronously returns a PlacesEmitter object which then asynchronously emits data and end or error events that contain information about the state of the operation.

Example

let { search } = require("sdk/places/history");

// Simple query
search(
  { url: "https://developers.mozilla.org/*" },
  { sort: "visitCount" }
).on("end", function (results) {
  // results is an array of objects containing
  // data about visits to any site on developers.mozilla.org
  // ordered by visit count
});

// Complex query
// The query objects are OR'd together
// Let's say we want to retrieve all visits from before a week ago
// with the query of 'ruby', but from last week onwards, we want
// all results with 'javascript' in the URL or title.
// We'd compose the query with the following options
let lastWeek = Date.now - (1000*60*60*24*7);
search(
  // First query looks for all entries before last week with 'ruby'
  [{ query: "ruby", to: lastWeek },
  // Second query searches all entries after last week with 'javascript'
   { query: "javascript", from: lastWeek }],
  // We want to order chronologically by visit date
  { sort: "date" }
).on("end", function (results) {
  // results is an array of objects containing visit data,
  // sorted by visit date, with all entries from more than a week ago
  // that contain 'ruby', *in addition to* entries from this last week
  // that contain 'javascript'
});

Globals

Functions

search(queries, options)

Queries can be performed on history entries by passing in one or more query options. Each query option can take several properties, which are AND'd together to make one complete query. For additional queries within the query, passing more query options in will OR the total results. An options object may be specified to determine overall settings, like sorting and how many objects should be returned.

Parameters

queries : object|array
An Object representing a query, or an Array of Objects representing queries. Each query object can take several properties, which are queried against the history database. Each property is AND'd together, meaning that bookmarks must match each property within a query object. Multiple query objects are then OR'd together.

options : object
Optional options:

Name Type
count number

The number of bookmark items to return. If left undefined, no limit is set.

sort string

A string specifying how the results should be sorted. Possible options are 'title', 'date', 'url', 'visitCount', 'keyword', 'dateAdded' and 'lastModified'.

descending boolean

A boolean specifying whether the results should be in descending order. By default, results are in ascending order.

PlacesEmitter

The PlacesEmitter is not exposed in the module, but returned from the search functions. The PlacesEmitter inherits from event/target, and emits data, error, and end. data events are emitted for every individual search result found, whereas end events are emitted as an aggregate of an entire search, passing in an array of all results into the handler.

Events

data

The data event is emitted for every item returned from a search.

Arguments

Object : This is an object representing a history entry. Contains url, time, accessCount and title of the entry.

error

The error event is emitted whenever a search could not be completed.

Arguments

string : A string indicating the error that occurred.

end

The end event is called when all search results have returned.

Arguments

array : The value passed into the handler is an array of all entries found in the history search. Each entry is an object containing the properties url, time, accessCount and title.