Issue with getting focused text selection when iframes are involved

203 views
Skip to first unread message

Link

unread,
Mar 9, 2016, 8:47:16 PM3/9/16
to Chromium-Extensions-Announce
While writing a text to speech extension I thought of an issue I can't figure out how to solve.

When you have one or more iframes on a page. 

One iframe for this example. See Test_page html below.
You select text first in source page "brown fox" and second in the iframe "my box".

Now you execute eg via keyboard shortcut [chrome.commands.onCommand]:
[extension] Background.js:
chrome.tabs.executeScript(
{code: "window.getSelection().toString()", allFrames: true},
function(results) { console.log(results); });

The result is this:
["brown fox", "my box"]

The code do what it should but the selected text that is in focus is "my box" in the iframe. That might be the text the user expects to be spoken.

Is there anyway to figure out which selected text on the page has focus?

I could easily queue up both selections but that might not be expected by the user.
Am I overthinking this?

Note: I've reduced the code to the important part.
Note 2: Focus, meaning the white text with the blue background, the default in Windows.


Test_page.html:
<!DOCTYPE html>
<html>
<head>
<title>Text to speech test page</title>
</head>
<body>
<div>The quick, brown fox jumps over a lazy dog.</div>
<div>
<iframe src="Test_page_iframe.html"></iframe>
</div>
</body>
</html>


Test_page_iframe:
<!DOCTYPE html>
<html>
<head>
<title>[iframe] Text to speech test page</title>
<style type="text/css">
.text_iframe {
color: orange;
}
</style>
</head>
<body>
<div class="text_iframe">Pack my box with five dozen liquor jugs.</div>
</body>
</html>

Dominic Mazzoni

unread,
Mar 10, 2016, 12:23:03 PM3/10/16
to Link, Chromium-Extensions-Announce
In the script you execute on each frame, call document.hasFocus() - it will return true for the focused iframe, and false for all other iframes.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/454f3ab5-6138-4eee-807b-42ea265d9b69%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.
Reply all
Reply to author
Forward
0 new messages