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
How to get selected text in selected tab from Browser Consol
-
- Posts: 9
- Joined: October 3rd, 2014, 8:35 pm
-
- Posts: 29
- Joined: October 23rd, 2017, 1:23 pm
Re: How to get selected text in selected tab from Browser Co
Try this in the browser console:
Code: Select all
gBrowser.selectedBrowser.messageManager.loadFrameScript('data:, console.log(content.getSelection().toString())', true)
-
- Posts: 6421
- Joined: February 3rd, 2009, 6:29 pm
Re: How to get selected text in selected tab from Browser Co
Here is an example using the sendAsyncMessage() function.
Communicating with frame scripts
http://developer.mozilla.org/docs/Mozil ... me_scripts
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);
console.log(message);
});
gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
})();
http://developer.mozilla.org/docs/Mozil ... me_scripts
-
- Posts: 9
- Joined: October 3rd, 2014, 8:35 pm
Re: How to get selected text in selected tab from Browser Co
Thanks, both! This is exactly what I needed! Thank you much!