summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/about-support/content/export.js
blob: 46eb0c649793450b044d1880fd7e85ba206fd045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* 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/. */

/* globals CLASS_DATA_PRIVATE, CLASS_DATA_PUBLIC, CLASS_DATA_UIONLY, createElement,
createParentElement, getAccountsText, getLoadContext, MailServices, Services */

"use strict";

var { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

/**
 * Create warning text to add to any private data.
 *
 * @returns A HTML paragraph node containing the warning.
 */
function createWarning() {
  let bundle = Services.strings.createBundle(
    "chrome://messenger/locale/aboutSupportMail.properties"
  );
  return createParentElement("p", [
    createElement("strong", bundle.GetStringFromName("warningLabel")),
    // Add some whitespace between the label and the text
    document.createTextNode(" "),
    document.createTextNode(bundle.GetStringFromName("warningText")),
  ]);
}

function getClipboardTransferable() {
  // Get the HTML and text representations for the important part of the page.
  let hidePrivateData = !document.getElementById("check-show-private-data")
    .checked;
  let contentsDiv = createCleanedUpContents(hidePrivateData);
  let dataHtml = contentsDiv.innerHTML;
  let dataText = createTextForElement(contentsDiv, hidePrivateData);

  // We can't use plain strings, we have to use nsSupportsString.
  let supportsStringClass = Cc["@mozilla.org/supports-string;1"];
  let ssHtml = supportsStringClass.createInstance(Ci.nsISupportsString);
  let ssText = supportsStringClass.createInstance(Ci.nsISupportsString);

  let transferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
    Ci.nsITransferable
  );
  transferable.init(getLoadContext());

  // Add the HTML flavor.
  transferable.addDataFlavor("text/html");
  ssHtml.data = dataHtml;
  transferable.setTransferData("text/html", ssHtml);

  // Add the plain text flavor.
  transferable.addDataFlavor("text/plain");
  ssText.data = dataText;
  transferable.setTransferData("text/plain", ssText);

  return transferable;
}

// This function intentionally has the same name as the one in aboutSupport.js
// so that the one here is called.
function copyContentsToClipboard() {
  let transferable = getClipboardTransferable();
  // Store the data into the clipboard.
  Services.clipboard.setData(
    transferable,
    null,
    Services.clipboard.kGlobalClipboard
  );
}

function sendViaEmail() {
  // Get the HTML representation for the important part of the page.
  let hidePrivateData = !document.getElementById("check-show-private-data")
    .checked;
  let contentsDiv = createCleanedUpContents(hidePrivateData);
  let dataHtml = contentsDiv.innerHTML;
  // The editor considers whitespace to be significant, so replace all
  // whitespace with a single space.
  dataHtml = dataHtml.replace(/\s+/g, " ");

  // Set up parameters and fields to use for the compose window.
  let params = Cc[
    "@mozilla.org/messengercompose/composeparams;1"
  ].createInstance(Ci.nsIMsgComposeParams);
  params.type = Ci.nsIMsgCompType.New;
  params.format = Ci.nsIMsgCompFormat.HTML;

  let fields = Cc[
    "@mozilla.org/messengercompose/composefields;1"
  ].createInstance(Ci.nsIMsgCompFields);
  fields.forcePlainText = false;
  fields.body = dataHtml;
  // In general we can have non-ASCII characters, and compose's charset
  // detection doesn't seem to work when the HTML part is pure ASCII but the
  // text isn't. So take the easy way out and force UTF-8.
  fields.bodyIsAsciiOnly = false;
  params.composeFields = fields;

  // Our params are set up. Now open a compose window.
  MailServices.compose.OpenComposeWindowWithParams(null, params);
}

function createCleanedUpContents(aHidePrivateData) {
  // Get the important part of the page.
  let contentsDiv = document.getElementById("contents");
  // Deep-clone the entire div.
  let clonedDiv = contentsDiv.cloneNode(true);
  // Go in and replace text with the text we actually want to copy.
  // (this mutates the cloned node)
  cleanUpText(clonedDiv, aHidePrivateData);
  // Insert a warning if we need to
  if (!aHidePrivateData) {
    clonedDiv.insertBefore(createWarning(), clonedDiv.firstChild);
  }
  return clonedDiv;
}

function cleanUpText(aElem, aHidePrivateData) {
  let node = aElem.firstChild;
  let copyData = aElem.dataset.copyData;
  delete aElem.dataset.copyData;
  while (node) {
    let classList = "classList" in node && node.classList;
    // Delete uionly and no-copy nodes.
    if (
      classList &&
      (classList.contains(CLASS_DATA_UIONLY) || classList.contains("no-copy"))
    ) {
      // Advance to the next node before removing the current node, since
      // node.nextElementSibling is null after remove()
      let nextNode = node.nextElementSibling;
      node.remove();
      node = nextNode;
      continue;
    } else if (
      aHidePrivateData &&
      classList &&
      classList.contains(CLASS_DATA_PRIVATE)
    ) {
      // Replace private data with a blank string.
      node.textContent = "";
    } else if (
      !aHidePrivateData &&
      classList &&
      classList.contains(CLASS_DATA_PUBLIC)
    ) {
      // Replace public data with a blank string.
      node.textContent = "";
    } else if (copyData != null) {
      // Replace localized text with non-localized text.
      node.textContent = copyData;
      copyData = null;
    }

    if (node.nodeType == Node.ELEMENT_NODE) {
      cleanUpText(node, aHidePrivateData);
    }

    // Advance!
    node = node.nextSibling;
  }
}

// Return the plain text representation of an element.  Do a little bit
// of pretty-printing to make it human-readable.
function createTextForElement(elem, aHidePrivateData) {
  // Generate the initial text.
  let textFragmentAccumulator = [];
  generateTextForElement(elem, aHidePrivateData, "", textFragmentAccumulator);
  let text = textFragmentAccumulator.join("");

  // Trim extraneous whitespace before newlines, then squash extraneous
  // blank lines.
  text = text.replace(/[ \t]+\n/g, "\n");
  text = text.replace(/\n{3,}/g, "\n\n");

  // Actual CR/LF pairs are needed for some Windows text editors.
  if ("@mozilla.org/windows-registry-key;1" in Cc) {
    text = text.replace(/\n/g, "\r\n");
  }

  return text;
}

/**
 * Elements to replace entirely with custom text. Keys are element ids, values
 * are functions that return the text. The functions themselves are defined in
 * the files for their respective sections.
 */
var gElementsToReplace = {
  "accounts-table": getAccountsText,
};

function generateTextForElement(
  elem,
  aHidePrivateData,
  indent,
  textFragmentAccumulator
) {
  // Add a little extra spacing around most elements.
  if (!["td", "th", "span", "a"].includes(elem.tagName)) {
    textFragmentAccumulator.push("\n");
  }

  // If this element is one of our elements to replace with text, do it.
  if (elem.id in gElementsToReplace) {
    let replaceFn = gElementsToReplace[elem.id];
    textFragmentAccumulator.push(replaceFn(aHidePrivateData, indent + "  "));
    return;
  }

  if (AppConstants.MOZ_CRASHREPORTER) {
    if (elem.id == "crashes-table") {
      textFragmentAccumulator.push(getCrashesText(indent));
      return;
    }
  }

  let childCount = elem.childElementCount;

  // We're not going to spread a two-column <tr> across multiple lines, so
  // handle that separately.
  if (elem.tagName == "tr" && childCount == 2) {
    textFragmentAccumulator.push(indent);
    textFragmentAccumulator.push(
      elem.children[0].textContent.trim() +
        ": " +
        elem.children[1].textContent.trim()
    );
    return;
  }

  // Generate the text representation for each child node.
  let node = elem.firstChild;
  while (node) {
    if (node.nodeType == Node.TEXT_NODE) {
      // Text belonging to this element uses its indentation level.
      generateTextForTextNode(node, indent, textFragmentAccumulator);
    } else if (node.nodeType == Node.ELEMENT_NODE) {
      // Recurse on the child element with an extra level of indentation (but
      // only if there's more than one child).
      generateTextForElement(
        node,
        aHidePrivateData,
        indent + (childCount > 1 ? "  " : ""),
        textFragmentAccumulator
      );
    }
    // Advance!
    node = node.nextSibling;
  }
}

function generateTextForTextNode(node, indent, textFragmentAccumulator) {
  // If the text node is the first of a run of text nodes, then start
  // a new line and add the initial indentation.
  let prevNode = node.previousSibling;
  if (!prevNode || prevNode.nodeType == Node.TEXT_NODE) {
    textFragmentAccumulator.push("\n" + indent);
  }

  // Trim the text node's text content and add proper indentation after
  // any internal line breaks.
  let text = node.textContent.trim().replace(/\n/g, "\n" + indent);
  textFragmentAccumulator.push(text);
}

/**
 * Returns a plaintext representation of crashes data.
 */

function getCrashesText(aIndent) {
  let crashesData = "";
  let recentCrashesSubmitted = document.querySelectorAll("#crashes-tbody > tr");
  for (let i = 0; i < recentCrashesSubmitted.length; i++) {
    let tds = recentCrashesSubmitted.item(i).querySelectorAll("td");
    crashesData +=
      aIndent.repeat(2) +
      tds.item(0).firstElementChild.href +
      " (" +
      tds.item(1).textContent +
      ")\n";
  }
  return crashesData;
}