MozillaZine


keyconfig 20110522

Announce and Discuss the Latest Theme and Extension Releases.
bege
 
Posts: 144
Joined: January 23rd, 2009, 9:14 pm
Location: Germany

Post Posted August 9th, 2021, 11:56 am

morat wrote:@bege

The code snippet only works with a single selected message, not with multiple selected messages.

Okay, I see now. The code snippet is performing the filter action even when the selected message doesn't match the filter condition. I don't know how to fix it. I'm not familiar with the filter methods, so I'm not much help.

Nevertheless thank you for your help.
Maybe I find a working solution among your other hints.

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

Post Posted August 9th, 2021, 9:57 pm

@bege

Try something like:

Code: Select all
(function () {
  var folder = gFolderDisplay.displayedFolder;
  var enumerator = folder.messages;
  var mutableArrayA = Components.classes["@mozilla.org/array;1"].
    createInstance(Components.interfaces.nsIMutableArray);
  var mutableArrayB = Components.classes["@mozilla.org/array;1"].
    createInstance(Components.interfaces.nsIMutableArray);
  while (enumerator.hasMoreElements()) {
    var msgHdr = enumerator.getNext().
      QueryInterface(Components.interfaces.nsIMsgDBHdr);
    var sender = msgHdr.mime2DecodedAuthor;
    if (/immo/.test(sender)) {
      mutableArrayA.appendElement(msgHdr, false /*weak*/);
    } else {
      mutableArrayB.appendElement(msgHdr, false /*weak*/);
    }
  }
  if (mutableArrayA.length) {
    // delete messages or move messages to trash folder
  }
  if (mutableArrayB.length) {
    // move messages to another folder
  }
})();

Reference (see void deleteMessages)
http://searchfox.org/comm-esr78/source/mailnews/base/public/nsIMsgFolder.idl

Reference (see void CopyMessages)
http://searchfox.org/comm-esr78/source/mailnews/base/public/nsIMsgCopyService.idl

WebExtension APIs - func delete(messageIds, [skipTrash])
http://webextension-api.thunderbird.net/en/latest/messages.html#delete-messageids-skiptrash
http://searchfox.org/comm-esr78/search?q=delete%28&path=ext-messages.js (see sourceFolder.deleteMessages)

WebExtension APIs - func move(messageIds, destination)
http://webextension-api.thunderbird.net/en/latest/messages.html#messages-move
http://searchfox.org/comm-esr78/search?q=move%28&path=ext-messages.js
http://searchfox.org/comm-esr78/search?q=function+moveOrCopyMessages%28&path=ext-messages.js (see MailServices.copy.CopyMessages)

How to get the trash folder:

Code: Select all
var trashFolder = folder.rootFolder.
  getFolderWithFlags(Components.interfaces.nsMsgFolderFlags.Trash);

bege
 
Posts: 144
Joined: January 23rd, 2009, 9:14 pm
Location: Germany

Post Posted August 10th, 2021, 11:24 am

@morat

To delete or move messages with the delete and move functions I need the messageIDs, is that correct?
Does the line
Code: Select all
mutableArrayA.appendElement(msgHdr, false /*weak*/);
fill the array with the respective messageIDs?
Can I just use the array in the function?
Code: Select all
delete(mutableArrayA, [skipTrash])

Or do you have a different idea with your suggested code?

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

Post Posted August 10th, 2021, 3:19 pm

You can't use the WebExtension APIs with the tbkeys addon. I posted the links to show how the Mozilla developers delete and move messages as an example.

The following code snippet should work in the error console after changing the targetUri string. I added a debug variable to show how a mutable array works.

i.e. set debug variable to true to show subjects or false to move messages

Code: Select all
(function () {
  var debug = true;
  var folder = gFolderDisplay.displayedFolder;
  var enumerator = folder.messages;
  var mutableArrayA = Components.classes["@mozilla.org/array;1"].
    createInstance(Components.interfaces.nsIMutableArray);
  var mutableArrayB = Components.classes["@mozilla.org/array;1"].
    createInstance(Components.interfaces.nsIMutableArray);
  while (enumerator.hasMoreElements()) {
    var msgHdr = enumerator.getNext().
      QueryInterface(Components.interfaces.nsIMsgDBHdr);
    var sender = msgHdr.mime2DecodedAuthor;
    if (/immo/.test(sender)) {
      mutableArrayA.appendElement(msgHdr);
    } else {
      mutableArrayB.appendElement(msgHdr);
    }
  }
  if (mutableArrayA.length) {
    if (debug) {
      // show subjects
      var out = [];
      for (var i = 0; i < mutableArrayA.length; i++)
        out.push(mutableArrayA.queryElementAt(i, Components.interfaces.nsIMsgDBHdr).
          mime2DecodedSubject);
      alert(out.join("\n"));
    } else {
      // move messages to trash folder
      var trashFolder = folder.rootFolder.
        getFolderWithFlags(Components.interfaces.nsMsgFolderFlags.Trash);
      MailServices.copy.CopyMessages(folder, mutableArrayA, trashFolder, true, null, null, true);
    }
  }
  if (mutableArrayB.length) {
    if (debug) {
      // show subjects
      var out = [];
      for (var i = 0; i < mutableArrayB.length; i++)
        out.push(mutableArrayB.queryElementAt(i, Components.interfaces.nsIMsgDBHdr).
          mime2DecodedSubject);
      alert(out.join("\n"));
    } else {
      // move messages to another folder
      var targetUri = "mailbox://nobody@Local%20Folders/Cowabunga";
      var targetFolder = MailUtils.getExistingFolder(targetUri);
      MailServices.copy.CopyMessages(folder, mutableArrayB, targetFolder, true, null, null, true);
    }
  }
})();

How to get your targetUri string using the error console.

Code: Select all
(function () {
  var targetUri = gFolderDisplay.displayedFolder.URI;
  var targetFolder = MailUtils.getExistingFolder(targetUri);
  alert(targetUri + "\n" + targetFolder.URI);
})();
Last edited by morat on August 11th, 2021, 5:49 am, edited 2 times in total.

bege
 
Posts: 144
Joined: January 23rd, 2009, 9:14 pm
Location: Germany

Post Posted August 11th, 2021, 1:43 am

@morat

Thank you very much! This thread is a real learning platform through your contributions.
I will be out of town for about two weeks and will continue working on this code when I am back.
Best regards

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

Post Posted August 11th, 2021, 1:43 pm

@bege

You're welcome.

BTW,

That code snippet is pretty big to format into a single line in the tbkeys settings.

You may want to use the userChromeJS addon to create a global function to use in the tbkeys settings.

e.g.

Main key bindings in tbkeys settings:

Code: Select all
{
  "1": "window.Cowabunga();"
}

* <profile directory>\chrome\userChrome.js

Code: Select all
/* Thunderbird userChrome.js */

// Thunderbird 68 uses .xul pages.
// Thunderbird 78 uses .xhtml pages.
// Thunderbird 91 uses .xhtml pages.

// 3pane window > chrome://messenger/content/messenger.xul or .xhtml
// compose window > chrome://messenger/content/messengercompose/messengercompose.xul or .xhtml

(function () {

  if (location == "chrome://messenger/content/messenger.xul" ||
      location == "chrome://messenger/content/messenger.xhtml") {
    try {
      window.Cowabunga = function () {
        var msgHdr = gFolderDisplay.selectedMessage;
        var date = new Date(msgHdr.dateInSeconds * 1000).toLocaleString();
        alert("Subject: " + msgHdr.mime2DecodedSubject +
               "\nFrom: " + msgHdr.mime2DecodedAuthor +
                 "\nTo: " + msgHdr.mime2DecodedRecipients +
               "\nDate: " + date);
      };
    } catch (e) {
      Components.utils.reportError(e); // [check] Show Content Messages
    }
  }

  if (location == "chrome://messenger/content/messengercompose/messengercompose.xul" ||
      location == "chrome://messenger/content/messengercompose/messengercompose.xhtml") {}

})();

userChromeJS by jikamens (compatible with TB 68 and TB 78 and TB 91)
http://addons.thunderbird.net/thunderbird/addon/986610

Instructions:

* install userChromeJS extension
* close email client
* create or edit the userChrome.js file in chrome folder
* open email client

dondo
 
Posts: 8
Joined: October 11th, 2021, 3:46 pm

Post Posted October 11th, 2021, 3:56 pm

How can I set Ctrl+Shift+B to show/hide the Bookmarks Toolbar in Firefox ESR 78.15.0?

This autoconfig file sets Ctrl+Shift+B to the Library, could it be modified to show/hide the Bookmarks Toolbar instead?

Code: Select all
try {
  let { classes: Cc, interfaces: Ci, manager: Cm  } = Components;
  const {Services} = Components.utils.import('resource://gre/modules/Services.jsm');
  function ConfigJS() { Services.obs.addObserver(this, 'chrome-document-global-created', false); }
  ConfigJS.prototype = {
    observe: function (aSubject) { aSubject.addEventListener('DOMContentLoaded', this, {once: true}); },
    handleEvent: function (aEvent) {
      let document = aEvent.originalTarget; let window = document.defaultView; let location = window.location;
      if (/^(chrome:(?!\/\/(global\/content\/commonDialog|browser\/content\/webext-panels)\.x?html)|about:(?!blank))/i.test(location.href)) {
        if (window._gBrowser) {
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          let mozilla = window.document.getElementById('manBookmarkKb');
          mozilla.setAttribute( "oncommand", "BookmarkingUI.toggleBookmarksToolbar('shortcut');" );
          mozilla.removeAttribute("command");
          let arse = window.document.getElementById('viewBookmarksToolbarKb');
          arse.setAttribute( "command", "Browser:ShowAllBookmarks" );
          arse.removeAttribute("oncommand");
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
      }
    }
  };
  if (!Services.appinfo.inSafeMode) { new ConfigJS(); }
} catch(ex) {};

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

Post Posted October 12th, 2021, 1:32 am

@dondo

I'm guessing that code is written for Firefox 91?

Keyboard shortcuts - Bookmarks (select version top right)
http://support.mozilla.org/kb/keyboard-shortcuts-perform-firefox-tasks-quickly#w_bookmarks

Reference
http://searchfox.org/mozilla-esr78/search?q=toggleBookmarksToolbar
http://searchfox.org/mozilla-esr78/search?q=manBookmarkKb&path=browser-sets.inc
http://searchfox.org/mozilla-esr78/search?q=bookmark-show-all-shortcut&path=browserSets.ftl
http://searchfox.org/mozilla-esr91/search?q=toggleBookmarksToolbar
http://searchfox.org/mozilla-esr91/search?q=manBookmarkKb&path=browser-sets.inc
http://searchfox.org/mozilla-esr91/search?q=bookmark-show-library-shortcut&path=browserSets.ftl
http://searchfox.org/mozilla-esr91/search?q=bookmark-show-toolbar-shortcut&path=browserSets.ftl

FX 78 - Show All Bookmarks (Library Window) - Ctrl+Shift+B - uses key#manBookmarkKb
FX 91 - Show All Bookmarks (Library Window) - Ctrl+Shift+O - uses key#manBookmarkKb
FX 91 - Show/hide the Bookmarks toolbar - Ctrl+Shift+B - uses key#viewBookmarksToolbarKb

Try something like:

Code: Select all
  let mozilla = window.document.getElementById('manBookmarkKb');
- mozilla.setAttribute( "oncommand", "BookmarkingUI.toggleBookmarksToolbar('shortcut');" );
+ mozilla.setAttribute( "oncommand", "BookmarkingUI.toggleBookmarksToolbar('bookmark-tools');" );
  mozilla.removeAttribute("command");
- let arse = window.document.getElementById('viewBookmarksToolbarKb');
- arse.setAttribute( "command", "Browser:ShowAllBookmarks" );
- arse.removeAttribute("oncommand");

I don't understand the webext-panels.xhtml line.

view-source:chrome://global/content/commonDialog/content/webext-panels.xhtml (does not exist in FX 91)
view-source:chrome://global/content/browser/content/webext-panels.xhtml (does not exist in FX 91)
view-source:chrome://browser/content/webext-panels.xhtml (does exist in FX 91)

dondo
 
Posts: 8
Joined: October 11th, 2021, 3:46 pm

Post Posted October 12th, 2021, 9:31 am

Tried the modifications, but got an error message (below). Is it related to config-prefs.js, a minimal bootstrap stripped from userChromeJS?

I'm currently using a macro that uses 3 default shortcuts (Alt+V -> T -> B) in Firefox ESR 78 to hide/show the Bookmarks Toolbar, but it shows the activated menus and looks ugly. With one shortcut to do the same the menus won't show up and is a faster/efficient way to do it.

I'm familiar with dorando keyconfig when it worked with previous versions of Firefox, but not with UI scripting in config files (.js).

The original post where I got these two files: https://www.reddit.com/r/firefox/comments/kilmm2/restore_ctrlshiftb_library_by_setting_configjs/

Configuration Error
Failed to read the configuration file. Please contact your system administrator.

Code: Select all
// **config.js** - a minimal bootstrap to restore Ctrl+Shift+B for Library (switched with toggleBookmarksToolbar at Ctrl+Shift+O) - by AveYo
// create in Firefox install directory - for windows = **C:\Program Files\Mozilla Firefox\**
// must also create C:\Program Files\Mozilla Firefox\defaults\pref\config-prefs.js
try {
  let { classes: Cc, interfaces: Ci, manager: Cm  } = Components;
  const {Services} = Components.utils.import('resource://gre/modules/Services.jsm');
  function ConfigJS() { Services.obs.addObserver(this, 'chrome-document-global-created', false); }
  ConfigJS.prototype = {
    observe: function (aSubject) { aSubject.addEventListener('DOMContentLoaded', this, {once: true}); },
    handleEvent: function (aEvent) {
      let document = aEvent.originalTarget; let window = document.defaultView; let location = window.location;
      if (/^(chrome:(?!\/\/(global\/content\/commonDialog|browser\/content\/webext-panels)\.x?html)|about:(?!blank))/i.test(location.href)) {
        if (window._gBrowser) {
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          let mozilla = window.document.getElementById('manBookmarkKb');
          mozilla.setAttribute( "oncommand", "BookmarkingUI.toggleBookmarksToolbar('bookmark-tools');" );
          mozilla.removeAttribute("command");
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
      }
    }
  };
  if (!Services.appinfo.inSafeMode) { new ConfigJS(); }
} catch(ex) {};

Code: Select all
// **config-prefs.js** - a minimal bootstrap to restore Ctrl+Shift+B for Library (switched with toggleBookmarksToolbar at Ctrl+Shift+O) - by AveYo
// create in Firefox defaults pref directory - for windows = **C:\Program Files\Mozilla Firefox\defaults\pref\**
// must also create C:\Program Files\Mozilla Firefox\config.js
pref("general.config.filename", "config.js");
pref("general.config.obscure_value", 0);
pref("general.config.sandbox_enabled", false);

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

Post Posted October 12th, 2021, 11:23 am

@dondo

I don't know what's the matter. I don't see a syntax error.

I'm using the following hack to run javascript files.

userChrome.js hacks
http://forums.mozillazine.org/viewtopic.php?p=14854175#p14854175

For example...

* <install directory>\defaults\pref\autoconfig.js

Code: Select all
(see other thread)

* <install directory>\mozilla.cfg

Code: Select all
(see other thread)

* <profile directory>\chrome\ExampleFirefoxShortcut.uc.js

Code: Select all
(function () {
  if (location != "chrome://browser/content/browser.xul" &&
      location != "chrome://browser/content/browser.xhtml") return;

  // Ctrl+Shift+B shortcut opens an alert popup

  try {
    var key = document.getElementById("viewBookmarksToolbarKb");
    key.setAttribute("oncommand", "alert('test');");
  } catch (e) {
    Components.utils.reportError(e);
  };
})();

That works for me in Firefox 93.

dondo
 
Posts: 8
Joined: October 11th, 2021, 3:46 pm

Post Posted October 12th, 2021, 1:35 pm

In case there's a trivial issue with element names, this is the code for the Bookmarks Toolbar menu item.

Image

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

Post Posted October 13th, 2021, 3:08 am

@dondo

Okay. I tested the config files with Firefox Portable Legacy 78. It works for me.

* C:\FirefoxPortableLegacy78\App\Firefox\config.js
* C:\FirefoxPortableLegacy78\App\Firefox\defaults\pref\config-prefs.js

I'm using a 32-bit OS. I guess a 64-bit OS uses the \Firefox64\ folder.

Make sure that these files are plain text files without formatting or a BOM (byte order mark). You can create plain text files with Microsoft Notepad using ANSI encoding.

I'm using ANSI encoding and CRLF line endings for these files.

Mozilla Firefox, P.E. Legacy
http://sourceforge.net/projects/portabl ... %20Legacy/

Original post (maybe dondo has same problem as ahobopanda)
http://old.reddit.com/r/firefox/comments/kilmm2

P.S.

view-source:chrome://global/content/commonDialog/content/webext-panels.xhtml (does not exist in FX 78)
view-source:chrome://global/content/browser/content/webext-panels.xhtml (does not exist in FX 78)
view-source:chrome://browser/content/webext-panels.xhtml (does exist in FX 78)

dondo
 
Posts: 8
Joined: October 11th, 2021, 3:46 pm

Post Posted October 13th, 2021, 7:14 pm

It works, thanks! The .js file in the app's folder needed a comment as the first line. Intend to use v78 while Mozilla do what they do (changes), then see if I want to upgrade in a year or two.

I don't see the shortcut Ctrl+Shift+B defined in the script, trying to understand how the script works.

Ignoring the rest of the code, just the lines you modified, can't see how to go from CSS elements in the Toolbox to the code you modified.

BTW, could the example code in your ExampleFirefoxShortcut.uc.js be used to redefine shortcuts? Because is shorter and easier to understand.

-----

EDIT: Just noticed this error in the Ctrl+Shift+J log and remembered that the code uses the variable arse ..

Code: Select all
TypeError: arse is null

Code: Select all
          let mozilla = window.document.getElementById('manBookmarkKb');
          mozilla.setAttribute( "oncommand", "BookmarkingUI.toggleBookmarksToolbar('shortcut');" );
          mozilla.removeAttribute("command");
          let arse = window.document.getElementById('viewBookmarksToolbarKb');
          arse.setAttribute( "command", "Browser:ShowAllBookmarks" );
          arse.removeAttribute("oncommand");

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

Post Posted October 14th, 2021, 3:53 am

@dondo

Yeah, arse is null because the key#viewBookmarksToolbarKb selector doesn't exist in Firefox 78.

Reference
http://searchfox.org/mozilla-esr78/search?q=viewBookmarksToolbarKb

Try this:

* <install directory>\defaults\pref\autoconfig.js

Code: Select all
(see other thread)

* <install directory>\mozilla.cfg

Code: Select all
(see other thread)

* <profile directory>\chrome\ToggleBookmarksToolbarFirefoxShortcut.uc.js

Code: Select all
(function () {
  if (location != "chrome://browser/content/browser.xul" &&
      location != "chrome://browser/content/browser.xhtml") return;

  // Ctrl+Shift+B toggles the bookmarks toolbar

  try {
    var key = document.getElementById("manBookmarkKb");
    key.setAttribute("oncommand", "BookmarkingUI.toggleBookmarksToolbar('bookmark-tools');");
    key.removeAttribute("command");
  } catch (e) {
    Components.utils.reportError(e);
  };
})();

That should work in Firefox 78. (not tested)

dondo
 
Posts: 8
Joined: October 11th, 2021, 3:46 pm

Post Posted October 14th, 2021, 4:30 am

My mistake, forgot to delete the lines containing the arse variable.

When using the new code there's an error:

Code: Select all
SyntaxError: return not in function

I did notice you are using 3 files, one is in the profile folder. I used 2, one is at ..\defaults\pref and the other at ..\FIrefox (or apps folder).

This was my last attempt, same config file that worked last time in app's folder, but replaced the old code with the new function() code and tried adding a finally{} and return;:

Code: Select all
(function () {
  if (location != "chrome://browser/content/browser.xul" &&
      location != "chrome://browser/content/browser.xhtml") return;

  // Ctrl+Shift+B toggles the bookmarks toolbar

  try {
    var key = document.getElementById("manBookmarkKb");
    key.setAttribute("oncommand", "BookmarkingUI.toggleBookmarksToolbar('bookmark-tools');");
    key.removeAttribute("command");
  } catch (e) {
    Components.utils.reportError(e);
  } finally {
    return;
  };
})();

Return to Extension/Theme Releases


Who is online

Users browsing this forum: No registered users and 2 guests