在Chrome上右键单击获取超链接文本 [英] getting hyperlink text on Chrome right click
问题描述
在我目前正在使用的Chrome扩展程序中,有一种情况是用户右键单击此链接:
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>
,然后从扩展程序的上下文菜单中选择某个选项,我需要以某种形式捕获字符串"Hello".如上下文菜单API 所示,很容易从右键单击中捕获文本选择或超链接的实际URL(在本例中为Google.com),因为它们出现在OnClickData中,但是我不确定如何从链接中捕获文本.
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.
推荐答案
似乎没有简单的方法可以做到这一点.但是在实现这种功能之前,有一些解决方法.来自 http://code.google.com/p/chromium/issues/detail?id = 39507
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
"我发现了一个可以暂时解决的黑客.我在Cloudboard中使用了它: 您可以在页面上执行脚本,并使用:document.activeElement获取当前选定的元素.您可以使用: document.activeElement.selectionStart,document.activeElement.selectionEnd以获取所选文本和document.activeElement.value.substr()"
"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()"
这篇关于在Chrome上右键单击获取超链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!