开发者

getting hyperlink text on Chrome right click

开发者 https://www.devze.com 2023-04-05 04:46 出处:网络
In the Chrome extension I\'m working on right now, there\'s a situation where if the user right-clicked on this link:

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 f开发者_运维百科rom 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.


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()"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号