getting hyperlink text on Chrome right click

允我心安 提交于 2020-01-01 11:39:11

问题


In the Chrome extension I'm working on right now, there's a situation where if the user right-clicked on this link:

<a href="http://www.google.com">Hello</a>

and selected a certain option from my extension's context menu, I need the string "Hello" to be captured in some form. As the Context Menus API shows, it's easy to capture text from a right-clicked selection or the actual URL of the hyperlink (in this case, Google.com) , since these appear in OnClickData, but I'm not sure how I would capture the text from a link.

As a side note, if a user highlights a hyperlink and tries to use my context menu, it doesn't work. However, it works properly if the user highlights normal text. I do have "link" and "selection" enabled under "contexts" when I create the context menu option.


回答1:


It seems, that there's no straightforward way to do it. But there are some workarounds until this kind of feature is implemented. From http://code.google.com/p/chromium/issues/detail?id=39507

"I figured out a hack that you can use to get around it for now. I used it in Cloudboard: You can executeScript on the page and use: document.activeElement to get the currently selected element. You can use: document.activeElement.selectionStart, document.activeElement.selectionEnd to get the selected text and document.activeElement.value.substr()"



来源:https://stackoverflow.com/questions/7427357/getting-hyperlink-text-on-chrome-right-click

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