如何获取通过 Google Chrome 上下文菜单右键单击的元素?
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在每个页面中注入一个内容脚本并侦听
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 mustsendRequest
to the tab to get the info about that last clicked element.