summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutwindowsmessages/content/aboutWindowsMessages.js
blob: 0896ae7669267d15eef55a194369c83bf9207381 (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
/* 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";

let AboutWindowsMessages = null;

function refreshMessages() {
  let windowMessages = {};
  let windowTitles = {};
  AboutWindowsMessages.getMessages(window, windowMessages, windowTitles);
  let windowsDiv = document.getElementById("windows-div");
  windowsDiv.innerHTML = "";
  const templateCard = document.querySelector("template[name=window-card]");
  for (let i = 0; i < windowTitles.value.length; ++i) {
    let windowCard = templateCard.content
      .cloneNode(true)
      .querySelector("details");
    // open the current window by default
    windowCard.open = i === 0;
    let summary = windowCard.querySelector("summary");
    let titleSpan = summary.querySelector("h3.window-card-title");
    titleSpan.appendChild(document.createTextNode(windowTitles.value[i]));
    titleSpan.classList.toggle("current-window", windowCard.open);
    let copyButton = summary.querySelector("button");
    copyButton.addEventListener("click", async e => {
      e.target.disabled = true;
      await copyMessagesToClipboard(e);
      e.target.disabled = false;
    });
    let innerUl = document.createElement("ul");
    for (let j = 0; j < windowMessages.value[i].length; ++j) {
      let innerLi = document.createElement("li");
      innerLi.className = "message";
      innerLi.innerText = windowMessages.value[i][j];
      innerUl.appendChild(innerLi);
    }
    windowCard.appendChild(innerUl);
    windowsDiv.append(windowCard);
  }
}

async function copyMessagesToClipboard(event) {
  const details = event.target.parentElement.parentElement;
  // Avoid copying the window name as it is Category 3 data,
  // and only useful for the user to identify which window
  // is which.
  const messagesText =
    Array.from(details.querySelector("ul").children)
      .map(li => li.innerText)
      .join("\n") + "\n";

  await navigator.clipboard.writeText(messagesText);
}

function onLoad() {
  refreshMessages();
}

try {
  AboutWindowsMessages = Cc["@mozilla.org/about-windowsmessages;1"].getService(
    Ci.nsIAboutWindowsMessages
  );
  document.addEventListener("DOMContentLoaded", onLoad, { once: true });
} catch (ex) {
  // Do nothing if we fail to create a singleton instance,
  // showing the default no-module message.
  console.error(ex);
}