ui-pendings.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, document, setInterval */
  24. (async () => {
  25. const URLLabel = document.getElementById("URLLabel");
  26. const statusLabel = document.getElementById("statusLabel");
  27. const resultsTable = document.getElementById("resultsTable");
  28. const cancelAllButton = document.getElementById("cancelAllButton");
  29. document.title = browser.i18n.getMessage("pendingsTitle");
  30. cancelAllButton.textContent = browser.i18n.getMessage("pendingsCancelAllButton");
  31. URLLabel.textContent = browser.i18n.getMessage("pendingsURLTitle");
  32. statusLabel.textContent = browser.i18n.getMessage("pendingsStatusTitle");
  33. const statusText = {
  34. pending: browser.i18n.getMessage("pendingsPendingStatus"),
  35. processing: browser.i18n.getMessage("pendingsProcessingStatus"),
  36. cancelling: browser.i18n.getMessage("pendingsCancellingStatus")
  37. };
  38. const noPendingsText = browser.i18n.getMessage("pendingsNoPendings");
  39. cancelAllButton.onclick = async () => {
  40. const results = await browser.runtime.sendMessage({ method: "downloads.getInfo" });
  41. await Promise.all(results.pending.concat(results.processing).map(([tabId]) => browser.runtime.sendMessage({ method: "downloads.cancel", tabId })));
  42. await refresh();
  43. };
  44. let previousState;
  45. setInterval(refresh, 1000);
  46. await refresh();
  47. function resetTable() {
  48. resultsTable.innerHTML = "";
  49. }
  50. function updateTable(results, type) {
  51. const data = results[type];
  52. if (data.length) {
  53. data.sort(([, tabInfo1], [, tabInfo2]) => tabInfo1.index - tabInfo2.index);
  54. data.forEach(([tabId, tabInfo]) => {
  55. const row = document.createElement("div");
  56. const cellURL = document.createElement("span");
  57. const cellStatus = document.createElement("span");
  58. const cellCancel = document.createElement("span");
  59. const buttonCancel = document.createElement("button");
  60. row.dataset.tabId = tabId;
  61. row.className = "result-row";
  62. cellURL.textContent = tabInfo.url;
  63. cellURL.className = "result-url";
  64. cellURL.onclick = () => selectTab(type, tabId);
  65. if (tabInfo.cancelled) {
  66. cellStatus.textContent = statusText.cancelling;
  67. } else {
  68. cellStatus.textContent = statusText[type];
  69. buttonCancel.textContent = "×";
  70. buttonCancel.onclick = () => cancel(type, tabId);
  71. cellCancel.appendChild(buttonCancel);
  72. }
  73. cellStatus.className = "result-status";
  74. cellCancel.className = "result-cancel";
  75. row.appendChild(cellURL);
  76. row.appendChild(cellStatus);
  77. row.appendChild(cellCancel);
  78. resultsTable.appendChild(row);
  79. });
  80. }
  81. }
  82. async function cancel(type, tabId) {
  83. await browser.runtime.sendMessage({ method: "downloads.cancel", tabId });
  84. await refresh();
  85. }
  86. async function selectTab(type, tabId) {
  87. await browser.runtime.sendMessage({ method: "tabs.activate", tabId });
  88. await refresh();
  89. }
  90. async function refresh() {
  91. const results = await browser.runtime.sendMessage({ method: "downloads.getInfo" });
  92. const currentState = JSON.stringify(results);
  93. if (previousState != currentState) {
  94. previousState = currentState;
  95. resetTable();
  96. updateTable(results, "processing");
  97. updateTable(results, "pending");
  98. if (!results.pending.length && !results.processing.length) {
  99. const row = document.createElement("div");
  100. row.className = "result-row";
  101. const cell = document.createElement("span");
  102. cell.colSpan = 3;
  103. cell.className = "no-result";
  104. cell.textContent = noPendingsText;
  105. row.appendChild(cell);
  106. resultsTable.appendChild(row);
  107. }
  108. }
  109. }
  110. })();