Solution to feed text to urlbar or not load when dropping

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

Solution to feed text to urlbar or not load when dropping

Post by avada »

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: 6393
Joined: February 3rd, 2009, 6:29 pm

Re: Solution to feed text to urlbar or not load when droppin

Post by morat »

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

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

Reference
http://searchfox.org/mozilla-release/se ... rInput.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 ... #p14839926

How to create keyboard shortcut
http://forums.mozillazine.org/viewtopic ... &t=3069554
avada
Posts: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

@morat
Thanks, I'll check it out.
avada
Posts: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

@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: 6393
Joined: February 3rd, 2009, 6:29 pm

Re: Solution to feed text to urlbar or not load when droppin

Post by morat »

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- ... ks-quickly
avada
Posts: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

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- ... ks-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: 6393
Joined: February 3rd, 2009, 6:29 pm

Re: Solution to feed text to urlbar or not load when droppin

Post by morat »

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: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

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: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

@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: 6393
Joined: February 3rd, 2009, 6:29 pm

Re: Solution to feed text to urlbar or not load when droppin

Post by morat »

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: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

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: 6393
Joined: February 3rd, 2009, 6:29 pm

Re: Solution to feed text to urlbar or not load when droppin

Post by morat »

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: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

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: 6393
Joined: February 3rd, 2009, 6:29 pm

Re: Solution to feed text to urlbar or not load when droppin

Post by morat »

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/fo ... ?f=4&t=293
avada
Posts: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: Solution to feed text to urlbar or not load when droppin

Post by avada »

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