Skip to content

Commit d3264da

Browse files
authored
🤖 Merge PR DefinitelyTyped#73999 [chrome] update pageAction namespace (MV2 only) by @erwanjugand
1 parent 80a919b commit d3264da

2 files changed

Lines changed: 103 additions & 44 deletions

File tree

‎types/chrome/index.d.ts‎

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7497,86 +7497,84 @@ declare namespace chrome {
74977497
* MV2 only
74987498
*/
74997499
export namespace pageAction {
7500-
export interface PageActionClickedEvent extends chrome.events.Event<(tab: chrome.tabs.Tab) => void> {}
7501-
75027500
export interface TitleDetails {
75037501
/** The id of the tab for which you want to modify the page action. */
75047502
tabId: number;
75057503
/** The tooltip string. */
75067504
title: string;
75077505
}
75087506

7509-
export interface GetDetails {
7510-
/** Specify the tab to get the title from. */
7507+
export interface TabDetails {
7508+
/** The ID of the tab to query state for. */
75117509
tabId: number;
75127510
}
75137511

75147512
export interface PopupDetails {
75157513
/** The id of the tab for which you want to modify the page action. */
75167514
tabId: number;
7517-
/** The html file to show in a popup. If set to the empty string (''), no popup is shown. */
7515+
/** The relative path to the HTML file to show in a popup. If set to the empty string (`''`), no popup is shown. */
75187516
popup: string;
75197517
}
75207518

7521-
export interface IconDetails {
7522-
/** The id of the tab for which you want to modify the page action. */
7523-
tabId: number;
7524-
/**
7525-
* Optional.
7526-
* @deprecated This argument is ignored.
7527-
*/
7528-
iconIndex?: number | undefined;
7529-
/**
7530-
* Optional.
7531-
* Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals scale, then image with size scale * 19 will be selected. Initially only scales 1 and 2 will be supported. At least one image must be specified. Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'19': foo}'
7532-
*/
7533-
imageData?: ImageData | { [index: number]: ImageData } | undefined;
7534-
/**
7535-
* Optional.
7536-
* Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals scale, then image with size scale * 19 will be selected. Initially only scales 1 and 2 will be supported. At least one image must be specified. Note that 'details.path = foo' is equivalent to 'details.imageData = {'19': foo}'
7537-
*/
7538-
path?: string | { [index: string]: string } | undefined;
7539-
}
7519+
export type IconDetails =
7520+
& {
7521+
/** @deprecated This argument is ignored. */
7522+
iconIndex?: number | undefined;
7523+
/** The id of the tab for which you want to modify the page action. */
7524+
tabId: number;
7525+
}
7526+
& (
7527+
| {
7528+
/** Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals `scale`, then image with size `scale` \* n will be selected, where n is the size of the icon in the UI. At least one image must be specified. Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'16': foo}' */
7529+
imageData: ImageData | { [index: number]: ImageData };
7530+
/** Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals `scale`, then image with size `scale` \* n will be selected, where n is the size of the icon in the UI. At least one image must be specified. Note that 'details.path = foo' is equivalent to 'details.path = {'16': foo}' */
7531+
path?: string | { [index: string]: string } | undefined;
7532+
}
7533+
| {
7534+
/** Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals `scale`, then image with size `scale` \* n will be selected, where n is the size of the icon in the UI. At least one image must be specified. Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'16': foo}' */
7535+
imageData?: ImageData | { [index: number]: ImageData } | undefined;
7536+
/** Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set. If the icon is specified as a dictionary, the actual image to be used is chosen depending on screen's pixel density. If the number of image pixels that fit into one screen space unit equals `scale`, then image with size `scale` \* n will be selected, where n is the size of the icon in the UI. At least one image must be specified. Note that 'details.path = foo' is equivalent to 'details.path = {'16': foo}' */
7537+
path: string | { [index: string]: string };
7538+
}
7539+
);
75407540

75417541
/**
7542-
* Shows the page action. The page action is shown whenever the tab is selected.
7542+
* Hides the page action. Hidden page actions still appear in the Chrome toolbar, but are grayed out.
75437543
* @param tabId The id of the tab for which you want to modify the page action.
7544-
* @param callback Supported since Chrome 67
7544+
* @param callback Since Chrome 67
75457545
*/
75467546
export function hide(tabId: number, callback?: () => void): void;
7547+
75477548
/**
75487549
* Shows the page action. The page action is shown whenever the tab is selected.
75497550
* @param tabId The id of the tab for which you want to modify the page action.
7550-
* @param callback Supported since Chrome 67
7551+
* @param callback Since Chrome 67
75517552
*/
75527553
export function show(tabId: number, callback?: () => void): void;
7554+
75537555
/**
75547556
* Sets the title of the page action. This is displayed in a tooltip over the page action.
7555-
* @param callback Supported since Chrome 67
7557+
* @param callback Since Chrome 67
75567558
*/
75577559
export function setTitle(details: TitleDetails, callback?: () => void): void;
7560+
75587561
/**
7559-
* Sets the html document to be opened as a popup when the user clicks on the page action's icon.
7560-
* @param callback Supported since Chrome 67
7562+
* Sets the HTML document to be opened as a popup when the user clicks on the page action's icon.
7563+
* @param callback Since Chrome 67
75617564
*/
75627565
export function setPopup(details: PopupDetails, callback?: () => void): void;
7563-
/**
7564-
* Gets the title of the page action.
7565-
* @since Chrome 19
7566-
*/
7567-
export function getTitle(details: GetDetails, callback: (result: string) => void): void;
7568-
/**
7569-
* Gets the html document set as the popup for this page action.
7570-
* @since Chrome 19
7571-
*/
7572-
export function getPopup(details: GetDetails, callback: (result: string) => void): void;
7573-
/**
7574-
* Sets the icon for the page action. The icon can be specified either as the path to an image file or as the pixel data from a canvas element, or as dictionary of either one of those. Either the path or the imageData property must be specified.
7575-
*/
7566+
7567+
/** Gets the title of the page action. */
7568+
export function getTitle(details: TabDetails, callback: (result: string) => void): void;
7569+
7570+
/** Gets the html document set as the popup for this page action. */
7571+
export function getPopup(details: TabDetails, callback: (result: string) => void): void;
7572+
7573+
/** Sets the icon for the page action. The icon can be specified either as the path to an image file or as the pixel data from a canvas element, or as dictionary of either one of those. Either the path or the imageData property must be specified. */
75767574
export function setIcon(details: IconDetails, callback?: () => void): void;
75777575

75787576
/** Fired when a page action icon is clicked. This event will not fire if the page action has a popup. */
7579-
export var onClicked: PageActionClickedEvent;
7577+
export const onClicked: events.Event<(tab: chrome.tabs.Tab) => void>;
75807578
}
75817579

75827580
////////////////////

‎types/chrome/test/index.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4784,6 +4784,67 @@ function testI18n() {
47844784
chrome.i18n.getUILanguage(); // $ExpectType string
47854785
}
47864786

4787+
// https://developer.chrome.com/docs/extensions/mv2/reference/pageAction
4788+
function testPageAction() {
4789+
const tabId = 1;
4790+
const getDetails: chrome.pageAction.TabDetails = { tabId };
4791+
4792+
chrome.pageAction.getPopup(getDetails, (result) => { // $ExpectType void
4793+
result; // $ExpectType string
4794+
});
4795+
// @ts-expect-error No matching signature
4796+
chrome.pageAction.getPopup(getDetails);
4797+
4798+
chrome.pageAction.getTitle(getDetails, (result) => { // $ExpectType void
4799+
result; // $ExpectType string
4800+
});
4801+
// @ts-expect-error No matching signature
4802+
chrome.pageAction.getTitle(getDetails);
4803+
4804+
chrome.pageAction.hide(tabId); // $ExpectType void
4805+
chrome.pageAction.hide(tabId, () => void 0); // $ExpectType void
4806+
4807+
const iconDetails: chrome.pageAction.IconDetails = {
4808+
tabId,
4809+
path: "path/to/icon.png",
4810+
};
4811+
4812+
const iconDetails2: chrome.pageAction.IconDetails = {
4813+
tabId,
4814+
imageData: new ImageData(16, 16),
4815+
};
4816+
4817+
chrome.pageAction.setIcon(iconDetails); // $ExpectType void
4818+
chrome.pageAction.setIcon(iconDetails2); // $ExpectType void
4819+
chrome.pageAction.setIcon(iconDetails, () => void 0); // $ExpectType void
4820+
chrome.pageAction.setIcon(iconDetails2, () => void 0); // $ExpectType void
4821+
// @ts-expect-error Either the path or imageData property must be specified.
4822+
chrome.pageAction.setIcon({});
4823+
4824+
const popupDetails: chrome.pageAction.PopupDetails = {
4825+
popup: "popup.html",
4826+
tabId,
4827+
};
4828+
4829+
chrome.pageAction.setPopup(popupDetails); // $ExpectType void
4830+
chrome.pageAction.setPopup(popupDetails, () => void 0); // $ExpectType void
4831+
4832+
const titleDetails: chrome.pageAction.TitleDetails = {
4833+
title: "My Page Action",
4834+
tabId,
4835+
};
4836+
4837+
chrome.pageAction.setTitle(titleDetails); // $ExpectType void
4838+
chrome.pageAction.setTitle(titleDetails, () => void 0); // $ExpectType void
4839+
4840+
chrome.pageAction.show(tabId); // $ExpectType void
4841+
chrome.pageAction.show(tabId, () => void 0); // $ExpectType void
4842+
4843+
checkChromeEvent(chrome.pageAction.onClicked, (tab) => {
4844+
tab; // $ExpectType Tab
4845+
});
4846+
}
4847+
47874848
// https://developer.chrome.com/docs/extensions/reference/api/pageCapture
47884849
function testPageCapture() {
47894850
const details = { tabId: 0 };

0 commit comments

Comments
 (0)