keyconfig 20110522

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

Re: keyconfig 20110522

Post by avada »

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

Re: keyconfig 20110522

Post by avada »

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

Re: keyconfig 20110522

Post by morat »

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

Re: keyconfig 20110522

Post by morat »

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

Re: keyconfig 20110522

Post by avada »

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

Re: keyconfig 20110522

Post by morat »

@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.
morat
Posts: 6394
Joined: February 3rd, 2009, 6:29 pm

Re: keyconfig 20110522

Post by morat »

Note: I'm posting a tip to help other tbkeys users.

Use the nsIDOMWindowUtils.sendNativeKeyEvent method to simulate or synthesize a keypress. The nsIDOMWindowUtils.sendKeyEvent method doesn't work anymore.

Examples:

Send the down arrow key.

Code: Select all

(function () {
  var KEYBOARD_LAYOUT_EN_US = {name: 'US', Mac: 0, Win: 0x00000409, hasAltGrOnWin: false};
  var WIN_VK_DOWN = 0xe0500028;
  window.windowUtils.sendNativeKeyEvent(KEYBOARD_LAYOUT_EN_US, WIN_VK_DOWN, 0, '', '');
})();
Send the Shift+F8 keypress to toggle the thread pane using the Maximize Message Pane addon.

Code: Select all

(function () {
  var KEYBOARD_LAYOUT_EN_US = {name: 'US', Mac: 0, Win: 0x00000409, hasAltGrOnWin: false};
  var WIN_VK_F8 = 0x00420077;
  var NATIVE_MODIFIER_SHIFT_LEFT = 0x00000100;
  var modifiers = 0;
  modifiers |= NATIVE_MODIFIER_SHIFT_LEFT;
  window.windowUtils.sendNativeKeyEvent(KEYBOARD_LAYOUT_EN_US, WIN_VK_F8, modifiers, '', '');
})();
Send the Ctrl+Shift+I keypress to open the Developer Tools utility.

Code: Select all

(function () {
  var KEYBOARD_LAYOUT_EN_US = {name: 'US', Mac: 0, Win: 0x00000409, hasAltGrOnWin: false};
  var WIN_VK_I = 0x00000049;
  var NATIVE_MODIFIER_CONTROL_LEFT = 0x00000400;
  var NATIVE_MODIFIER_SHIFT_LEFT = 0x00000100;
  var modifiers = 0;
  modifiers |= NATIVE_MODIFIER_CONTROL_LEFT;
  modifiers |= NATIVE_MODIFIER_SHIFT_LEFT;
  window.windowUtils.sendNativeKeyEvent(KEYBOARD_LAYOUT_EN_US, WIN_VK_I, modifiers, '', '');
})();
French users need to use the KEYBOARD_LAYOUT_FRENCH native keyboard layout object.
German users need to use the KEYBOARD_LAYOUT_GERMAN native keyboard layout object.

Mac users need to use the MAC_VK_ native key code constants. Windows users need to use the WIN_VK_ native key code constants.

The NATIVE_MODIFIER_ modifier constants are the same on Mac and Windows.

Reference
http://searchfox.org/mozilla-esr102/sea ... YOUT_EN_US (see native keyboard layout objects in EventUtils.js)
http://searchfox.org/mozilla-esr102/sea ... IN_VK_DOWN (see native key code constants in NativeKeyCodes.js)
http://searchfox.org/mozilla-esr102/sea ... SHIFT_LEFT (see native modifier constants in nsIDOMWindowUtils.idl)
http://searchfox.org/mozilla-esr102/sea ... &case=true
http://searchfox.org/mozilla-esr102/sea ... &case=true

Similar thread
http://github.com/wshanks/tbkeys/issues/125

Use the nsIDOMWindowUtils.sendMouseEvent method to simulate or synthesize a mouse click.

Example:

Send a mouse click to focus the message and display the caret when caret browsing.

Code: Select all

(function () {
  var browser = window.document.getElementById('messagepane');
  var rect = browser.getBoundingClientRect();
  var x = rect.left + 10;
  var y = rect.top + 10;
  var button = 0;
  var clickCount = 1;
  var modifiers = 0;
  window.windowUtils.sendMouseEvent('mousedown', x, y, button, clickCount, modifiers);
  window.windowUtils.sendMouseEvent('mouseup', x, y, button, clickCount, modifiers);
})();
Reference
http://searchfox.org/mozilla-esr102/sea ... FIER_SHIFT (see non-native modifier constants in nsIDOMWindowUtils.idl)
http://searchfox.org/mozilla-esr102/sea ... &case=true

Tips: http://forums.mozillazine.org/viewtopic ... #p14872763
Last edited by morat on March 7th, 2023, 8:03 pm, edited 3 times in total.
202nine
Posts: 8
Joined: March 9th, 2021, 11:52 am

Re: keyconfig 20110522

Post by 202nine »

Morat or anyone:

For those using Dumby's latest CB for nightlies or xiaoxiaoflood's keyconfig Mozilla has made some kind of change when loading URIs in v112. This is what we've been using but it no longer works:

Code: Select all

 gBrowser.loadURI("about:home", {
    triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
    });
From what I can tell they've added loadURIOptions to the code somewhere but I can't make it work.
morat
Posts: 6394
Joined: February 3rd, 2009, 6:29 pm

Re: keyconfig 20110522

Post by morat »

@202nine

Try these:

Code: Select all

BrowserHome();

Code: Select all

gBrowser.loadURI("about:home", {
  triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
});

Code: Select all

loadOneOrMoreURIs("about:home", Services.scriptSecurityManager.getSystemPrincipal(), null);

Code: Select all

gBrowser.loadTabs(["about:home"], {
  inBackground: false,
  replace: true,
  triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
  csp: null,
});
Not tested. Firefox 112 isn't available yet on the searchfox.org site so I didn't check the source.

Reference
http://searchfox.org/mozilla-central/so ... ersion.txt
202nine
Posts: 8
Joined: March 9th, 2021, 11:52 am

Re: keyconfig 20110522

Post by 202nine »

Morat, thanks you saved the day. I've got several bookmarks that were broken but this code works good:

Code: Select all

loadOneOrMoreURIs("about:home", Services.scriptSecurityManager.getSystemPrincipal(), null);
morat
Posts: 6394
Joined: February 3rd, 2009, 6:29 pm

Re: keyconfig 20110522

Post by morat »

@202nine

I found the bug.

Code: Select all

gBrowser.loadURI(Services.io.newURI("about:home"), {
  triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
Loading URIs should be done with URI objects
http://bugzilla.mozilla.org/show_bug.cgi?id=1810141
202nine
Posts: 8
Joined: March 9th, 2021, 11:52 am

Re: keyconfig 20110522

Post by 202nine »

morat, thanks for that link. I've gone through it and both of these seem to be a working change:

Code: Select all

gBrowser.loadURI(Services.io.newURI("about:home"), {
  triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});

Code: Select all

gBrowser.fixupAndLoadURIString ("about:home", {
    triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
    });
With these two bookmarklets work as well, with the previous posted ones they didn't.
avada
Posts: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: keyconfig 20110522

Post by avada »

Hi!

I have this code in keyconfig to hide the tab-bar:

Code: Select all

    var css = [];
    css.push("#TabsToolbar { visibility: collapse !important; }");
    var ios = Components.classes["@mozilla.org/network/io-service;1"].
      getService(Components.interfaces.nsIIOService);
    var uri = ios.newURI("data:text/css," + encodeURIComponent(css.join("\n")), null, null);
    var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"].
      getService(Components.interfaces.nsIStyleSheetService);
    if (sss.sheetRegistered(uri, sss.USER_SHEET)) {
      sss.unregisterSheet(uri, sss.USER_SHEET);
    } else {
      sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
    }
But it would be better if it was hidden by default, but also can be made visible when needed. So is it possible to change this into an userchrome JS script with a keyboard shortcut?
morat
Posts: 6394
Joined: February 3rd, 2009, 6:29 pm

Re: keyconfig 20110522

Post by morat »

Continue using keyconfig and put the code snippet in the mozilla.cfg file to handle the default state.

* <install directory>\mozilla.cfg

Code: Select all

// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.setTimeout(function () {
    try {
      if (chromeWindow.userChromeJsMod) return;
      chromeWindow.userChromeJsMod = true;
      var css = [];
      css.push("#TabsToolbar { visibility: collapse !important; }");
      var ios = Components.classes["@mozilla.org/network/io-service;1"].
        getService(Components.interfaces.nsIIOService);
      var uri = ios.newURI("data:text/css," + encodeURIComponent(css.join("\n")), null, null);
      var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"].
        getService(Components.interfaces.nsIStyleSheetService);
      if (sss.sheetRegistered(uri, sss.USER_SHEET)) {
        sss.unregisterSheet(uri, sss.USER_SHEET);
      } else {
        sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
      }
    } catch (aError) {
      Components.utils.reportError(aError); // [check] Show Content Messages
    }
  }, 10);
}, "browser-delayed-startup-finished", false);
Examples
http://github.com/morat523035/FirefoxButtons
avada
Posts: 1932
Joined: February 10th, 2008, 6:30 am
Location: Hungary

Re: keyconfig 20110522

Post by avada »

morat wrote:Continue using keyconfig and put the code snippet in the mozilla.cfg file to handle the default state.

* <install directory>\mozilla.cfg

Code: Select all

// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.setTimeout(function () {
    try {
      if (chromeWindow.userChromeJsMod) return;
      chromeWindow.userChromeJsMod = true;
      var css = [];
      css.push("#TabsToolbar { visibility: collapse !important; }");
      var ios = Components.classes["@mozilla.org/network/io-service;1"].
        getService(Components.interfaces.nsIIOService);
      var uri = ios.newURI("data:text/css," + encodeURIComponent(css.join("\n")), null, null);
      var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"].
        getService(Components.interfaces.nsIStyleSheetService);
      if (sss.sheetRegistered(uri, sss.USER_SHEET)) {
        sss.unregisterSheet(uri, sss.USER_SHEET);
      } else {
        sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
      }
    } catch (aError) {
      Components.utils.reportError(aError); // [check] Show Content Messages
    }
  }, 10);
}, "browser-delayed-startup-finished", false);
Examples
http://github.com/morat523035/FirefoxButtons
I put it in an uc.js file instead it works just as well. The tabbar is hidden as the windows are loaded. Thanks!
Post Reply