ui-pendings.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2010-2020 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, window, document, setInterval, location */
  24. const URLLabel = document.getElementById("URLLabel");
  25. const titleLabel = document.getElementById("titleLabel");
  26. const resultsTable = document.getElementById("resultsTable");
  27. const cancelAllButton = document.getElementById("cancelAllButton");
  28. const addUrlsButton = document.getElementById("addUrlsButton");
  29. const addUrlsInput = document.getElementById("addUrlsInput");
  30. const addUrlsCancelButton = document.getElementById("addUrlsCancelButton");
  31. const addUrlsOKButton = document.getElementById("addUrlsOKButton");
  32. document.title = browser.i18n.getMessage("pendingsTitle");
  33. cancelAllButton.textContent = browser.i18n.getMessage("pendingsCancelAllButton");
  34. addUrlsButton.textContent = browser.i18n.getMessage("pendingsAddUrlsButton");
  35. addUrlsCancelButton.textContent = browser.i18n.getMessage("pendingsAddUrlsCancelButton");
  36. addUrlsOKButton.textContent = browser.i18n.getMessage("pendingsAddUrlsOKButton");
  37. document.getElementById("addUrlsLabel").textContent = browser.i18n.getMessage("pendingsAddUrls");
  38. URLLabel.textContent = browser.i18n.getMessage("pendingsURLTitle");
  39. titleLabel.textContent = browser.i18n.getMessage("pendingsTitleTitle");
  40. document.getElementById("statusLabel").textContent = browser.i18n.getMessage("pendingsStatusTitle");
  41. const statusText = {
  42. pending: browser.i18n.getMessage("pendingsPendingStatus"),
  43. processing: browser.i18n.getMessage("pendingsProcessingStatus"),
  44. cancelling: browser.i18n.getMessage("pendingsCancellingStatus")
  45. };
  46. const noPendingsText = browser.i18n.getMessage("pendingsNoPendings");
  47. cancelAllButton.onclick = async () => {
  48. await browser.runtime.sendMessage({ method: "downloads.cancelAll" });
  49. await refresh();
  50. };
  51. addUrlsButton.onclick = displayAddUrlsPopup;
  52. if (location.href.endsWith("#side-panel")) {
  53. document.documentElement.classList.add("side-panel");
  54. }
  55. let URLDisplayed = true;
  56. document.getElementById("URLTitleLabel").onclick = () => {
  57. URLDisplayed = !URLDisplayed;
  58. refresh(true);
  59. };
  60. let previousState;
  61. setInterval(refresh, 1000);
  62. refresh();
  63. function resetTable() {
  64. resultsTable.innerHTML = "";
  65. }
  66. function updateTable(results) {
  67. if (results.length) {
  68. results.sort((taskInfo1, taskInfo2) => taskInfo1.index - taskInfo2.index);
  69. results.forEach((taskInfo) => {
  70. const row = document.createElement("div");
  71. const cellURL = document.createElement("span");
  72. const cellStatus = document.createElement("span");
  73. const cellCancel = document.createElement("span");
  74. const buttonCancel = document.createElement("button");
  75. row.className = "result-row";
  76. if (URLDisplayed) {
  77. cellURL.textContent = taskInfo.url;
  78. } else {
  79. cellURL.textContent = taskInfo.title;
  80. }
  81. cellURL.className = "result-url-title";
  82. cellURL.onclick = () => selectTab(taskInfo.tabId);
  83. if (taskInfo.cancelled) {
  84. cellStatus.textContent = statusText.cancelling;
  85. } else {
  86. cellStatus.textContent = statusText[taskInfo.status];
  87. buttonCancel.textContent = "×";
  88. buttonCancel.onclick = () => cancel(taskInfo.id);
  89. cellCancel.appendChild(buttonCancel);
  90. }
  91. cellStatus.className = "result-status";
  92. cellCancel.className = "result-cancel";
  93. row.appendChild(cellURL);
  94. row.appendChild(cellStatus);
  95. row.appendChild(cellCancel);
  96. resultsTable.appendChild(row);
  97. });
  98. }
  99. }
  100. async function cancel(taskId) {
  101. await browser.runtime.sendMessage({ method: "downloads.cancel", taskId });
  102. await refresh();
  103. }
  104. async function selectTab(tabId) {
  105. await browser.runtime.sendMessage({ method: "tabs.activate", tabId });
  106. await refresh();
  107. }
  108. async function displayAddUrlsPopup() {
  109. document.getElementById("formAddUrls").style.setProperty("display", "flex");
  110. document.querySelector("#formAddUrls .popup-content").style.setProperty("align-self", "center");
  111. addUrlsInput.value = "";
  112. addUrlsInput.focus();
  113. document.body.style.setProperty("overflow-y", "hidden");
  114. const urls = await new Promise(resolve => {
  115. addUrlsOKButton.onclick = event => hideAndResolve(event, addUrlsInput.value);
  116. addUrlsCancelButton.onclick = event => hideAndResolve(event);
  117. window.onkeyup = event => {
  118. if (event.key == "Escape") {
  119. hideAndResolve(event);
  120. }
  121. };
  122. function hideAndResolve(event, value = "") {
  123. event.preventDefault();
  124. document.getElementById("formAddUrls").style.setProperty("display", "none");
  125. document.body.style.setProperty("overflow-y", "");
  126. resolve(value.split("\n").map(url => url.trim()).filter(url => url));
  127. }
  128. });
  129. if (urls.length) {
  130. await browser.runtime.sendMessage({ method: "downloads.saveUrls", urls });
  131. }
  132. }
  133. async function refresh(force) {
  134. const results = await browser.runtime.sendMessage({ method: "downloads.getInfo" });
  135. const currentState = JSON.stringify(results);
  136. if (previousState != currentState || force) {
  137. previousState = currentState;
  138. resetTable();
  139. if (URLDisplayed) {
  140. URLLabel.className = "";
  141. titleLabel.className = "unselected";
  142. } else {
  143. URLLabel.className = "unselected";
  144. titleLabel.className = "";
  145. }
  146. updateTable(results);
  147. if (!results.length) {
  148. const row = document.createElement("div");
  149. row.className = "result-row";
  150. const cell = document.createElement("span");
  151. cell.colSpan = 3;
  152. cell.className = "no-result";
  153. cell.textContent = noPendingsText;
  154. row.appendChild(cell);
  155. resultsTable.appendChild(row);
  156. }
  157. }
  158. }