ui-button.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 config from "./../../core/bg/config.js";
  25. import { queryTabs } from "./../../core/bg/tabs-util.js";
  26. import * as tabsData from "./../../core/bg/tabs-data.js";
  27. import { autoSaveIsEnabled } from "../../core/bg/autosave-util.js";
  28. const DEFAULT_ICON_PATH = "/src/ui/resources/icon_128.png";
  29. const WAIT_ICON_PATH_PREFIX = "/src/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. let business;
  109. browser.browserAction.onClicked.addListener(async tab => {
  110. const highlightedTabs = await queryTabs({ currentWindow: true, highlighted: true });
  111. if (highlightedTabs.length <= 1) {
  112. toggleSaveTab(tab);
  113. } else {
  114. business.saveTabs(highlightedTabs);
  115. }
  116. function toggleSaveTab(tab) {
  117. if (business.isSavingTab(tab)) {
  118. business.cancelTab(tab.id);
  119. } else {
  120. business.saveTabs([tab]);
  121. }
  122. }
  123. });
  124. export {
  125. onMessage,
  126. onStart,
  127. onUploadProgress,
  128. onForbiddenDomain,
  129. onError,
  130. onEdit,
  131. onEnd,
  132. onCancelled,
  133. refreshTab,
  134. init
  135. };
  136. function init(businessApi) {
  137. business = businessApi;
  138. }
  139. function onMessage(message, sender) {
  140. if (message.method.endsWith(".processInit")) {
  141. const allTabsData = tabsData.getTemporary(sender.tab.id);
  142. delete allTabsData[sender.tab.id].button;
  143. refreshTab(sender.tab);
  144. }
  145. if (message.method.endsWith(".processProgress")) {
  146. if (message.maxIndex) {
  147. onSaveProgress(sender.tab.id, message.index, message.maxIndex);
  148. }
  149. }
  150. if (message.method.endsWith(".processEnd")) {
  151. onEnd(sender.tab.id);
  152. }
  153. if (message.method.endsWith(".processError")) {
  154. if (message.error) {
  155. console.error("Initialization error", message.error); // eslint-disable-line no-console
  156. }
  157. onError(sender.tab.id);
  158. }
  159. if (message.method.endsWith(".processCancelled")) {
  160. onCancelled(sender.tab);
  161. }
  162. return Promise.resolve({});
  163. }
  164. function onStart(tabId, step, autoSave) {
  165. let state;
  166. if (autoSave) {
  167. state = getButtonState("inject", true);
  168. } else {
  169. state = step == INJECT_SCRIPTS_STEP ? getButtonState("inject") : getButtonState("execute");
  170. state.setTitle = { title: BUTTON_INITIALIZING_TOOLTIP_MESSAGE + " (" + step + "/2)" };
  171. state.setIcon = { path: WAIT_ICON_PATH_PREFIX + "0.png" };
  172. }
  173. refresh(tabId, state);
  174. }
  175. function onError(tabId) {
  176. refresh(tabId, getButtonState("error"));
  177. }
  178. function onEdit(tabId) {
  179. refresh(tabId, getButtonState("edit"));
  180. }
  181. function onEnd(tabId, autoSave) {
  182. refresh(tabId, autoSave ? getButtonState("default", true) : getButtonState("end"));
  183. }
  184. function onForbiddenDomain(tab) {
  185. refresh(tab.id, getButtonState("forbidden"));
  186. }
  187. function onCancelled(tab) {
  188. refreshTab(tab);
  189. }
  190. function onSaveProgress(tabId, index, maxIndex) {
  191. onProgress(tabId, index, maxIndex, BUTTON_SAVE_PROGRESS_TOOLTIP_MESSAGE);
  192. }
  193. function onUploadProgress(tabId, index, maxIndex) {
  194. onProgress(tabId, index, maxIndex, BUTTON_UPLOAD_PROGRESS_TOOLTIP_MESSAGE);
  195. }
  196. function onProgress(tabId, index, maxIndex, tooltipMessage) {
  197. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  198. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  199. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  200. const state = getButtonState("progress");
  201. state.setTitle = { title: tooltipMessage + (progress * 5) + "%" };
  202. state.setIcon = { path };
  203. refresh(tabId, state);
  204. }
  205. async function refreshTab(tab) {
  206. const autoSave = await autoSaveIsEnabled(tab);
  207. const state = getButtonState("default", autoSave);
  208. await refresh(tab.id, state);
  209. }
  210. async function refresh(tabId, state) {
  211. try {
  212. const allTabsData = tabsData.getTemporary(tabId);
  213. if (state) {
  214. if (!allTabsData[tabId].button) {
  215. allTabsData[tabId].button = { lastState: null };
  216. }
  217. const lastState = allTabsData[tabId].button.lastState || {};
  218. const newState = {};
  219. Object.keys(state).forEach(property => {
  220. if (state[property] !== undefined && (JSON.stringify(lastState[property]) != JSON.stringify(state[property]))) {
  221. newState[property] = state[property];
  222. }
  223. });
  224. if (Object.keys(newState).length) {
  225. allTabsData[tabId].button.lastState = state;
  226. await refreshAsync(tabId, newState);
  227. }
  228. }
  229. } catch (error) {
  230. // ignored
  231. }
  232. }
  233. async function refreshAsync(tabId, state) {
  234. for (const browserActionMethod of Object.keys(state)) {
  235. await refreshProperty(tabId, browserActionMethod, state[browserActionMethod]);
  236. }
  237. }
  238. async function refreshProperty(tabId, browserActionMethod, browserActionParameter) {
  239. const actionMethodSupported = browserActionMethod != "setBadgeBackgroundColor" || config.BADGE_COLOR_SUPPORTED;
  240. if (browser.browserAction[browserActionMethod] && actionMethodSupported) {
  241. const parameter = JSON.parse(JSON.stringify(browserActionParameter));
  242. parameter.tabId = tabId;
  243. await browser.browserAction[browserActionMethod](parameter);
  244. }
  245. }
  246. function getButtonState(name, autoSave) {
  247. return JSON.parse(JSON.stringify(autoSave ? BUTTON_STATES.autosave[name] : BUTTON_STATES[name]));
  248. }