ui.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_PASSIVE_ICON_PATH = "../resources/icon_19_forbidden.png";
  24. var DEFAULT_BADGE_CONFIG = {
  25. text : "",
  26. color : [ 64, 64, 64, 255 ],
  27. title : "Process this page with SingleFile",
  28. path : DEFAULT_ICON_PATH
  29. };
  30. var badgeStates = [], currentBarProgress = -1, currentProgress = -1, tabs = {}, badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  31. function refreshBadge(tabId) {
  32. function refreshTabBadge(tabId) {
  33. function refreshBadgeProperty(property, fn) {
  34. var badgeState;
  35. var tabData = tabs[tabId], confObject = {
  36. tabId : tabId
  37. };
  38. if (!badgeStates[tabId])
  39. badgeStates[tabId] = [];
  40. badgeState = badgeStates[tabId];
  41. if (tabData && tabData[property]) {
  42. if (badgeState[property] != tabData[property]) {
  43. badgeState[property] = tabData[property];
  44. confObject[property] = tabData[property];
  45. fn(confObject);
  46. }
  47. } else {
  48. if (badgeState[property] != badgeConfig[property]) {
  49. badgeState[property] = badgeConfig[property];
  50. confObject[property] = badgeConfig[property];
  51. fn(confObject);
  52. }
  53. }
  54. }
  55. refreshBadgeProperty("text", chrome.browserAction.setBadgeText);
  56. refreshBadgeProperty("color", chrome.browserAction.setBadgeBackgroundColor);
  57. refreshBadgeProperty("title", chrome.browserAction.setTitle);
  58. refreshBadgeProperty("path", chrome.browserAction.setIcon);
  59. }
  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, displayNotification, displayBanner, url, title) {
  96. var params = encodeURIComponent(url) + "&" + encodeURIComponent(title);
  97. if (displayNotification)
  98. webkitNotifications.createHTMLNotification("notification.html?" + params).show();
  99. if (displayBanner)
  100. chrome.tabs.sendRequest(tabId, {
  101. displayBanner : true,
  102. url : chrome.extension.getURL("/pages/banner.html") + "?" + params
  103. });
  104. tabs[tabId].text = "OK";
  105. delete tabs[tabId].processing;
  106. badgeConfig.text = ("" + processingPagesCount || "");
  107. if (!processingPagesCount) {
  108. currentBarProgress = -1;
  109. currentProgress = -1;
  110. delete tabs[tabId].title;
  111. badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  112. }
  113. refreshBadge();
  114. };
  115. singlefile.ui.notifyProcessProgress = function(index, maxIndex) {
  116. var barProgress, progress;
  117. if (maxIndex) {
  118. progress = Math.min(100, Math.floor((index / maxIndex) * 100));
  119. if (currentProgress != progress) {
  120. currentProgress = progress;
  121. badgeConfig.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  122. barProgress = Math.floor((index / maxIndex) * 15);
  123. if (currentBarProgress != barProgress) {
  124. currentBarProgress = barProgress;
  125. badgeConfig.path = "../resources/icon_19_wait" + barProgress + ".png";
  126. }
  127. refreshBadge();
  128. }
  129. }
  130. };
  131. singlefile.ui.notifyTabRemoved = function(tabId) {
  132. delete tabs[tabId];
  133. delete badgeStates[tabId];
  134. };
  135. singlefile.ui.notifyProcessable = function(tabId, processable, reset) {
  136. if (!processable) {
  137. tabs[tabId] = {
  138. path : DEFAULT_PASSIVE_ICON_PATH,
  139. title : "SingleFile cannot process this page"
  140. };
  141. } else if (reset && tabs[tabId] && !tabs[tabId].processing)
  142. delete tabs[tabId];
  143. refreshBadge(tabId);
  144. };
  145. })();