I’m developing a firefox extension. So, when the user selects a text and then right-clicks in order for some context menus to appear, i should be able to retrieve from that selection a list with all the links contained in it. How am i able to do that? i see that currentDocument.getSelection() only retrieves simple text not the whole HTML data.
Thank you TIM . I have one more problem though!
This is how i use your function . Where i call it the text is bolded.
The problem is that when i select a text and right the text that activates your function, if the cursor is over normal text i get the links allright; if the cursor is over LINK TEXT (the actual link) i get and undefined response. Why might this happen?
window.addEventListener("contextmenu", function(e) {
getSelectionLink();
}, false);
function getSelectionLink() {
var SelectionText = "";
var trywindow = false;
var start = 0;
var stop = 0;
var focusedElement = document.commandDispatcher.focusedElement;
if(focusedElement && null != focusedElement)
{
try
{
alert(focusedElement.value);
}
catch(e)
{
trywindow = true;
}
}
else
{
trywindow = true;
}
if(trywindow)
{
var focusedWindow = document.commandDispatcher.focusedWindow;
var winWrapper = new XPCNativeWrapper(focusedWindow, 'document');
var Selection = winWrapper.getSelection();
alert(getSelectedElements(winWrapper, "a"));
//parseSelection(Selection);
}
}
Here’s a function that will get you a list of all elements with a particular tag name that are wholly or partially selected. It works in all major browsers except IE < 9: