<commit_before>/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';

import {
  configs,
  log
} from '/common/common.js';
import * as Constants from '/common/constants.js';
import * as MessageBody from '/extlib/messageBody.js';

let lastComposingResolver;

browser.runtime.onMessage.addListener((message, sender) => {
  switch (message && message.type) {
    case Constants.TYPE_COMPOSE_STARTED:
      log('TYPE_COMPOSE_STARTED received ', message, sender);
      if (lastComposingResolver)
        browser.compose.getComposeDetails(sender.tab.id).then(async details => {
          lastComposingResolver({ tabId: sender.tab.id, details });
          lastComposingResolver = null;
        });
      break;
  }
});
browser.composeScripts.register({
  js: [
    // This sends a Constants.TYPE_COMPOSE_STARTED message on load.
    { file: '/resources/compose.js' }
  ]
});

browser.runtime.onMessage.addListener((message, _sender) => {
  switch (message && message.type) {
    case Constants.TYPE_DO_BUTTON_COMMAND:
      startTypicalReply(message.params).catch(console.error);
      break;
  }
});

async function startTypicalReply(params) {
  log(`startTypicalReply: `, params);
  if (!params)
    return;

  const tabs = await browser.mailTabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT });
  if (tabs.length == 0)
    return;

  const tab     = tabs[0];
  const message = await browser.messageDisplay.getDisplayedMessage(tab.id);

  const composeInfo = await new Promise(async (resolve, _reject) => {
    lastComposingResolver = resolve;
    const details = {};
    switch (params.forwardType) {
      case 'attachment':
        console.log('begin forwared as attachment ', details);
        browser.compose.beginForward(message.id, 'forwardAsAttachment', details);
        break;

      case 'inline':
        console.log('begin forwared inline ', details);
        browser.compose.beginForward(message.id, 'forwardInline', details);
        break;

      default:
        console.log('begin reply ', details);
        switch (params.recipients) {
          case Constants.RECIPIENTS_ALL:
            browser.compose.beginReply(message.id, 'replyToAll', details);
            break;

          case Constants.RECIPIENTS_SENDER:
            browser.compose.beginReply(message.id, 'replyToSender', details);
            break;
<commit_msg>Try to start composition with HTML body (but fails...)<commit_after>/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';

import {
  configs,
  log
} from '/common/common.js';
import * as Constants from '/common/constants.js';
import * as MessageBody from '/extlib/messageBody.js';

let lastComposingResolver;

browser.runtime.onMessage.addListener((message, sender) => {
  switch (message && message.type) {
    case Constants.TYPE_COMPOSE_STARTED:
      log('TYPE_COMPOSE_STARTED received ', message, sender);
      if (lastComposingResolver)
        browser.compose.getComposeDetails(sender.tab.id).then(async details => {
          lastComposingResolver({ tabId: sender.tab.id, details });
          lastComposingResolver = null;
        });
      break;
  }
});
browser.composeScripts.register({
  js: [
    // This sends a Constants.TYPE_COMPOSE_STARTED message on load.
    { file: '/resources/compose.js' }
  ]
});

browser.runtime.onMessage.addListener((message, _sender) => {
  switch (message && message.type) {
    case Constants.TYPE_DO_BUTTON_COMMAND:
      startTypicalReply(message.params).catch(console.error);
      break;
  }
});

async function startTypicalReply(params) {
  log(`startTypicalReply: `, params);
  if (!params)
    return;

  const tabs = await browser.mailTabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT });
  if (tabs.length == 0)
    return;

  const tab     = tabs[0];
  const message = await browser.messageDisplay.getDisplayedMessage(tab.id);

  const composeInfo = await new Promise(async (resolve, _reject) => {
    lastComposingResolver = resolve;
    const details = {
      isPlainText: !params.bodyImage
    };
    switch (params.forwardType) {
      case 'attachment':
        console.log('begin forwared as attachment ', details);
        browser.compose.beginForward(message.id, 'forwardAsAttachment', details);
        break;

      case 'inline':
        console.log('begin forwared inline ', details);
        browser.compose.beginForward(message.id, 'forwardInline', details);
        break;

      default:
        console.log('begin reply ', details);
        switch (params.recipients) {
          case Constants.RECIPIENTS_ALL:
            browser.compose.beginReply(message.id, 'replyToAll', details);
            break;

          case Constants.RECIPIENTS_SENDER:
            browser.compose.beginReply(message.id, 'replyToSender', details);
            break;

{'fraction_non_alphanumeric': 0.11947849776221055, 'fraction_numerical': 0.002335084646818447, 'mean_word_length': 3.1924959216965743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2542682', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1513, 'n_words': 360}