How can I get the element that was right-clicked via a Google Chrome context menu?

与世无争的帅哥 提交于 2019-12-04 11:36:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5642603/how-can-i-get-the-element-that-was-right-clicked-via-a-google-chrome-context-men

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!