如何获取通过 G​​oogle Chrome 上下文菜单右键单击的元素?

发布于 2024-10-31 18:12:55 字数 542 浏览 5 评论 0原文

我正在 Google Chrome 中创建一个上下文菜单项,如下所示:

chrome.contextMenus.create({
 "title":"My Context Menu Item", 
 "contexts":["editable"], 
 "onclick": onClick
});

onClick 回调接收两个参数:OnClickInfo 对象和选项卡对象。这两个对象似乎都不包含对调用上下文菜单时右键单击的 DOM 元素的引用。我有 console.logged 这两个对象,但在任何地方都没有看到任何对目标元素的引用。

期望的结果:

  • 用户右键单击可编辑元素,
  • 用户在 onClick 回调中单击我的上下文菜单项
  • 对于目标右键单击元素的父“form”元素

,让我的扩展搜索相 目前来看,Chrome 似乎没有提供任何对目标元素的引用。有没有人找到解决这个限制的方法?

I am creating a context menu item in Google chrome like so:

chrome.contextMenus.create({
 "title":"My Context Menu Item", 
 "contexts":["editable"], 
 "onclick": onClick
});

The onClick callback receives two arguments: an OnClickInfo object and a tab object. Neither object appears to contain a reference to the DOM element that was right-clicked when invoking the context menu. I have console.logged both of these objects but don't see any reference to the targeted element anywhere.

The desired outcome:

  • user right-clicks an editable element
  • user clicks my context menu item
  • within the onClick callback, have my extension search for a parent 'form' element relative to the targeted right-click element

As it stands right now, it doesn't look like Chrome offers any reference to the targeted element. Has anyone found a way around this limitation?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-11-07 18:12:55

您必须在每个页面中注入一个内容脚本并侦听 mousedown 事件并记录最后单击的元素。然后,当调用上下文菜单回调时,您必须 sendRequest 到选项卡以获取有关上次单击的元素的信息。

You must inject a content script in each page and listen for mousedown event and log the last clicked element. Then, when a context menu callback is called you must sendRequest to the tab to get the info about that last clicked element.

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