ui-button.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2010-2020 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 */
  24. import * as business from "./../../core/bg/business.js";
  25. import * as tabs from "./../../core/bg/tabs.js";
  26. import * as tabsData from "./../../core/bg/tabs-data.js";
  27. import * as autosave from "./../../core/bg/autosave.js";
  28. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_128.png";
  29. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_128_wait";
  30. const BUTTON_DEFAULT_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonDefaultTooltip");
  31. const BUTTON_BLOCKED_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonBlockedTooltip");
  32. const BUTTON_DEFAULT_BADGE_MESSAGE = "";
  33. const BUTTON_INITIALIZING_BADGE_MESSAGE = browser.i18n.getMessage("buttonInitializingBadge");
  34. const BUTTON_INITIALIZING_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonInitializingTooltip");
  35. const BUTTON_ERROR_BADGE_MESSAGE = browser.i18n.getMessage("buttonErrorBadge");
  36. const BUTTON_BLOCKED_BADGE_MESSAGE = browser.i18n.getMessage("buttonBlockedBadge");
  37. const BUTTON_OK_BADGE_MESSAGE = browser.i18n.getMessage("buttonOKBadge");
  38. const BUTTON_SAVE_PROGRESS_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonSaveProgressTooltip");
  39. const BUTTON_UPLOAD_PROGRESS_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonUploadProgressTooltip");
  40. const BUTTON_AUTOSAVE_ACTIVE_BADGE_MESSAGE = browser.i18n.getMessage("buttonAutoSaveActiveBadge");
  41. const BUTTON_AUTOSAVE_ACTIVE_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonAutoSaveActiveTooltip");
  42. const DEFAULT_COLOR = [2, 147, 20, 192];
  43. const ACTIVE_COLOR = [4, 229, 36, 192];
  44. const FORBIDDEN_COLOR = [255, 255, 255, 1];
  45. const ERROR_COLOR = [229, 4, 12, 192];
  46. const AUTOSAVE_DEFAULT_COLOR = [208, 208, 208, 192];
  47. const AUTOSAVE_INITIALIZING_COLOR = [64, 64, 64, 192];
  48. const INJECT_SCRIPTS_STEP = 1;
  49. const BUTTON_STATES = {
  50. default: {
  51. setBadgeBackgroundColor: { color: DEFAULT_COLOR },
  52. setBadgeText: { text: BUTTON_DEFAULT_BADGE_MESSAGE },
  53. setTitle: { title: BUTTON_DEFAULT_TOOLTIP_MESSAGE },
  54. setIcon: { path: DEFAULT_ICON_PATH }
  55. },
  56. inject: {
  57. setBadgeBackgroundColor: { color: DEFAULT_COLOR },
  58. setBadgeText: { text: BUTTON_INITIALIZING_BADGE_MESSAGE },
  59. setTitle: { title: BUTTON_INITIALIZING_TOOLTIP_MESSAGE },
  60. },
  61. execute: {
  62. setBadgeBackgroundColor: { color: ACTIVE_COLOR },
  63. setBadgeText: { text: BUTTON_INITIALIZING_BADGE_MESSAGE },
  64. },
  65. progress: {
  66. setBadgeBackgroundColor: { color: ACTIVE_COLOR },
  67. setBadgeText: { text: BUTTON_DEFAULT_BADGE_MESSAGE }
  68. },
  69. edit: {
  70. setBadgeBackgroundColor: { color: DEFAULT_COLOR },
  71. setBadgeText: { text: BUTTON_DEFAULT_BADGE_MESSAGE },
  72. setTitle: { title: BUTTON_DEFAULT_TOOLTIP_MESSAGE },
  73. setIcon: { path: DEFAULT_ICON_PATH }
  74. },
  75. end: {
  76. setBadgeBackgroundColor: { color: ACTIVE_COLOR },
  77. setBadgeText: { text: BUTTON_OK_BADGE_MESSAGE },
  78. setTitle: { title: BUTTON_DEFAULT_TOOLTIP_MESSAGE },
  79. setIcon: { path: DEFAULT_ICON_PATH }
  80. },
  81. error: {
  82. setBadgeBackgroundColor: { color: ERROR_COLOR },
  83. setBadgeText: { text: BUTTON_ERROR_BADGE_MESSAGE },
  84. setTitle: { title: BUTTON_DEFAULT_BADGE_MESSAGE },
  85. setIcon: { path: DEFAULT_ICON_PATH }
  86. },
  87. forbidden: {
  88. setBadgeBackgroundColor: { color: FORBIDDEN_COLOR },
  89. setBadgeText: { text: BUTTON_BLOCKED_BADGE_MESSAGE },
  90. setTitle: { title: BUTTON_BLOCKED_TOOLTIP_MESSAGE },
  91. setIcon: { path: DEFAULT_ICON_PATH }
  92. },
  93. autosave: {
  94. inject: {
  95. setBadgeBackgroundColor: { color: AUTOSAVE_INITIALIZING_COLOR },
  96. setBadgeText: { text: BUTTON_AUTOSAVE_ACTIVE_BADGE_MESSAGE },
  97. setTitle: { title: BUTTON_AUTOSAVE_ACTIVE_TOOLTIP_MESSAGE },
  98. setIcon: { path: DEFAULT_ICON_PATH }
  99. },
  100. default: {
  101. setBadgeBackgroundColor: { color: AUTOSAVE_DEFAULT_COLOR },
  102. setBadgeText: { text: BUTTON_AUTOSAVE_ACTIVE_BADGE_MESSAGE },
  103. setTitle: { title: BUTTON_AUTOSAVE_ACTIVE_TOOLTIP_MESSAGE },
  104. setIcon: { path: DEFAULT_ICON_PATH }
  105. }
  106. }
  107. };
  108. browser.browserAction.onClicked.addListener(async tab => {
  109. const highlightedTabs = await tabs.get({ currentWindow: true, highlighted: true });
  110. if (highlightedTabs.length <= 1) {
  111. toggleSaveTab(tab);
  112. } else {
  113. business.saveTabs(highlightedTabs);
  114. }
  115. function toggleSaveTab(tab) {
  116. if (business.isSavingTab(tab)) {
  117. business.cancelTab(tab.id);
  118. } else {
  119. business.saveTabs([tab]);
  120. }
  121. }
  122. });
  123. export {
  124. onMessage,
  125. onStart,
  126. onUploadProgress,
  127. onForbiddenDomain,
  128. onError,
  129. onEdit,
  130. onEnd,
  131. onCancelled,
  132. refreshTab
  133. };
  134. function onMessage(message, sender) {
  135. if (message.method.endsWith(".processInit")) {
  136. const allTabsData = tabsData.getTemporary(sender.tab.id);
  137. delete allTabsData[sender.tab.id].button;
  138. refreshTab(sender.tab);
  139. }
  140. if (message.method.endsWith(".processProgress")) {
  141. if (message.maxIndex) {
  142. onSaveProgress(sender.tab.id, message.index, message.maxIndex);
  143. }
  144. }
  145. if (message.method.endsWith(".processEnd")) {
  146. onEnd(sender.tab.id);
  147. }
  148. if (message.method.endsWith(".processError")) {
  149. if (message.error) {
  150. console.error("Initialization error", message.error); // eslint-disable-line no-console
  151. }
  152. onError(sender.tab.id);
  153. }
  154. if (message.method.endsWith(".processCancelled")) {
  155. onCancelled(sender.tab);
  156. }
  157. return Promise.resolve({});
  158. }
  159. function onStart(tabId, step, autoSave) {
  160. let state;
  161. if (autoSave) {
  162. state = getButtonState("inject", true);
  163. } else {
  164. state = step == INJECT_SCRIPTS_STEP ? getButtonState("inject") : getButtonState("execute");
  165. state.setTitle = { title: BUTTON_INITIALIZING_TOOLTIP_MESSAGE + " (" + step + "/2)" };
  166. state.setIcon = { path: WAIT_ICON_PATH_PREFIX + "0.png" };
  167. }
  168. refresh(tabId, state);
  169. }
  170. function onError(tabId) {
  171. refresh(tabId, getButtonState("error"));
  172. }
  173. function onEdit(tabId) {
  174. refresh(tabId, getButtonState("edit"));
  175. }
  176. function onEnd(tabId, autoSave) {
  177. refresh(tabId, autoSave ? getButtonState("default", true) : getButtonState("end"));
  178. }
  179. function onForbiddenDomain(tab) {
  180. refresh(tab.id, getButtonState("forbidden"));
  181. }
  182. function onCancelled(tab) {
  183. refreshTab(tab);
  184. }
  185. function onSaveProgress(tabId, index, maxIndex) {
  186. onProgress(tabId, index, maxIndex, BUTTON_SAVE_PROGRESS_TOOLTIP_MESSAGE);
  187. }
  188. function onUploadProgress(tabId, index, maxIndex) {
  189. onProgress(tabId, index, maxIndex, BUTTON_UPLOAD_PROGRESS_TOOLTIP_MESSAGE);
  190. }
  191. function onProgress(tabId, index, maxIndex, tooltipMessage) {
  192. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  193. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  194. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  195. const state = getButtonState("progress");
  196. state.setTitle = { title: tooltipMessage + (progress * 5) + "%" };
  197. state.setIcon = { path };
  198. refresh(tabId, state);
  199. }
  200. async function refreshTab(tab) {
  201. const autoSave = await autosave.isEnabled(tab);
  202. const state = getButtonState("default", autoSave);
  203. await refresh(tab.id, state);
  204. }
  205. async function refresh(tabId, state) {
  206. const allTabsData = tabsData.getTemporary(tabId);
  207. if (state) {
  208. if (!allTabsData[tabId].button) {
  209. allTabsData[tabId].button = { lastState: null };
  210. }
  211. const lastState = allTabsData[tabId].button.lastState || {};
  212. const newState = {};
  213. Object.keys(state).forEach(property => {
  214. if (state[property] !== undefined && (JSON.stringify(lastState[property]) != JSON.stringify(state[property]))) {
  215. newState[property] = state[property];
  216. }
  217. });
  218. if (Object.keys(newState).length) {
  219. allTabsData[tabId].button.lastState = state;
  220. await refreshAsync(tabId, newState);
  221. }
  222. }
  223. }
  224. async function refreshAsync(tabId, state) {
  225. for (const browserActionMethod of Object.keys(state)) {
  226. await refreshProperty(tabId, browserActionMethod, state[browserActionMethod]);
  227. }
  228. }
  229. async function refreshProperty(tabId, browserActionMethod, browserActionParameter) {
  230. if (browser.browserAction[browserActionMethod]) {
  231. const parameter = JSON.parse(JSON.stringify(browserActionParameter));
  232. parameter.tabId = tabId;
  233. await browser.browserAction[browserActionMethod](parameter);
  234. }
  235. }
  236. function getButtonState(name, autoSave) {
  237. return JSON.parse(JSON.stringify(autoSave ? BUTTON_STATES.autosave[name] : BUTTON_STATES[name]));
  238. }