MozillaZine


Solution to feed text to urlbar or not load when dropping

Talk about add-ons and extension development.
avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted February 21st, 2021, 1:54 pm

Hi!

Once upon a time you could drop text to the urlbar change it (eg: add keyword) then press enter to load. Bug 1321619 stupidly ruined this.

Which was a lot less of a problem with XUL addons, because I could find one ( caa:addon/simplify-awesome-bar ) that could feed selected text via hotkey to the url bar, which was actually superior to doing it by hand.
But since I let go of old FF and moved to recent versions I constantly miss the functionality and firefox's idiocy of loading everything instantly annoys me to no end.

But of course that's gone. And even though i have set up FF to run XUL addons and userchrome.js scripts, no one fixed it and I have not found a replacement.

So is there any script/addon out there with identical or similar functionality? At least disabling the url bar auto-load nonsense would be nice.

morat
 
Posts: 4864
Joined: February 3rd, 2009, 6:29 pm

Post Posted February 22nd, 2021, 9:27 am

The urlbarBindings.xml file is now the UrlbarInput.jsm file.

Patch for bug 1321619 (see urlbarBindings.xml changes)
http://bug1321619.bmoattachments.org/attachment.cgi?id=8925542

Reference
http://searchfox.org/mozilla-release/search?q=_on_drop&path=UrlbarInput.jsm

I don't know how to change the _on_drop function with a userChrome.js hack.

How to change class prototype
http://stackoverflow.com/questions/37680766

How to set the urlbar...

Code: Select all
gURLBar.value = "test";
gURLBar.focus();

The following code snippets succeed on the about:about page, but fail on the mozillazine.org page.

Code: Select all
(function () {
  var focusedWindow = document.commandDispatcher.focusedWindow;
  var selection = focusedWindow.getSelection();
  if (!selection.isCollapsed) {
    var text = selection.toString();
    console.log(text);
  }
})();

Code: Select all
(function () {
  var selection = window.content.getSelection();
  if (!selection.isCollapsed) {
    var text = selection.toString();
    console.log(text);
  }
})();

Code: Select all
(function () {
  var selectionInfo = BrowserUtils.getSelectionDetails(window);
  if (!selectionInfo.docSelectionIsCollapsed) {
    console.log(selectionInfo.text);
  }
})();

How to get selected text using frame script
http://forums.mozillazine.org/viewtopic.php?p=14839926#p14839926

How to create keyboard shortcut
http://forums.mozillazine.org/viewtopic.php?f=38&t=3069554

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted February 22nd, 2021, 10:55 am

@morat
Thanks, I'll check it out.

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted February 25th, 2021, 1:47 am

@morat
So I guess there's no readily made solution for this. Can't make much more out of it to be honest. These are past my knowledge.
What I don't see is interaction with the urlbar (or megabar or whatever these days), I wonder if it's possible at all.

morat
 
Posts: 4864
Joined: February 3rd, 2009, 6:29 pm

Post Posted February 25th, 2021, 7:24 am

avada wrote:feed selected text via hotkey to the url bar

It works here when I put all the code snippets together.

Code: Select all
(function () {
  window.__unique_identifier_urlbar = function (event) {
    var script = "data:text/plain," + encodeURIComponent(`
      var data = {};
      data.selection = content.getSelection().toString();
      sendAsyncMessage("foobar", data);
    `);
    gBrowser.selectedBrowser.messageManager.addMessageListener("foobar", function removeMe(message) {
      gBrowser.selectedBrowser.messageManager.removeMessageListener("foobar", removeMe);
      gURLBar.value = message.data.selection;
      gURLBar.focus();
    });
    gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
  };
  var keyset = document.getElementById("mainKeyset");
  var key = document.createXULElement("key");
  key.setAttribute("id", "__unique_identifier_key_urlbar");
  key.setAttribute("keycode", "VK_F1");
  key.setAttribute("oncommand", "__unique_identifier_urlbar(event);");
  keyset.appendChild(key);
})();

Instructions:

* open about:config page
* set devtools.chrome.enabled preference to true
* open browser console i.e. tools > web developer > browser console
* copy and paste code into browser console command line
* press enter to run
* focus browser window
* open test page e.g. www.mozillazine.org
* select text
* press f1 to test

Remember to restart the application before running the code because there is an odd bug that if you press f1 before running the code, then pressing f1 after running the code won't work.

If you're already using the f1 shortcut, then you would have to unbind the f1 shortcut or use another shortcut.

Firefox shortcuts
http://support.mozilla.org/kb/keyboard-shortcuts-perform-firefox-tasks-quickly

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted March 7th, 2021, 9:41 am

morat wrote:
avada wrote:feed selected text via hotkey to the url bar

It works here when I put all the code snippets together.

Code: Select all
(function () {
  window.__unique_identifier_urlbar = function (event) {
    var script = "data:text/plain," + encodeURIComponent(`
      var data = {};
      data.selection = content.getSelection().toString();
      sendAsyncMessage("foobar", data);
    `);
    gBrowser.selectedBrowser.messageManager.addMessageListener("foobar", function removeMe(message) {
      gBrowser.selectedBrowser.messageManager.removeMessageListener("foobar", removeMe);
      gURLBar.value = message.data.selection;
      gURLBar.focus();
    });
    gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
  };
  var keyset = document.getElementById("mainKeyset");
  var key = document.createXULElement("key");
  key.setAttribute("id", "__unique_identifier_key_urlbar");
  key.setAttribute("keycode", "VK_F1");
  key.setAttribute("oncommand", "__unique_identifier_urlbar(event);");
  keyset.appendChild(key);
})();

Instructions:

* open about:config page
* set devtools.chrome.enabled preference to true
* open browser console i.e. tools > web developer > browser console
* copy and paste code into browser console command line
* press enter to run
* focus browser window
* open test page e.g. www.mozillazine.org
* select text
* press f1 to test

Remember to restart the application before running the code because there is an odd bug that if you press f1 before running the code, then pressing f1 after running the code won't work.

If you're already using the f1 shortcut, then you would have to unbind the f1 shortcut or use another shortcut.

Firefox shortcuts
http://support.mozilla.org/kb/keyboard-shortcuts-perform-firefox-tasks-quickly


Thanks for looking into it.

I get:
Code: Select all
Uncaught TypeError: document.createXULElement is not a function
    <anonymous> debugger eval code:16
    <anonymous> debugger eval code:21
debugger eval code:16:22
    <névtelen> debugger eval code:16
    <névtelen> debugger eval code:21


Can't the code be run from Keyconfig or Custom Buttons?

morat
 
Posts: 4864
Joined: February 3rd, 2009, 6:29 pm

Post Posted March 7th, 2021, 10:31 am

avada wrote:Can't the code be run from Keyconfig or Custom Buttons?

Those addons are obsolete. What version are you running? Are you using e10s?

i.e. Menu Bar > Help > Troubleshooting Information > Multiprocess Windows

I think document.createXULElement is available in Firefox 63.

Bug 1479538
http://bugzilla.mozilla.org/show_bug.cgi?id=1479538

Firefox 68 uses the browser.xul page.

Firefox 69 uses the browser.xhtml page so document.createXULElement should be available.

Fix for older versions...

Code: Select all
- var key = document.createXULElement("key");
+ var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+ var key = document.createElementNS(XUL_NS, "key");

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted March 10th, 2021, 4:06 pm

morat wrote:
avada wrote:Can't the code be run from Keyconfig or Custom Buttons?

Those addons are obsolete. What version are you running? Are you using e10s?

i.e. Menu Bar > Help > Troubleshooting Information > Multiprocess Windows

I think document.createXULElement is available in Firefox 63.

Bug 1479538
http://bugzilla.mozilla.org/show_bug.cgi?id=1479538

Firefox 68 uses the browser.xul page.

Firefox 69 uses the browser.xhtml page so document.createXULElement should be available.

Fix for older versions...

Code: Select all
- var key = document.createXULElement("key");
+ var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+ var key = document.createElementNS(XUL_NS, "key");

I use FF 86 dev. These updated ones work fine enough, from here: https://github.com/xiaoxiaoflood/firefox-scripts

Yes I have e10s enabled.

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted April 3rd, 2021, 6:22 am

@morat
Okay. So it turns out I can use this part of the code to send text to the urlbar:
(Although it doesn't work in input fields, text fields and such)
Code: Select all
var script = "data:text/plain," + encodeURIComponent(`
      var data = {};
      data.selection = content.getSelection().toString();
      sendAsyncMessage("foobar", data);
    `);
    gBrowser.selectedBrowser.messageManager.addMessageListener("foobar", function removeMe(message) {
      gBrowser.selectedBrowser.messageManager.removeMessageListener("foobar", removeMe);
      gURLBar.value = " " + message.data.selection;
      gURLBar.select()
    });
    gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);


Is there an easy way to move caret/cursor to the beginning of the urlbar?

morat
 
Posts: 4864
Joined: February 3rd, 2009, 6:29 pm

Post Posted April 3rd, 2021, 12:09 pm

Try something like:

Code: Select all
// Move the caret to the beginning
gURLBar.selectionStart = 0;
gURLBar.selectionEnd = 0;

Reference
http://searchfox.org/mozilla-release/so ... otAtEnd.js

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted April 3rd, 2021, 3:02 pm

morat wrote:Try something like:

Code: Select all
// Move the caret to the beginning
gURLBar.selectionStart = 0;
gURLBar.selectionEnd = 0;

Reference
http://searchfox.org/mozilla-release/so ... otAtEnd.js

Thanks! That solved the caret issue.

Do you know why the code doesn't work for inputs, textfields and such?

morat
 
Posts: 4864
Joined: February 3rd, 2009, 6:29 pm

Post Posted April 3rd, 2021, 4:43 pm

It works in the web console.

Knowledge Base textbox example:

Code: Select all
(function () {
  var inputs = document.querySelectorAll("input.inputbox.search.itsy");
  inputs[1].focus();
  inputs[1].value = "example";
  inputs[1].selectionStart = 0;
  inputs[1].selectionEnd = 0;
})();

Test page
http://forums.mozillazine.org/

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted April 4th, 2021, 1:56 am

morat wrote:It works in the web console.



I mean that the code above fails to fetch and feed the selected text to the urlbar when I press the hotkey, when in any input field or text field. The URLbar only gets the focus.

morat
 
Posts: 4864
Joined: February 3rd, 2009, 6:29 pm

Post Posted April 4th, 2021, 9:31 am

Textareas and text inputs have a differenct selection API. They have selectionStart and selectionEnd properties that are character offsets within the value property of the textarea or input.

Try something like:

Code: Select all
(function () {
  var script = "data:text/plain," + encodeURIComponent(`
    var data = {};
    data.selection = null;
    var selection = content.getSelection();
    if (selection.isCollapsed) {
      var element = content.document.activeElement;
      if (element instanceof HTMLTextAreaElement ||
          element instanceof HTMLInputElement &&
         (element.type ==     "text" ||
          element.type == "password" ||
          element.type ==    "email" ||
          element.type ==   "search" ||
          element.type ==      "tel" ||
          element.type ==      "url")) {
        var start = element.selectionStart;
        var end = element.selectionEnd;
        data.selection = element.value.substring(start, end);
      }
    } else {
      data.selection = selection.toString();
    }
    sendAsyncMessage("foobar", data);
  `);
  gBrowser.selectedBrowser.messageManager.addMessageListener("foobar", function removeMe(message) {
    gBrowser.selectedBrowser.messageManager.removeMessageListener("foobar", removeMe);
    if (message.data.selection) {
      gURLBar.value = message.data.selection;
   // gURLBar.focus();
      gURLBar.select();
    }
  });
  gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
})();

Test page
http://www.mozillazine.org/

More info
http://stackoverflow.com/a/20427804

Text To Speech (similar code)
http://custombuttons.sourceforge.net/forum/viewtopic.php?f=4&t=293

avada
 
Posts: 1901
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted April 4th, 2021, 11:53 am

@morat
Thanks a lot!
It looks like it works perfectly now.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests