Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:22:47+00:00 2026-05-23T01:22:47+00:00

I’m developing a firefox extension. So, when the user selects a text and then

  • 0

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);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:22:48+00:00Added an answer on May 23, 2026 at 1:22 am

    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:

    function getSelectedElements(win, tagName) {
        var sel = win.getSelection(), selectedElements = [];
        var range, elementRange, elements;
        if (sel.getRangeAt && sel.rangeCount) {
            elementRange = win.document.createRange();
            for (var r = 0; r < sel.rangeCount; ++r) {
                range = sel.getRangeAt(r);
                containerEl = range.commonAncestorContainer;
                if (containerEl.nodeType != 1) {
                    containerEl = containerEl.parentNode;
                }
                if (containerEl.nodeName.toLowerCase() == tagName) {
                    selectedElements.push(containerEl);
                } else {
                    elements = containerEl.getElementsByTagName(tagName);
                    for (var i = 0; i < elements.length; ++i) {
                        elementRange.selectNodeContents(elements[i]);
                        if (elementRange.compareBoundaryPoints(range.END_TO_START, range) < 1
                                && elementRange.compareBoundaryPoints(range.START_TO_END, range) > -1) {
                            selectedElements.push(elements[i]);
                        }
                    }
                }
            }
            elementRange.detach();
        }
        return selectedElements;
    }
    
    console.log( getSelectedElements(currentWindow, "a") );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.