Skip to content

Commit 52c8ef6

Browse files
authored
🤖 Merge PR DefinitelyTyped#73535 [chrome] update history namespace by @erwanjugand
1 parent 47ce111 commit 52c8ef6

File tree

2 files changed

+161
-79
lines changed

2 files changed

+161
-79
lines changed

types/chrome/index.d.ts

Lines changed: 84 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5815,46 +5815,81 @@ declare namespace chrome {
58155815
/** An object encapsulating one visit to a URL. */
58165816
export interface VisitItem {
58175817
/** The transition type for this visit from its referrer. */
5818-
transition: string;
5819-
/** Optional. When this visit occurred, represented in milliseconds since the epoch. */
5820-
visitTime?: number | undefined;
5818+
transition: `${TransitionType}`;
5819+
/**
5820+
* True if the visit originated on this device. False if it was synced from a different device
5821+
* @since Chrome 115
5822+
*/
5823+
isLocal: boolean;
5824+
/** When this visit occurred, represented in milliseconds since the epoch. */
5825+
visitTime?: number;
58215826
/** The unique identifier for this visit. */
58225827
visitId: string;
58235828
/** The visit ID of the referrer. */
58245829
referringVisitId: string;
5825-
/** The unique identifier for the item. */
5830+
/** The unique identifier for the corresponding {@link history.HistoryItem}. */
58265831
id: string;
58275832
}
58285833

58295834
/** An object encapsulating one result of a history query. */
58305835
export interface HistoryItem {
5831-
/** Optional. The number of times the user has navigated to this page by typing in the address. */
5832-
typedCount?: number | undefined;
5833-
/** Optional. The title of the page when it was last loaded. */
5834-
title?: string | undefined;
5835-
/** Optional. The URL navigated to by a user. */
5836-
url?: string | undefined;
5837-
/** Optional. When this page was last loaded, represented in milliseconds since the epoch. */
5838-
lastVisitTime?: number | undefined;
5839-
/** Optional. The number of times the user has navigated to this page. */
5840-
visitCount?: number | undefined;
5836+
/** The number of times the user has navigated to this page by typing in the address. */
5837+
typedCount?: number;
5838+
/** The title of the page when it was last loaded. */
5839+
title?: string;
5840+
/** The URL navigated to by a user. */
5841+
url?: string;
5842+
/** When this page was last loaded, represented in milliseconds since the epoch. */
5843+
lastVisitTime?: number;
5844+
/** The number of times the user has navigated to this page. */
5845+
visitCount?: number;
58415846
/** The unique identifier for the item. */
58425847
id: string;
58435848
}
58445849

5850+
/**
5851+
* The transition type for this visit from its referrer.
5852+
* @since Chrome 44
5853+
*/
5854+
export enum TransitionType {
5855+
/** The user arrived at this page by clicking a link on another page. */
5856+
LINK = "link",
5857+
/** The user arrived at this page by typing the URL in the address bar. This is also used for other explicit navigation actions. */
5858+
TYPED = "typed",
5859+
/** The user arrived at this page through a suggestion in the UI, for example, through a menu item. */
5860+
AUTO_BOOKMARK = "auto_bookmark",
5861+
/** The user arrived at this page through subframe navigation that they didn't request, such as through an ad loading in a frame on the previous page. These don't always generate new navigation entries in the back and forward menus. */
5862+
AUTO_SUBFRAME = "auto_subframe",
5863+
/** The user arrived at this page by selecting something in a subframe. */
5864+
MANUAL_SUBFRAME = "manual_subframe",
5865+
/** The user arrived at this page by typing in the address bar and selecting an entry that didn't look like a URL, such as a Google Search suggestion. For example, a match might have the URL of a Google Search result page, but it might appear to the user as "Search Google for ...". These are different from typed navigations because the user didn't type or see the destination URL. They're also related to keyword navigations. */
5866+
GENERATED = "generated",
5867+
/** The page was specified in the command line or is the start page. */
5868+
AUTO_TOPLEVEL = "auto_toplevel",
5869+
/** The user arrived at this page by filling out values in a form and submitting the form. Not all form submissions use this transition type. */
5870+
FORM_SUBMIT = "form_submit",
5871+
/** The user reloaded the page, either by clicking the reload button or by pressing Enter in the address bar. Session restore and Reopen closed tab also use this transition type. */
5872+
RELOAD = "reload",
5873+
/** The URL for this page was generated from a replaceable keyword other than the default search provider. */
5874+
KEYWORD = "keyword",
5875+
/** Corresponds to a visit generated for a keyword. */
5876+
KEYWORD_GENERATED = "keyword_generated",
5877+
}
5878+
58455879
export interface HistoryQuery {
5846-
/** A free-text query to the history service. Leave empty to retrieve all pages. */
5880+
/** A free-text query to the history service. Leave this empty to retrieve all pages. */
58475881
text: string;
5848-
/** Optional. The maximum number of results to retrieve. Defaults to 100. */
5882+
/** The maximum number of results to retrieve. Defaults to 100. */
58495883
maxResults?: number | undefined;
5850-
/** Optional. Limit results to those visited after this date, represented in milliseconds since the epoch. */
5884+
/** Limit results to those visited after this date, represented in milliseconds since the epoch. If property is not specified, it will default to 24 hours. */
58515885
startTime?: number | undefined;
5852-
/** Optional. Limit results to those visited before this date, represented in milliseconds since the epoch. */
5886+
/** Limit results to those visited before this date, represented in milliseconds since the epoch. */
58535887
endTime?: number | undefined;
58545888
}
58555889

5856-
export interface Url {
5857-
/** The URL for the operation. It must be in the format as returned from a call to history.search. */
5890+
/** @since Chrome 88 */
5891+
export interface UrlDetails {
5892+
/** The URL for the operation. It must be in the format as returned from a call to {@link history.search}. */
58585893
url: string;
58595894
}
58605895

@@ -5868,73 +5903,62 @@ declare namespace chrome {
58685903
export interface RemovedResult {
58695904
/** True if all history was removed. If true, then urls will be empty. */
58705905
allHistory: boolean;
5871-
/** Optional. */
5872-
urls?: string[] | undefined;
5906+
urls?: string[];
58735907
}
58745908

5875-
export interface HistoryVisitedEvent extends chrome.events.Event<(result: HistoryItem) => void> {}
5876-
5877-
export interface HistoryVisitRemovedEvent extends chrome.events.Event<(removed: RemovedResult) => void> {}
5878-
58795909
/**
58805910
* Searches the history for the last visit time of each page matching the query.
5881-
* @return The `search` method provides its result via callback or returned as a `Promise` (MV3 only).
5911+
*
5912+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
58825913
*/
58835914
export function search(query: HistoryQuery): Promise<HistoryItem[]>;
5884-
/**
5885-
* Searches the history for the last visit time of each page matching the query.
5886-
*/
58875915
export function search(query: HistoryQuery, callback: (results: HistoryItem[]) => void): void;
5916+
58885917
/**
58895918
* Adds a URL to the history at the current time with a transition type of "link".
5890-
* @return The `addUrl` method provides its result via callback or returned as a `Promise` (MV3 only). It has no parameters.
5891-
*/
5892-
export function addUrl(details: Url): Promise<void>;
5893-
/**
5894-
* Adds a URL to the history at the current time with a transition type of "link".
5919+
*
5920+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
58955921
*/
5896-
export function addUrl(details: Url, callback: () => void): void;
5922+
export function addUrl(details: UrlDetails): Promise<void>;
5923+
export function addUrl(details: UrlDetails, callback: () => void): void;
5924+
58975925
/**
58985926
* Removes all items within the specified date range from the history. Pages will not be removed from the history unless all visits fall within the range.
5899-
* @return The `deleteRange` method provides its result via callback or returned as a `Promise` (MV3 only). It has no parameters.
5927+
*
5928+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
59005929
*/
59015930
export function deleteRange(range: Range): Promise<void>;
5902-
/**
5903-
* Removes all items within the specified date range from the history. Pages will not be removed from the history unless all visits fall within the range.
5904-
*/
59055931
export function deleteRange(range: Range, callback: () => void): void;
5932+
59065933
/**
59075934
* Deletes all items from the history.
5908-
* @return The `deleteAll` method provides its result via callback or returned as a `Promise` (MV3 only). It has no parameters.
5935+
*
5936+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
59095937
*/
59105938
export function deleteAll(): Promise<void>;
5911-
/**
5912-
* Deletes all items from the history.
5913-
*/
59145939
export function deleteAll(callback: () => void): void;
5940+
59155941
/**
59165942
* Retrieves information about visits to a URL.
5917-
* @return The `getVisits` method provides its result via callback or returned as a `Promise` (MV3 only).
5918-
*/
5919-
export function getVisits(details: Url): Promise<VisitItem[]>;
5920-
/**
5921-
* Retrieves information about visits to a URL.
5922-
*/
5923-
export function getVisits(details: Url, callback: (results: VisitItem[]) => void): void;
5924-
/**
5925-
* Removes all occurrences of the given URL from the history.
5926-
* @return The `deleteUrl` method provides its result via callback or returned as a `Promise` (MV3 only). It has no parameters.
5943+
*
5944+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
59275945
*/
5928-
export function deleteUrl(details: Url): Promise<void>;
5946+
export function getVisits(details: UrlDetails): Promise<VisitItem[]>;
5947+
export function getVisits(details: UrlDetails, callback: (results: VisitItem[]) => void): void;
5948+
59295949
/**
59305950
* Removes all occurrences of the given URL from the history.
5951+
*
5952+
* Can return its result via Promise in Manifest V3 or later since Chrome 96.
59315953
*/
5932-
export function deleteUrl(details: Url, callback: () => void): void;
5954+
export function deleteUrl(details: UrlDetails): Promise<void>;
5955+
export function deleteUrl(details: UrlDetails, callback: () => void): void;
5956+
5957+
/** Fired when a URL is visited, providing the {@link HistoryItem} data for that URL. This event fires before the page has loaded. */
5958+
export const onVisited: events.Event<(result: HistoryItem) => void>;
59335959

5934-
/** Fired when a URL is visited, providing the HistoryItem data for that URL. This event fires before the page has loaded. */
5935-
export var onVisited: HistoryVisitedEvent;
5936-
/** Fired when one or more URLs are removed from the history service. When all visits have been removed the URL is purged from history. */
5937-
export var onVisitRemoved: HistoryVisitRemovedEvent;
5960+
/** Fired when one or more URLs are removed from history. When all visits have been removed the URL is purged from history. */
5961+
export const onVisitRemoved: events.Event<(removed: RemovedResult) => void>;
59385962
}
59395963

59405964
////////////////////

types/chrome/test/index.ts

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,30 +4806,88 @@ function testGcm() {
48064806
});
48074807
}
48084808

4809-
// https://developer.chrome.com/docs/extensions/reference/history
4809+
// https://developer.chrome.com/docs/extensions/reference/api/history
48104810
function testHistory() {
4811+
chrome.history.TransitionType.AUTO_BOOKMARK === "auto_bookmark";
4812+
chrome.history.TransitionType.AUTO_SUBFRAME === "auto_subframe";
4813+
chrome.history.TransitionType.AUTO_TOPLEVEL === "auto_toplevel";
4814+
chrome.history.TransitionType.FORM_SUBMIT === "form_submit";
4815+
chrome.history.TransitionType.GENERATED === "generated";
4816+
chrome.history.TransitionType.KEYWORD === "keyword";
4817+
chrome.history.TransitionType.KEYWORD_GENERATED === "keyword_generated";
4818+
chrome.history.TransitionType.LINK === "link";
4819+
chrome.history.TransitionType.MANUAL_SUBFRAME === "manual_subframe";
4820+
chrome.history.TransitionType.RELOAD === "reload";
4821+
chrome.history.TransitionType.TYPED === "typed";
4822+
4823+
const urlDetails: chrome.history.UrlDetails = {
4824+
url: "https://example.com",
4825+
};
4826+
4827+
chrome.history.addUrl(urlDetails); // $ExpectType Promise<void>
4828+
chrome.history.addUrl(urlDetails, () => void 0); // $ExpectType void
48114829
// @ts-expect-error
4812-
chrome.history.search({}, (results) => {});
4813-
chrome.history.search({ text: "" }, (results) => {});
4830+
chrome.history.addUrl(urlDetails, () => {}).then(() => {});
4831+
4832+
chrome.history.deleteAll(); // $ExpectType Promise<void>
4833+
chrome.history.deleteAll(() => void 0); // $ExpectType void
4834+
// @ts-expect-error
4835+
chrome.history.deleteAll(() => {}).then(() => {});
4836+
4837+
const range: chrome.history.Range = {
4838+
endTime: new Date().getTime(),
4839+
startTime: new Date().getTime(),
4840+
};
4841+
4842+
chrome.history.deleteRange(range); // $ExpectType Promise<void>
4843+
chrome.history.deleteRange(range, () => void 0); // $ExpectType void
48144844
// @ts-expect-error
4815-
chrome.history.addUrl({}, () => {});
4816-
chrome.history.addUrl({ url: "https://example.com" }, () => {});
4845+
chrome.history.deleteRange(range, () => {}).then(() => {});
4846+
4847+
chrome.history.deleteUrl(urlDetails); // $ExpectType Promise<void>
4848+
chrome.history.deleteUrl(urlDetails, () => void 0); // $ExpectType void
48174849
// @ts-expect-error
4818-
chrome.history.deleteRange({}, () => {});
4819-
chrome.history.deleteRange({ startTime: 1646172000000, endTime: 1646258400000 }, () => {});
4820-
chrome.history.deleteAll(() => {});
4821-
chrome.history.deleteUrl({ url: "https://example.com" }, () => {});
4822-
chrome.history.getVisits({ url: "https://example.com" }, () => {});
4823-
}
4850+
chrome.history.deleteUrl(urlDetails, () => {}).then(() => {});
4851+
4852+
chrome.history.getVisits(urlDetails); // $ExpectType Promise<VisitItem[]>
4853+
chrome.history.getVisits(urlDetails, ([result]) => { // $ExpectType void
4854+
result.id; // $ExpectType string
4855+
result.isLocal; // $ExpectType boolean
4856+
result.referringVisitId; // $ExpectType string
4857+
result.transition; // $ExpectType "link" | "typed" | "auto_bookmark" | "auto_subframe" | "manual_subframe" | "generated" | "auto_toplevel" | "form_submit" | "reload" | "keyword" | "keyword_generated"
4858+
result.visitId; // $ExpectType string
4859+
result.visitTime; // $ExpectType number | undefined
4860+
});
4861+
// @ts-expect-error
4862+
chrome.history.getVisits(urlDetails, () => {}).then(() => {});
4863+
4864+
const query: chrome.history.HistoryQuery = {
4865+
endTime: new Date().getTime(),
4866+
maxResults: 2,
4867+
startTime: new Date().getTime(),
4868+
text: "example",
4869+
};
4870+
4871+
chrome.history.search(query); // $ExpectType Promise<HistoryItem[]>
4872+
chrome.history.search(query, ([result]) => { // $ExpectType void
4873+
result.id; // $ExpectType string
4874+
result.lastVisitTime; // $ExpectType number | undefined
4875+
result.title; // $ExpectType string | undefined
4876+
result.typedCount; // $ExpectType number | undefined
4877+
result.url; // $ExpectType string | undefined
4878+
result.visitCount; // $ExpectType number | undefined
4879+
});
4880+
// @ts-expect-error
4881+
chrome.history.search(query, () => {}).then(() => {});
48244882

4825-
// https://developer.chrome.com/docs/extensions/reference/history
4826-
async function testHistoryForPromise() {
4827-
await chrome.history.search({ text: "" });
4828-
await chrome.history.addUrl({ url: "https://example.com" });
4829-
await chrome.history.deleteRange({ startTime: 1646172000000, endTime: 1646258400000 });
4830-
await chrome.history.deleteAll();
4831-
await chrome.history.deleteUrl({ url: "https://example.com" });
4832-
await chrome.history.getVisits({ url: "https://example.com" });
4883+
checkChromeEvent(chrome.history.onVisited, (result) => {
4884+
result; // $ExpectType HistoryItem
4885+
});
4886+
4887+
checkChromeEvent(chrome.history.onVisitRemoved, (result) => {
4888+
result.allHistory; // $ExpectType boolean
4889+
result.urls; // $ExpectType string[] | undefined
4890+
});
48334891
}
48344892

48354893
// https://developer.chrome.com/docs/extensions/reference/api/identity

0 commit comments

Comments
 (0)