MozillaZine


keyconfig 20110522

Announce and Discuss the Latest Theme and Extension Releases.
avada
 
Posts: 1915
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Post Posted January 8th, 2023, 5:33 am

morat wrote:Maybe the message listener isn't triggering correctly. If so, then I can't fix that.

Try running the following code snippets in the browser console.

1.

Does the url bar value change to "Hocus Pocus" string?

Code: Select all
gURLBar.value = "Hocus Pocus";

2.

Does the "Cowabunga" message appear in the browser console?

Code: Select all
gBrowser.selectedBrowser.messageManager.loadFrameScript(`data:application/javascript,console.log("Cowabunga")`, false);


Yes to both.

I tested ff108 and ff-dev108. And ind both, it works.
Something changed for 109.

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

Post Posted January 8th, 2023, 5:58 am

(Not related to the problem)

During testing I remembered that the script doesn't work if there's a different frame on the page. (such as w3schools tryit editor)
Is this because some sort of Firefox restriction?

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

Post Posted January 9th, 2023, 8:47 am

Okay. I installed the Firefox Developer Edition.

The original code fails in the browser console utility.

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();
      gURLBar.selectionStart = 0;
      gURLBar.selectionEnd = 0;
    }
  });
  gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
})();

A simplified message listener works correctly in the browser console utility.

Code: Select all
(function () {
  var script = "data:text/plain," + encodeURIComponent(`
    var data = {};
    data.test = "abracadabra"
    sendAsyncMessage("foobar", data);
  `);
  gBrowser.selectedBrowser.messageManager.addMessageListener("foobar", function removeMe(message) {
    gBrowser.selectedBrowser.messageManager.removeMessageListener("foobar", removeMe);
    alert(message.data.test);
  });
  gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
})();

A simplified selection code works correctly in the web developer tools utility.

Code: Select all
(function () {
  var data = {};
  data.selection = null;
  var selection = window.getSelection();
  if (selection.isCollapsed) {
    var element = window.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();
  }
  alert(data.selection);
})();

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

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

Post Posted January 9th, 2023, 9:38 am

@avada

Okay. I fixed the issue with the test page.

Find: HTMLTextAreaElement
Replace with: content.HTMLTextAreaElement

Find: HTMLInputElement
Replace with: content.HTMLInputElement

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 content.HTMLTextAreaElement ||
          element instanceof content.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();
      gURLBar.selectionStart = 0;
      gURLBar.selectionEnd = 0;
    }
  });
  gBrowser.selectedBrowser.messageManager.loadFrameScript(script, false);
})();

Annoying bug.

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

Post Posted January 9th, 2023, 2:31 pm

morat wrote:
Okay. I fixed the issue with the test page.

Find: HTMLTextAreaElement
Replace with: content.HTMLTextAreaElement

Find: HTMLInputElement
Replace with: content.HTMLInputElement


Thanks a lot!

Was this always incorrect and Firefox just got stricter? Or did they just change how things work?

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

Post Posted January 9th, 2023, 5:27 pm

@avada

1. content.HTMLTextAreaElement
2. window.HTMLTextAreaElement

In Firefox 108, (1) and (2) are the same, not different like in Firefox 109.

Maybe a security change, don't know really.

Return to Extension/Theme Releases


Who is online

Users browsing this forum: No registered users and 10 guests