How to get selected text in selected tab from Browser Consol

User Help for Mozilla Firefox
Posts: 9
Joined: October 3rd, 2014, 8:35 pm

Post Posted July 31st, 2019, 7:02 pm

From the Web Console, I am able to pull the following information:

window.getSelection().toString() // gets currently selected text
document.title // gets page title
document.URL // gets page URL

I would like to similarly get the selected text from within the Browser Console. From what I'm able to find from other webpages, the expression "gBrowser.selectedTab.linkedBrowser.contentDocument" should get me to the same object in the Browser Console as "document" is in the Web Console. However,

window.getSelection().toString() // returns empty string from Browser Console
gBrowser.selectedTab.linkedBrowser.contentDocument // returns null, so unable to find title or URL in here, and unable to get to window for selection

I've experimented with several permutations, trying to navigate my way to the selected text, but all have failed so far:

gBrowser.ownerDocument.getSelection().toString() // bad
gBrowser.ownerDocument.defaultView.getSelection().toString() // bad
gBrowser.getBrowserForTab(gBrowser.selectedTab).ownerDocument.defaultView.getSelection().toString() // bad
gBrowser.selectedTab.linkedBrowser.contentDocument.getSelection() // bad
document.commandDispatcher.focusedWindow.getSelection().toString() // bad

How do I get the selected text of the selected tab from within the Browser Console? Alternatively, how do I get the "window" and "document" equivalents for the selected tab from the XUL objects we're manipulating in the Browser Console? I'm not sure if this changed at all with the recent restrictions for extensions (so firefox may have changed from the webpages I'm trying to get info from)?

Thanks for any assistance

Posts: 29
Joined: October 23rd, 2017, 1:23 pm

Post Posted August 1st, 2019, 12:07 am

Try this in the browser console:

Code: Select all
gBrowser.selectedBrowser.messageManager.loadFrameScript('data:, console.log(content.getSelection().toString())', true)

Posts: 4264
Joined: February 3rd, 2009, 6:29 pm

Post Posted August 1st, 2019, 12:20 am

Here is an example using the sendAsyncMessage() function.

Code: Select all
(function () {
  var script = "data:text/plain," + encodeURIComponent(`
    var data = {};
    data.selection = content.getSelection().toString();
    data.title = content.document.title;
    data.href = content.document.location.href;
    sendAsyncMessage("foobar", data);
  gBrowser.selectedBrowser.messageManager.addMessageListener("foobar", function removeMe(message) {
    gBrowser.selectedBrowser.messageManager.removeMessageListener("foobar", removeMe);
  gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);

Communicating with frame scripts ... me_scripts

Posts: 9
Joined: October 3rd, 2014, 8:35 pm

Post Posted August 1st, 2019, 10:37 am

Thanks, both! This is exactly what I needed! Thank you much!

Return to Firefox Support

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests