ui-pendings.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. document.title = browser.i18n.getMessage("pendingsTitle");
  30. cancelAllButton.textContent = browser.i18n.getMessage("pendingsCancelAllButton");
  31. addUrlsButton.textContent = browser.i18n.getMessage("pendingsAddUrlsButton");
  32. document.getElementById("addUrlsLabel").textContent = browser.i18n.getMessage("pendingsAddUrls");
  33. URLLabel.textContent = browser.i18n.getMessage("pendingsURLTitle");
  34. titleLabel.textContent = browser.i18n.getMessage("pendingsTitleTitle");
  35. document.getElementById("statusLabel").textContent = browser.i18n.getMessage("pendingsStatusTitle");
  36. const statusText = {
  37. pending: browser.i18n.getMessage("pendingsPendingStatus"),
  38. processing: browser.i18n.getMessage("pendingsProcessingStatus"),
  39. cancelling: browser.i18n.getMessage("pendingsCancellingStatus")
  40. };
  41. const noPendingsText = browser.i18n.getMessage("pendingsNoPendings");
  42. cancelAllButton.onclick = async () => {
  43. await browser.runtime.sendMessage({ method: "downloads.cancelAll" });
  44. await refresh();
  45. };
  46. addUrlsButton.onclick = () => window.open("batch-save-urls.html", "sf-add-urls");
  47. if (location.href.endsWith("#side-panel")) {
  48. document.documentElement.classList.add("side-panel");
  49. }
  50. let URLDisplayed = true;
  51. document.getElementById("URLTitleLabel").onclick = () => {
  52. URLDisplayed = !URLDisplayed;
  53. refresh(true);
  54. };
  55. let previousState;
  56. setInterval(refresh, 1000);
  57. refresh();
  58. function resetTable() {
  59. resultsTable.innerHTML = "";
  60. }
  61. function updateTable(results) {
  62. if (results.length) {
  63. results.sort((taskInfo1, taskInfo2) => taskInfo1.index - taskInfo2.index);
  64. results.forEach((taskInfo) => {
  65. const row = document.createElement("div");
  66. const cellURL = document.createElement("span");
  67. const cellStatus = document.createElement("span");
  68. const cellCancel = document.createElement("span");
  69. const buttonCancel = document.createElement("button");
  70. row.className = "result-row";
  71. if (URLDisplayed) {
  72. cellURL.textContent = taskInfo.url;
  73. } else {
  74. cellURL.textContent = taskInfo.title;
  75. }
  76. cellURL.className = "result-url-title";
  77. cellURL.onclick = () => selectTab(taskInfo.tabId);
  78. if (taskInfo.cancelled) {
  79. cellStatus.textContent = statusText.cancelling;
  80. } else {
  81. cellStatus.textContent = statusText[taskInfo.status];
  82. buttonCancel.textContent = "×";
  83. buttonCancel.onclick = () => cancel(taskInfo.id);
  84. cellCancel.appendChild(buttonCancel);
  85. }
  86. cellStatus.className = "result-status";
  87. cellCancel.className = "result-cancel";
  88. row.appendChild(cellURL);
  89. row.appendChild(cellStatus);
  90. row.appendChild(cellCancel);
  91. resultsTable.appendChild(row);
  92. });
  93. }
  94. }
  95. async function cancel(taskId) {
  96. await browser.runtime.sendMessage({ method: "downloads.cancel", taskId });
  97. await refresh();
  98. }
  99. async function selectTab(tabId) {
  100. await browser.runtime.sendMessage({ method: "tabs.activate", tabId });
  101. await refresh();
  102. }
  103. async function refresh(force) {
  104. const results = await browser.runtime.sendMessage({ method: "downloads.getInfo" });
  105. const currentState = JSON.stringify(results);
  106. if (previousState != currentState || force) {
  107. previousState = currentState;
  108. resetTable();
  109. if (URLDisplayed) {
  110. URLLabel.className = "";
  111. titleLabel.className = "unselected";
  112. } else {
  113. URLLabel.className = "unselected";
  114. titleLabel.className = "";
  115. }
  116. updateTable(results);
  117. if (!results.length) {
  118. const row = document.createElement("div");
  119. row.className = "result-row";
  120. const cell = document.createElement("span");
  121. cell.colSpan = 3;
  122. cell.className = "no-result";
  123. cell.textContent = noPendingsText;
  124. row.appendChild(cell);
  125. resultsTable.appendChild(row);
  126. }
  127. }
  128. }