Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

menus.getTargetElement()

Returns the element for a giventargetElementId

This method is available to all extension script contexts (content scripts, background pages and other extension pages) and returns the element for a giveninfo.targetElementId, provided that the element still exists in the document where the method is invoked.

The method only works in the document that includes the right-clicked element and thetargetElementId expires when the user opens another context menu.

Note:menus.getTargetElement only return the requested element if called in the same context as the document that contains the element, for example using content scripts (as shown in the example below).

An extension requires the "menus" permission to use this API.

Syntax

js
let elem = browser.menus.getTargetElement(targetElementId);

Parameters

targetElementId

The property of themenus.OnClickData object passed to themenus.onClicked handler ormenus.onShown event.

Return value

The element referred to by thetargetElementId parameter. If thetargetElementId parameter is not valid, the method returnsnull.

Examples

The following example uses thegetTargetElement method to get the element referred to by theinfo.targetElementId property and then removes it.

js
browser.menus.create({  title: "Remove element",  documentUrlPatterns: ["*://*/*"],  contexts: [    "audio",    "editable",    "frame",    "image",    "link",    "page",    "password",    "video",  ],  onclick(info, tab) {    browser.tabs.executeScript(tab.id, {      frameId: info.frameId,      code: `browser.menus.getTargetElement(${info.targetElementId}).remove();`,    });  },});

Example extensions

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp