在 Chrome 上右键单击获取超链接文本
在我现在正在开发的 Chrome 扩展程序中,存在一种情况,如果用户右键单击此链接:
<a href="http://www.google.com">Hello</a>
并从扩展程序的上下文菜单中选择了某个选项,我需要以某种形式捕获字符串“Hello” 。正如上下文菜单 API 所示,通过右键单击捕获文本非常容易选择或超链接的实际 URL(在本例中为 Google.com),因为它们出现在 OnClickData 中,但我不确定如何从链接捕获文本。
附带说明一下,如果用户突出显示一个超链接并尝试使用我的上下文菜单,则它不起作用。但是,如果用户突出显示普通文本,它就可以正常工作。当我创建上下文菜单选项时,我确实在“上下文”下启用了“链接”和“选择”。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎没有直接的方法可以做到这一点。但在实现此类功能之前,还有一些解决方法。来自 http://code.google.com/p/chromium/issues/ detail?id=39507
“我想出了一个可以暂时解决这个问题的技巧。我在 Cloudboard 中使用了它:
您可以在页面上执行脚本并使用:document.activeElement来获取当前选定的元素。您可以使用:
document.activeElement.selectionStart, document.activeElement.selectionEnd 获取所选文本和 document.activeElement.value.substr()"
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()"