ui.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global singlefile, navigator */
  21. singlefile.ui = (() => {
  22. const browser = this.browser || this.chrome;
  23. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_19.png";
  24. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_19_wait";
  25. const DEFAULT_TITLE = "Process this page with SingleFile";
  26. const DEFAULT_COLOR = [2, 147, 20, 255];
  27. const BADGE_PROPERTIES = [{ name: "text", browserActionMethod: "setBadgeText" }, { name: "color", browserActionMethod: "setBadgeBackgroundColor" }, { name: "title", browserActionMethod: "setTitle" }, { name: "path", browserActionMethod: "setIcon" }];
  28. const RUNNING_IN_EDGE = navigator.userAgent.includes("Edge");
  29. const tabs = {};
  30. const badgeTabs = {};
  31. let badgeRefreshPending = [];
  32. return {
  33. init(tabId) {
  34. tabs[tabId] = {
  35. id: tabId,
  36. text: "...",
  37. color: DEFAULT_COLOR,
  38. title: "initializing...",
  39. path: DEFAULT_ICON_PATH,
  40. progress: -1,
  41. barProgress: -1
  42. };
  43. refreshBadge(tabId);
  44. },
  45. error(tabId) {
  46. const tabData = tabs[tabId];
  47. tabData.text = "ERR";
  48. tabData.color = [229, 4, 12, 255];
  49. tabData.title = DEFAULT_TITLE;
  50. tabData.path = DEFAULT_ICON_PATH;
  51. tabData.progress = -1;
  52. tabData.barProgress = -1;
  53. refreshBadge(tabId);
  54. },
  55. end(tabId) {
  56. const tabData = tabs[tabId];
  57. tabData.text = "OK";
  58. tabData.color = [4, 229, 36, 255];
  59. tabData.title = DEFAULT_TITLE;
  60. tabData.path = DEFAULT_ICON_PATH;
  61. tabData.progress = -1;
  62. tabData.barProgress = -1;
  63. refreshBadge(tabId);
  64. },
  65. progress(tabId, index, maxIndex) {
  66. const tabData = tabs[tabId];
  67. const progress = Math.max(Math.min(100, Math.floor((index / maxIndex) * 100)), 0);
  68. if (tabData.progress != progress) {
  69. tabData.progress = progress;
  70. tabData.text = "";
  71. tabData.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  72. tabData.color = [4, 229, 36, 255];
  73. const barProgress = Math.floor((index / maxIndex) * 15);
  74. if (tabData.barProgress != barProgress) {
  75. tabData.barProgress = barProgress;
  76. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  77. }
  78. refreshBadge(tabId);
  79. }
  80. },
  81. removed(tabId) {
  82. delete tabs[tabId];
  83. },
  84. active(tabId, isActive) {
  85. if (isActive) {
  86. browser.browserAction.enable(tabId);
  87. } else {
  88. browser.browserAction.disable(tabId);
  89. }
  90. }
  91. };
  92. async function refreshBadge(tabId) {
  93. await Promise.all(badgeRefreshPending);
  94. const promise = refreshBadgeAsync(tabId);
  95. badgeRefreshPending.push(promise);
  96. await promise;
  97. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  98. }
  99. async function refreshBadgeAsync(tabId) {
  100. for (let property of BADGE_PROPERTIES) {
  101. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  102. }
  103. }
  104. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  105. const tabData = tabs[tabId];
  106. const value = tabData[property];
  107. if (!badgeTabs[tabId]) {
  108. badgeTabs[tabId] = {};
  109. }
  110. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  111. const browserActionParameter = { tabId };
  112. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  113. return new Promise(resolve => {
  114. if (RUNNING_IN_EDGE) {
  115. browser.browserAction[browserActionMethod](browserActionParameter);
  116. resolve();
  117. } else {
  118. browser.browserAction[browserActionMethod](browserActionParameter, resolve);
  119. }
  120. });
  121. }
  122. }
  123. })();