ui.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, chrome */
  21. singlefile.ui = (() => {
  22. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_19.png";
  23. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_19_wait";
  24. const DEFAULT_TITLE = "Process this page with SingleFile";
  25. const DEFAULT_COLOR = [2, 147, 20, 255];
  26. const BADGE_PROPERTIES = [{ name: "text", browserActionMethod: "setBadgeText" }, { name: "color", browserActionMethod: "setBadgeBackgroundColor" }, { name: "title", browserActionMethod: "setTitle" }, { name: "path", browserActionMethod: "setIcon" }];
  27. const tabs = {};
  28. const badgeTabs = {};
  29. let badgeRefreshPending = [];
  30. return {
  31. init(tabId) {
  32. tabs[tabId] = {
  33. id: tabId,
  34. text: "...",
  35. color: DEFAULT_COLOR,
  36. title: "initializing...",
  37. path: DEFAULT_ICON_PATH,
  38. progress: -1,
  39. barProgress: -1
  40. };
  41. refreshBadge(tabId);
  42. },
  43. error(tabId) {
  44. const tabData = tabs[tabId];
  45. tabData.text = "ERR";
  46. tabData.color = [229, 4, 12, 255];
  47. tabData.title = DEFAULT_TITLE;
  48. tabData.path = DEFAULT_ICON_PATH;
  49. tabData.progress = -1;
  50. tabData.barProgress = -1;
  51. refreshBadge(tabId);
  52. },
  53. end(tabId) {
  54. const tabData = tabs[tabId];
  55. tabData.text = "OK";
  56. tabData.color = [4, 229, 36, 255];
  57. tabData.title = DEFAULT_TITLE;
  58. tabData.path = DEFAULT_ICON_PATH;
  59. tabData.progress = -1;
  60. tabData.barProgress = -1;
  61. refreshBadge(tabId);
  62. },
  63. progress(tabId, index, maxIndex) {
  64. const tabData = tabs[tabId];
  65. const progress = Math.max(Math.min(100, Math.floor((index / maxIndex) * 100)), 0);
  66. if (tabData.progress != progress) {
  67. tabData.progress = progress;
  68. tabData.text = "";
  69. tabData.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  70. tabData.color = [4, 229, 36, 255];
  71. const barProgress = Math.floor((index / maxIndex) * 15);
  72. if (tabData.barProgress != barProgress) {
  73. tabData.barProgress = barProgress;
  74. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  75. }
  76. refreshBadge(tabId);
  77. }
  78. },
  79. removed(tabId) {
  80. delete tabs[tabId];
  81. },
  82. active(tabId, isActive) {
  83. if (isActive) {
  84. chrome.browserAction.enable(tabId);
  85. } else {
  86. chrome.browserAction.disable(tabId);
  87. }
  88. }
  89. };
  90. async function refreshBadge(tabId) {
  91. await Promise.all(badgeRefreshPending);
  92. const promise = refreshBadgeAsync(tabId);
  93. badgeRefreshPending.push(promise);
  94. await promise;
  95. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  96. }
  97. async function refreshBadgeAsync(tabId) {
  98. for (let property of BADGE_PROPERTIES) {
  99. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  100. }
  101. }
  102. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  103. const tabData = tabs[tabId];
  104. const value = tabData[property];
  105. if (!badgeTabs[tabId]) {
  106. badgeTabs[tabId] = {};
  107. }
  108. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  109. const browserActionParameter = { tabId };
  110. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  111. return new Promise(resolve => chrome.browserAction[browserActionMethod](browserActionParameter, resolve));
  112. }
  113. }
  114. })();