Skip to content

Commit 1c06248

Browse files
authored
🤖 Merge PR DefinitelyTyped#72253 [Chrome] update contextMenus namespaces by @erwanjugand
1 parent c68a4e7 commit 1c06248

File tree

2 files changed

+191
-251
lines changed

2 files changed

+191
-251
lines changed

‎types/chrome/index.d.ts‎

Lines changed: 116 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,198 +1865,146 @@ declare namespace chrome {
18651865
* Permissions: "contextMenus"
18661866
*/
18671867
export namespace contextMenus {
1868-
export interface OnClickData {
1869-
/**
1870-
* Optional.
1871-
* @since Chrome 35
1872-
* The text for the context selection, if any.
1873-
*/
1874-
selectionText?: string | undefined;
1875-
/**
1876-
* Optional.
1877-
* @since Chrome 35
1878-
* A flag indicating the state of a checkbox or radio item after it is clicked.
1879-
*/
1880-
checked?: boolean | undefined;
1881-
/**
1882-
* @since Chrome 35
1883-
* The ID of the menu item that was clicked.
1884-
*/
1885-
menuItemId: number | string;
1886-
/**
1887-
* Optional.
1888-
* @since Chrome 35
1889-
* The ID of the frame of the element where the context menu was
1890-
* clicked, if it was in a frame.
1891-
*/
1892-
frameId?: number | undefined;
1893-
/**
1894-
* Optional.
1895-
* @since Chrome 35
1896-
* The URL of the frame of the element where the context menu was clicked, if it was in a frame.
1897-
*/
1898-
frameUrl?: string | undefined;
1899-
/**
1900-
* @since Chrome 35
1901-
* A flag indicating whether the element is editable (text input, textarea, etc.).
1902-
*/
1903-
editable: boolean;
1904-
/**
1905-
* Optional.
1906-
* @since Chrome 35
1907-
* One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements.
1908-
*/
1909-
mediaType?: "image" | "video" | "audio" | undefined;
1910-
/**
1911-
* Optional.
1912-
* @since Chrome 35
1913-
* A flag indicating the state of a checkbox or radio item before it was clicked.
1914-
*/
1915-
wasChecked?: boolean | undefined;
1916-
/**
1917-
* @since Chrome 35
1918-
* The URL of the page where the menu item was clicked. This property is not set if the click occurred in a context where there is no current page, such as in a launcher context menu.
1919-
*/
1920-
pageUrl: string;
1921-
/**
1922-
* Optional.
1923-
* @since Chrome 35
1924-
* If the element is a link, the URL it points to.
1925-
*/
1926-
linkUrl?: string | undefined;
1927-
/**
1928-
* Optional.
1929-
* @since Chrome 35
1930-
* The parent ID, if any, for the item clicked.
1931-
*/
1932-
parentMenuItemId?: number | string;
1933-
/**
1934-
* Optional.
1935-
* @since Chrome 35
1936-
* Will be present for elements with a 'src' URL.
1937-
*/
1938-
srcUrl?: string | undefined;
1868+
/**
1869+
* The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'launcher'. The 'launcher' context is only supported by apps and is used to add menu items to the context menu that appears when clicking the app icon in the launcher/taskbar/dock/etc. Different platforms might put limitations on what is actually supported in a launcher context menu.
1870+
* @since Chrome 44
1871+
*/
1872+
export enum ContextType {
1873+
ALL = "all",
1874+
PAGE = "page",
1875+
FRAME = "frame",
1876+
SELECTION = "selection",
1877+
LINK = "link",
1878+
EDITABLE = "editable",
1879+
IMAGE = "image",
1880+
VIDEO = "video",
1881+
AUDIO = "audio",
1882+
LAUNCHER = "launcher",
1883+
BROWSER_ACTION = "browser_action",
1884+
PAGE_ACTION = "page_action",
1885+
ACTION = "action",
19391886
}
19401887

1941-
type ContextType =
1942-
| "all"
1943-
| "page"
1944-
| "frame"
1945-
| "selection"
1946-
| "link"
1947-
| "editable"
1948-
| "image"
1949-
| "video"
1950-
| "audio"
1951-
| "launcher"
1952-
| "browser_action"
1953-
| "page_action"
1954-
| "action";
1955-
1956-
type ContextItemType = "normal" | "checkbox" | "radio" | "separator";
1957-
1888+
/**
1889+
* Properties of the new context menu item.
1890+
* @since Chrome 123
1891+
*/
19581892
export interface CreateProperties {
1959-
/** Optional. Lets you restrict the item to apply only to documents whose URL matches one of the given patterns. (This applies to frames as well.) For details on the format of a pattern, see Match Patterns. */
1960-
documentUrlPatterns?: string[] | undefined;
1961-
/** Optional. The initial state of a checkbox or radio item: true for selected and false for unselected. Only one radio item can be selected at a time in a given group of radio items. */
1962-
checked?: boolean | undefined;
1963-
/** Optional. The text to be displayed in the item; this is required unless type is 'separator'. When the context is 'selection', you can use %s within the string to show the selected text. For example, if this parameter's value is "Translate '%s' to Pig Latin" and the user selects the word "cool", the context menu item for the selection is "Translate 'cool' to Pig Latin". */
1964-
title?: string | undefined;
1965-
/** Optional. List of contexts this menu item will appear in. Defaults to ['page'] if not specified. */
1966-
contexts?: ContextType | ContextType[] | undefined;
1967-
/**
1968-
* Optional.
1969-
* @since Chrome 20
1970-
* Whether this context menu item is enabled or disabled. Defaults to true.
1971-
*/
1972-
enabled?: boolean | undefined;
1973-
/** Optional. Similar to documentUrlPatterns, but lets you filter based on the src attribute of img/audio/video tags and the href of anchor tags. */
1974-
targetUrlPatterns?: string[] | undefined;
1975-
/**
1976-
* Optional.
1977-
* A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for chrome.contextMenus.onClicked.
1978-
* @param info Information sent when a context menu item is clicked.
1979-
* @param tab The details of the tab where the click took place. Note: this parameter only present for extensions.
1980-
*/
1981-
onclick?: ((info: OnClickData, tab: chrome.tabs.Tab) => void) | undefined;
1982-
/** Optional. The ID of a parent menu item; this makes the item a child of a previously added item. */
1983-
parentId?: number | string | undefined;
1984-
/** Optional. The type of menu item. Defaults to 'normal' if not specified. */
1985-
type?: ContextItemType | undefined;
1986-
/**
1987-
* Optional.
1988-
* @since Chrome 21
1989-
* The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.
1990-
*/
1991-
id?: string | undefined;
1893+
/** The initial state of a checkbox or radio button: `true` for selected, `false` for unselected. Only one radio button can be selected at a time in a given group. */
1894+
checked?: boolean;
1895+
/** List of contexts this menu item will appear in. Defaults to `['page']`. */
1896+
contexts?: [`${ContextType}`, ...`${ContextType}`[]];
1897+
/** Restricts the item to apply only to documents or frames whose URL matches one of the given patterns. For details on pattern formats, see Match Patterns. */
1898+
documentUrlPatterns?: string[];
1899+
/** Whether this context menu item is enabled or disabled. Defaults to `true`. */
1900+
enabled?: boolean;
1901+
/** The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension. */
1902+
id?: string;
1903+
/** The ID of a parent menu item; this makes the item a child of a previously added item. */
1904+
parentId?: number | string;
1905+
/** Similar to `documentUrlPatterns`, filters based on the `src` attribute of `img`, `audio`, and `video` tags and the `href` attribute of `a` tags. */
1906+
targetUrlPatterns?: string[];
1907+
/** The text to display in the item; this is _required_ unless `type` is `separator`. When the context is `selection`, use `%s` within the string to show the selected text. For example, if this parameter's value is "Translate '%s' to Pig Latin" and the user selects the word "cool", the context menu item for the selection is "Translate 'cool' to Pig Latin". */
1908+
title?: string;
1909+
/** The type of menu item. Defaults to `normal`. */
1910+
type?: `${ItemType}`;
1911+
/** Whether the item is visible in the menu. */
1912+
visible?: boolean;
19921913
/**
1993-
* Optional.
1994-
* @since Chrome 62
1995-
* Whether the item is visible in the menu.
1914+
* A function that is called back when the menu item is clicked. This is not available inside of a service worker; instead, you should register a listener for {@link contextMenus.onClicked}.
1915+
* @param info Information about the item clicked and the context where the click happened.
1916+
* @param tab The details of the tab where the click took place. This parameter is not present for platform apps.
19961917
*/
1997-
visible?: boolean | undefined;
1918+
onclick?: (
1919+
info: OnClickData,
1920+
tab: tabs.Tab,
1921+
) => void;
19981922
}
19991923

2000-
export interface UpdateProperties extends Omit<CreateProperties, "id"> {}
2001-
2002-
export interface MenuClickedEvent
2003-
extends chrome.events.Event<(info: OnClickData, tab?: chrome.tabs.Tab) => void>
2004-
{}
2005-
20061924
/**
2007-
* @since Chrome 38
2008-
* The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored.
1925+
* The type of menu item.
1926+
* @since Chrome 44
20091927
*/
2010-
export var ACTION_MENU_TOP_LEVEL_LIMIT: number;
1928+
export enum ItemType {
1929+
NORMAL = "normal",
1930+
CHECKBOX = "checkbox",
1931+
RADIO = "radio",
1932+
SEPARATOR = "separator",
1933+
}
1934+
1935+
/** Information sent when a context menu item is clicked. */
1936+
export interface OnClickData {
1937+
/** A flag indicating the state of a checkbox or radio item after it is clicked. */
1938+
checked?: boolean;
1939+
/** A flag indicating whether the element is editable (text input, textarea, etc.). */
1940+
editable: boolean;
1941+
/**
1942+
* The ID of the frame of the element where the context menu was clicked, if it was in a frame.
1943+
* @since Chrome 51
1944+
*/
1945+
frameId?: number;
1946+
/** The URL of the frame of the element where the context menu was clicked, if it was in a frame. */
1947+
frameUrl?: string;
1948+
/** If the element is a link, the URL it points to. */
1949+
linkUrl?: string;
1950+
/** One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements. */
1951+
mediaType?: `${ContextType.IMAGE}` | `${ContextType.VIDEO}` | `${ContextType.AUDIO}`;
1952+
/** The ID of the menu item that was clicked. */
1953+
menuItemId: number | string;
1954+
/** The URL of the page where the menu item was clicked. This property is not set if the click occurred in a context where there is no current page, such as in a launcher context menu. */
1955+
pageUrl?: string;
1956+
/** The parent ID, if any, for the item clicked.*/
1957+
parentMenuItemId?: number | string;
1958+
/** The text for the context selection, if any. */
1959+
selectionText?: string | undefined;
1960+
/** Will be present for elements with a 'src' URL. */
1961+
srcUrl?: string | undefined;
1962+
/** A flag indicating the state of a checkbox or radio item before it was clicked. */
1963+
wasChecked?: boolean | undefined;
1964+
}
1965+
1966+
/** The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored. */
1967+
export const ACTION_MENU_TOP_LEVEL_LIMIT: 6;
20111968

20121969
/**
2013-
* Removes all context menu items added by this extension.
2014-
* @since Chrome 123
2015-
*/
2016-
export function removeAll(): Promise<void>;
2017-
/**
2018-
* Removes all context menu items added by this extension.
2019-
* @param callback Called when removal is complete.
2020-
*/
2021-
export function removeAll(callback: () => void): void;
2022-
/**
2023-
* Creates a new context menu item. Note that if an error occurs during creation, you may not find out until the creation callback fires (the details will be in chrome.runtime.lastError).
2024-
* @param callback Called when the item has been created in the browser. If there were any problems creating the item, details will be available in chrome.runtime.lastError.
1970+
* Creates a new context menu item. If an error occurs during creation, it may not be detected until the creation callback fires; details will be in {@link chrome.runtime.lastError}.
20251971
* @return The ID of the newly created item.
20261972
*/
20271973
export function create(createProperties: CreateProperties, callback?: () => void): number | string;
2028-
/**
2029-
* Updates a previously created context menu item.
2030-
* @param id The ID of the item to update.
2031-
* @param updateProperties The properties to update. Accepts the same values as the create function.
2032-
* @since Chrome 123
2033-
*/
2034-
export function update(id: string | number, updateProperties: UpdateProperties): Promise<void>;
2035-
/**
2036-
* Updates a previously created context menu item.
2037-
* @param id The ID of the item to update.
2038-
* @param updateProperties The properties to update. Accepts the same values as the create function.
2039-
* @param callback Called when the context menu has been updated.
2040-
*/
2041-
export function update(id: string | number, updateProperties: UpdateProperties, callback: () => void): void;
1974+
20421975
/**
20431976
* Removes a context menu item.
20441977
* @param menuItemId The ID of the context menu item to remove.
2045-
* @since Chrome 123
1978+
*
1979+
* Can return its result via Promise since Chrome 123.
20461980
*/
20471981
export function remove(menuItemId: string | number): Promise<void>;
1982+
export function remove(menuItemId: string | number, callback: () => void): void;
1983+
20481984
/**
2049-
* Removes a context menu item.
2050-
* @param menuItemId The ID of the context menu item to remove.
2051-
* @param callback Called when the context menu has been removed.
1985+
* Removes all context menu items added by this extension.
1986+
*
1987+
* Can return its result via Promise since Chrome 123.
20521988
*/
2053-
export function remove(menuItemId: string | number, callback: () => void): void;
1989+
export function removeAll(): Promise<void>;
1990+
export function removeAll(callback: () => void): void;
20541991

20551992
/**
2056-
* @since Chrome 21
2057-
* Fired when a context menu item is clicked.
1993+
* Updates a previously created context menu item.
1994+
* @param id The ID of the item to update.
1995+
* @param updateProperties The properties to update. Accepts the same values as the {@link contextMenus.create} function.
1996+
*
1997+
* Can return its result via Promise since Chrome 123.
20581998
*/
2059-
export var onClicked: MenuClickedEvent;
1999+
export function update(id: string | number, updateProperties: Omit<CreateProperties, "id">): Promise<void>;
2000+
export function update(
2001+
id: string | number,
2002+
updateProperties: Omit<CreateProperties, "id">,
2003+
callback: () => void,
2004+
): void;
2005+
2006+
/** Fired when a context menu item is clicked. */
2007+
export const onClicked: events.Event<(info: OnClickData, tab?: tabs.Tab) => void>;
20602008
}
20612009

20622010
////////////////////

0 commit comments

Comments
 (0)