Question

In Firefox Extension development, I wants to get selected content from the chrome page, when I use content.getSelection(), its working fine .

But if the selected text is in the iframe, its not working. anyone can help me ?

Was it helpful?

Solution

You need to call getSelection() on the window object where text has been selected. For example, if there is an <iframe id="iframe"> in the content document, you would use the following to get the selection in that frame:

alert(content.document.getElementById("iframe").contentWindow.getSelection());

If you don't know where the user selected text and you want to look at all frames then you should go through them recursively starting at the top level, something like this:

function getSelection(wnd)
{
  var result = wnd.getSelection();

  for (var i = 0; !result && i < wnd.frames.length; i++)
    result = getSelection(wnd.frames[i]);

  return result;
}

alert(getSelection(content));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top