ui-pendings.js 4.7 KB

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