menus.getTargetElement() 编辑

Returns the element for a given targetElementId

This method is available to all extension script contexts (content scripts, background pages and other extension pages) and returns the element for a given info.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 the targetElementId 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

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

Parameters

targetElementId
The property of the menus.OnClickData object passed to the menus.onClicked handler or menus.onShown event.

Return value

The element referred to by the targetElementId parameter. If the targetElementId parameter is not valid, the method returns null.

Examples

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

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

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:95 次

字数:3465

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文