MozillaZine

keyconfig 20110522

Announce and Discuss the Latest Theme and Extension Releases.
tettnanger
 
Posts: 3
Joined: December 22nd, 2016, 6:41 am

Post Posted December 22nd, 2016, 6:56 am

I'm just getting my feet wet with keyconfig and was looking to do something which I think would be fairly simple. What I want to do is have a hotkey that would take the current message in Thunderbird and forward it to a specific email address. In this case, I was wanting to use my Todoist address email because there no longer seems to be a Todoist for Thunderbird add on.

Could anyone provide a very basic code structure for forwarding a message to a specific email address and tying it to a hot key?

thanks

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

Post Posted December 22nd, 2016, 11:26 am

@tettnanger

Try these:

Code: Select all
// forward
MsgForwardMessage(null);

Code: Select all
// forward inline
MsgForwardAsInline(null);

Code: Select all
// forward as attachment
MsgForwardAsAttachment(null);

Code: Select all
// forward in non-default format
var cmdEvent = document.createEvent("xulcommandevent");
cmdEvent.initCommandEvent(
 "command" /* type         */,
  true     /* bubble       */,
  true     /* cancelable   */,
  window   /* view         */,
  0        /* detail       */,
  false    /* ctrl  key    */,
  false    /* alt   key    */,
  true     /* shift key    */,
  false    /* meta  key    */,
  null     /* source event */
);
MsgForwardMessage(cmdEvent);

Code: Select all
// forward, add recipient, send now
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
  getService(Components.interfaces.nsIWindowMediator);
var msgHdr = gFolderDisplay.selectedMessage;
var listener = {
  onWindowTitleChange: function () {},
  onCloseWindow: function () {},
  onOpenWindow: function (aWindow) {
    wm.removeListener(this);
    this.compose = aWindow.docShell.
      QueryInterface(Components.interfaces.nsIInterfaceRequestor).
      getInterface(Components.interfaces.nsIDOMWindow);
    this.compose.addEventListener("compose-window-init", this, true);
  },
  handleEvent: function (event) {
    event.currentTarget.removeEventListener(event.type, this, true);
    event.currentTarget.gMsgCompose.RegisterStateListener(listener);
  },
  NotifyComposeFieldsReady: function () {},
  NotifyComposeBodyReady: function () {
    this.compose.gMsgCompose.UnregisterStateListener(this);
    this.compose.window.setTimeout(function () {
      this.AddRecipient("addr_to", "foo bar <test@invalid.com>");
      this.awDeleteRow(this.awGetMaxRecipients());
      this.goDoCommand("cmd_sendNow");
    }, 0);
  }
}
wm.addListener(listener);
MsgForwardMessage(null);

Code: Select all
// automatically forward
var forwardTo = "foo bar <test@invalid.com>";
var messageArray = gFolderDisplay.selectedMessageUris;
for (var i = 0; i < messageArray.length; i++) {
  var messageUri = messageArray[i];
  var messenger = Components.classes["@mozilla.org/messenger;1"].
    createInstance(Components.interfaces.nsIMessenger);
  var msgHdr = messenger.messageServiceFromURI(messageUri).messageURIToMsgHdr(messageUri);
  var msgWindow = Components.classes["@mozilla.org/messenger/msgwindow;1"].
    createInstance(Components.interfaces.nsIMsgWindow);
  msgWindow.domWindow = window;
  msgWindow.rootDocShell.allowAuth = true;
  var msgAccountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].
    getService(Components.interfaces.nsIMsgAccountManager);
  var server = msgAccountManager.defaultAccount.incomingServer;
  var msgComposeService = Components.classes["@mozilla.org/messengercompose;1"].
    getService(Components.interfaces.nsIMsgComposeService);
  var forwardAsDefault = msgComposeService.kForwardAsDefault;
  var forwardAsAttachment = msgComposeService.kForwardAsAttachment;
  var forwardInline = msgComposeService.kForwardInline;
  // kForwardAsDefault uses mail.forward_message_mode preference
  // 0: forward as attachment
  // 1: forward as quoted (obsolete, mapped to 2)
  // 2: forward as inline with attachments (default)
  var forwardType = forwardAsDefault;
  msgComposeService.forwardMessage(forwardTo, msgHdr, msgWindow, server, forwardType);
}

tettnanger
 
Posts: 3
Joined: December 22nd, 2016, 6:41 am

Post Posted December 22nd, 2016, 2:45 pm

@morat

Perfect. That last code example worked like a charm. Thank you!

tettnanger
 
Posts: 3
Joined: December 22nd, 2016, 6:41 am

Post Posted January 3rd, 2017, 7:15 am

One follow up question. Is there a way (using the last code example in Morat's list) to automatically REMOVE the attachment prior to forwarding? Since I'm using this to put a reminder in my Todoist inbox I generally do not need attachments, nor do I want potentially large attachments sent to Todoist.

thanks

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

Post Posted January 3rd, 2017, 8:16 am

@tettnanger

1.

Do you want to delete all attachments on the original message with prompts, then automatically forward?

Code: Select all
// delete all attachments
TryHandleAllAttachments("delete");

http://dxr.mozilla.org/comm-release/sea ... ttachments

2.

Do you want to silently delete all attachments on the original message, then automatically forward?

It may be possible with the detachAttachmentsWOPrompts method.

http://dxr.mozilla.org/comm-release/sou ... senger.idl

3.

Do you want to automatically forward without attachments, but keep all attachments on the original message?

AFAIK, there is no API to automatically forward without attachments.

http://dxr.mozilla.org/comm-release/sou ... ervice.idl

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

Post Posted January 4th, 2017, 10:02 am

@tettnanger

You could combine the second to last and last code examples.

If the selected message has an attachment, then use the second to last code example, else use the last code example.

Code: Select all
var forwardTo = "foo bar <test@invalid.com>";
var msgHdr = gFolderDisplay.selectedMessage;
if (msgHdr.flags & Components.interfaces.nsMsgMessageFlags.Attachment) {
  // forward, add recipient, delete all attachments, send now
  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
    getService(Components.interfaces.nsIWindowMediator);
  var listener = {
    onWindowTitleChange: function () {},
    onCloseWindow: function () {},
    onOpenWindow: function (aWindow) {
      wm.removeListener(this);
      this.compose = aWindow.docShell.
        QueryInterface(Components.interfaces.nsIInterfaceRequestor).
        getInterface(Components.interfaces.nsIDOMWindow);
      this.compose.addEventListener("compose-window-init", this, true);
    },
    handleEvent: function (event) {
      event.currentTarget.removeEventListener(event.type, this, true);
      event.currentTarget.gMsgCompose.RegisterStateListener(listener);
    },
    NotifyComposeFieldsReady: function () {},
    NotifyComposeBodyReady: function () {
      this.compose.gMsgCompose.UnregisterStateListener(this);
      this.compose.window.setTimeout(function () {
        this.AddRecipient("addr_to", forwardTo);
        this.awDeleteRow(this.awGetMaxRecipients());
        this.GetMsgAttachmentElement().focus();
        this.goDoCommand("cmd_selectAll");
        this.goDoCommand("cmd_delete");
        this.goDoCommand("cmd_sendNow");
      }, 0);
    }
  }
  wm.addListener(listener);
  MsgForwardMessage(null);
} else {
  // automatically forward
  var msgWindow = Components.classes["@mozilla.org/messenger/msgwindow;1"].
    createInstance(Components.interfaces.nsIMsgWindow);
  msgWindow.domWindow = window;
  msgWindow.rootDocShell.allowAuth = true;
  var msgAccountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].
    getService(Components.interfaces.nsIMsgAccountManager);
  var server = msgAccountManager.defaultAccount.incomingServer;
  var msgComposeService = Components.classes["@mozilla.org/messengercompose;1"].
    getService(Components.interfaces.nsIMsgComposeService);
  var forwardAsDefault = msgComposeService.kForwardAsDefault;
  var forwardAsAttachment = msgComposeService.kForwardAsAttachment;
  var forwardInline = msgComposeService.kForwardInline;
  // kForwardAsDefault uses mail.forward_message_mode preference
  // 0: forward as attachment
  // 1: forward as quoted (obsolete, mapped to 2)
  // 2: forward as inline with attachments (default)
  var forwardType = forwardAsDefault;
  msgComposeService.forwardMessage(forwardTo, msgHdr, msgWindow, server, forwardType);
}

chirpy_7
 
Posts: 162
Joined: March 19th, 2007, 6:24 am

Post Posted January 7th, 2017, 6:03 am

How to open a website in the current tab (and, alternatively, in a new tab) with keyconfig (and Scratchpad)

This works (opens the first google hit in a new tab):

Code: Select all
if(window.content.location.href.match(/^http(s)?:\/\/www\.google.(com|fr)\/search/))
{
var nodes = content.document.evaluate('//h3[@class]/a', content.document, null, 7, null);
var url_temp = nodes.snapshotItem(0);
gBrowser.addTab(url_temp.href);
// gBrowser.addTab(nodes.snapshotItem(0));
}


Now I wanted to add another domain from which the same shortcut would load yet another page (within the current tab). I got this far:

Code: Select all
if(window.content.location.href.match(/^http(s)?:\/\/www\.google.(com|fr)\/search/))
{
var nodes = content.document.evaluate('//h3[@class]/a', content.document, null, 7, null);
var url_temp = nodes.snapshotItem(0);
gBrowser.addTab(url_temp.href);
} else if(window.content.location.href.match(/^http(s)?:\/\/(www\.)?hello-world.net\/ict\/folder/))
{
alert("Hello! I am an alert box!");
// gBrowser.addTab(http://www.good-afternoon.com/)
}


If I uncomment the "gBrowser.addTab(http://www.good-afternoon.com/)"-bit, it stops working and I do not know why.

I tried Firefox's Scratchpad, but this throughs an unexpected error: "Exception: ReferenceError: gBrowser is not defined" :(

1)
How can I make this work in keyconfig?

2)
How / where can I better debug / test my keyconfig attempts?

3)
Why does gbrowser throw an error in Scratchpad? (I googled for far too long already...:(

THANK YOU [-o<

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

Post Posted January 7th, 2017, 7:39 am

@chirpy_7

Instructions:

1. open about:config
2. set devtools.chrome.enabled preference to true
3. tools > web developer > scratchpad
4. environment > browser
5. edit > paste (i.e. copy and paste code below)
6. execute > run

Code: Select all
/* Scratchpad Browser Environment */

(function() {

// notice the quotes around the url

gBrowser.selectedTab = gBrowser.addTab("http://www.google.com/");

})();

chirpy_7
 
Posts: 162
Joined: March 19th, 2007, 6:24 am

Post Posted January 7th, 2017, 8:44 am

Thanks morat for that breezingly fast reply!

I had tried to introduce the scratchpad browser environment by adding the line
Code: Select all
// -sp-context: browser
as detailed here https://developer.mozilla.org/en-US/doc ... er_context - however, that did not work; maybe it's still not in the right environment and/or doesn't have the right "scope"...

I feel slightly overwhelmed by the seemingly many ways of loading URL's, at least some of which seem to be already deprecated (possibly including gbrowser ?) and therefore earmarked as obsolete: https://developer.mozilla.org/en-US/doc ... ed_browser

This one seems valid, but I could not get it to work (neither in Scratchpad nor in keyconfig): https://developer.mozilla.org/en-US/Add ... abs/update

How can I modify your solution so that the URL will open in the current tab ?

chirpy_7
 
Posts: 162
Joined: March 19th, 2007, 6:24 am

Post Posted January 7th, 2017, 9:15 am

I figured this one out:

Code: Select all
gBrowser.loadURI("http://www.google.com/");


But what about deprecation? Any other ways to achieve the same which will be around and stay supported longer?

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

Post Posted January 7th, 2017, 11:31 am

@chirpy_7

I doubt Mozilla will remove the gBrowser methods before the XUL apocalypse. (end of 2017)

Old Firefox Extensions Will Stop Working in Firefox 57
viewtopic.php?f=7&t=3025513

Keyconfig extension: Open Location in Firefox
http://kb.mozillazine.org/Keyconfig_ext ... n_Location

Tabbed Browser and loadURI method
http://developer.mozilla.org/en-US/docs ... tabbrowser
http://developer.mozilla.org/en-US/docs ... od/loadURI

frangio
 
Posts: 1
Joined: January 12th, 2017, 12:32 pm

Post Posted January 12th, 2017, 12:39 pm

Hello. I have been using the following hotkey to navigate tabs for a while, assigned to Ctrl+Shift+}, and similarly for the other direction.

Code: Select all
gBrowser.mTabContainer.advanceSelectedTab(1,true);

But now there's a weird issue going on. When I use this hotkey to move to a tab it's like the focus is not properly restored to the document so for example pressing the spacebar will not scroll the page like it should. This doesn't happen with the native hotkey which is Ctrl+PgUp / PgDn.

What could be the cause of this problem and how can I fix it? Thank you.

EDIT: I'm sorry, I was pretty sure I had isolated the problem to this hotkey but it seems that it also happens with the native hotkey. Keyconfig may have nothing to do with it. :(

emf10679
 
Posts: 20
Joined: April 15th, 2009, 8:07 pm

Post Posted January 19th, 2017, 8:19 pm

@morat

Re: full screen. Sorry for the delay.

My mistake was that "Back in the main dialog, I selected the new FullScreen key,": it was,of course, already selected.

Now I can toggle the full screen with Alt+Enter, which is OK but I'd prefer to exit full screen with Esc. How can I do that?

emf10679

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

Post Posted January 20th, 2017, 12:24 am

@emf10679

Try this:

Code: Select all
if (window.fullScreen) {
  BrowserFullScreen();
} else {
  BrowserStop();
}

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

Post Posted January 29th, 2017, 1:10 pm

Can I do something like running an external program?

I would like to run youtube-dl:
Code: Select all
youtube-dl -f "(bestvideo[vcodec=vp9]/bestvideo[vcodec^=avc]/bestvideo[vcodec!=vp8])[height<=1440]+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec^=m4a]/bestaudio)/(best[vcodec=vp9]/best[vcodec^=avc]/best[vcodec!=vp8])[height<=1440]" --write-description --write-sub --write-thumbnail --embed-thumbnail --embed-subs --sub-lang en,hu --no-mtime -o "%(title)s.%(ext)s" <url>

Acually It look like most of it I can put in a configuration file, so this would serve:
Code: Select all
youtube-dl <url>


where the <url> would be the curren't tab's url.

Return to Extension/Theme Releases


Who is online

Users browsing this forum: No registered users and 2 guests