ui.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright 2011 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. (function() {
  21. singlefile.ui = {};
  22. var DEFAULT_ICON_PATH = "../resources/icon_19.png";
  23. var DEFAULT_BADGE_CONFIG = {
  24. text : "",
  25. color : [ 64, 64, 64, 255 ],
  26. title : "Process this page with SingleFile",
  27. path : DEFAULT_ICON_PATH
  28. };
  29. var badgeStates = [], currentBarProgress = -1, currentProgress = -1, tabs = {}, badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  30. function refreshBadge(tabId) {
  31. function refreshTabBadge(tabId) {
  32. function refreshBadgeProperty(property, fn) {
  33. var badgeState;
  34. var tabData = tabs[tabId], confObject = {
  35. tabId : tabId
  36. };
  37. if (!badgeStates[tabId])
  38. badgeStates[tabId] = [];
  39. badgeState = badgeStates[tabId];
  40. if (tabData && tabData[property]) {
  41. if (badgeState[property] != tabData[property]) {
  42. badgeState[property] = tabData[property];
  43. confObject[property] = tabData[property];
  44. fn(confObject);
  45. }
  46. } else {
  47. if (badgeState[property] != badgeConfig[property]) {
  48. badgeState[property] = badgeConfig[property];
  49. confObject[property] = badgeConfig[property];
  50. fn(confObject);
  51. }
  52. }
  53. }
  54. refreshBadgeProperty("text", chrome.browserAction.setBadgeText);
  55. refreshBadgeProperty("color", chrome.browserAction.setBadgeBackgroundColor);
  56. refreshBadgeProperty("title", chrome.browserAction.setTitle);
  57. refreshBadgeProperty("path", chrome.browserAction.setIcon);
  58. }
  59. // TODO : getAllInWindow is deprecated
  60. if (tabId)
  61. refreshTabBadge(tabId);
  62. else
  63. chrome.tabs.getAllInWindow(null, function(tabs) {
  64. tabs.forEach(function(tab) {
  65. refreshTabBadge(tab.id);
  66. });
  67. });
  68. }
  69. singlefile.ui.notifyProcessInit = function(tabId) {
  70. var tabData = {
  71. id : tabId,
  72. text : "...",
  73. color : [ 2, 147, 20, 255 ],
  74. title : "Initialize process...",
  75. path : DEFAULT_ICON_PATH,
  76. processing : true
  77. };
  78. tabs[tabId] = tabData;
  79. refreshBadge(tabId);
  80. };
  81. singlefile.ui.notifyProcessStart = function(tabId, processingPagesCount) {
  82. delete tabs[tabId].text;
  83. delete tabs[tabId].title;
  84. delete tabs[tabId].path;
  85. tabs[tabId].color = [ 4, 229, 36, 255 ];
  86. badgeConfig.text = "" + processingPagesCount;
  87. refreshBadge();
  88. };
  89. singlefile.ui.notifyProcessError = function(tabId) {
  90. delete tabs[tabId].processing;
  91. tabs[tabId].color = [ 229, 4, 12, 255 ];
  92. tabs[tabId].text = "ERR";
  93. refreshBadge(tabId);
  94. };
  95. singlefile.ui.notifyProcessEnd = function(tabId, processingPagesCount, displayBanner, url, title) {
  96. var params = encodeURIComponent(url) + "&" + encodeURIComponent(title);
  97. if (displayBanner)
  98. chrome.tabs.sendMessage(tabId, {
  99. displayBanner : true,
  100. url : chrome.extension.getURL("/pages/banner.html") + "?" + params
  101. });
  102. tabs[tabId].text = "OK";
  103. delete tabs[tabId].processing;
  104. badgeConfig.text = ("" + processingPagesCount || "");
  105. if (!processingPagesCount) {
  106. currentBarProgress = -1;
  107. currentProgress = -1;
  108. delete tabs[tabId].title;
  109. badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  110. }
  111. refreshBadge();
  112. };
  113. singlefile.ui.notifyProcessProgress = function(index, maxIndex) {
  114. var barProgress, progress;
  115. if (maxIndex) {
  116. progress = Math.min(100, Math.floor((index / maxIndex) * 100));
  117. if (currentProgress != progress) {
  118. currentProgress = progress;
  119. badgeConfig.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  120. barProgress = Math.floor((index / maxIndex) * 15);
  121. if (currentBarProgress != barProgress) {
  122. currentBarProgress = barProgress;
  123. badgeConfig.path = "../resources/icon_19_wait" + barProgress + ".png";
  124. }
  125. refreshBadge();
  126. }
  127. }
  128. };
  129. singlefile.ui.notifyTabRemoved = function(tabId) {
  130. delete tabs[tabId];
  131. delete badgeStates[tabId];
  132. };
  133. singlefile.ui.notifyProcessable = function(tabId, processable, reset) {
  134. if (processable)
  135. chrome.browserAction.enable(tabId);
  136. if (!processable) {
  137. chrome.browserAction.disable(tabId);
  138. } else if (reset && tabs[tabId] && !tabs[tabId].processing) {
  139. delete tabs[tabId];
  140. refreshBadge(tabId);
  141. }
  142. };
  143. })();