ui-button.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2010-2019 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, singlefile */
  24. singlefile.extension.ui.bg.button = (() => {
  25. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_128.png";
  26. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_128_wait";
  27. const BUTTON_DEFAULT_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonDefaultTooltip");
  28. const BUTTON_BLOCKED_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonBlockedTooltip");
  29. const BUTTON_INITIALIZING_BADGE_MESSAGE = browser.i18n.getMessage("buttonInitializingBadge");
  30. const BUTTON_INITIALIZING_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonInitializingTooltip");
  31. const BUTTON_ERROR_BADGE_MESSAGE = browser.i18n.getMessage("buttonErrorBadge");
  32. const BUTTON_BLOCKED_BADGE_MESSAGE = browser.i18n.getMessage("buttonBlockedBadge");
  33. const BUTTON_OK_BADGE_MESSAGE = browser.i18n.getMessage("buttonOKBadge");
  34. const BUTTON_SAVE_PROGRESS_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonSaveProgressTooltip");
  35. const BUTTON_AUTOSAVE_ACTIVE_BADGE_MESSAGE = browser.i18n.getMessage("buttonAutoSaveActiveBadge");
  36. const AUTOSAVE_COLOR = [208, 208, 208, 192];
  37. const BUTTON_AUTOSAVE_ACTIVE_TOOLTIP_MESSAGE = browser.i18n.getMessage("buttonAutoSaveActiveTooltip");
  38. const DEFAULT_COLOR = [2, 147, 20, 192];
  39. browser.browserAction.onClicked.addListener(async tab => {
  40. const business = singlefile.extension.core.bg.business;
  41. const allTabs = await singlefile.extension.core.bg.tabs.get({ currentWindow: true, highlighted: true });
  42. if (!allTabs.length) {
  43. business.saveTab(tab);
  44. } else {
  45. allTabs.forEach(tab => (tab.active || tab.highlighted) && business.saveTab(tab));
  46. }
  47. });
  48. return {
  49. onMessage,
  50. onTabCreated,
  51. onTabUpdated,
  52. onInitialize,
  53. onProgress,
  54. onEnd,
  55. onForbiddenDomain,
  56. onError,
  57. refreshTab,
  58. refresh: async (tab, options) => {
  59. if (tab.id) {
  60. await refresh(tab.id, getProperties(options));
  61. }
  62. }
  63. };
  64. function onMessage(message, sender) {
  65. if (message.method.endsWith(".loadURL")) {
  66. onLoad(sender.tab.id);
  67. }
  68. if (message.method.endsWith(".processProgress")) {
  69. if (message.maxIndex) {
  70. onProgress(sender.tab.id, message.index, message.maxIndex, message.options);
  71. }
  72. }
  73. if (message.method.endsWith(".processEnd")) {
  74. onEnd(sender.tab.id, message.options);
  75. }
  76. if (message.method.endsWith(".processError")) {
  77. if (message.error) {
  78. console.error("Initialization error", message.error); // eslint-disable-line no-console
  79. }
  80. onError(sender.tab.id, message.options);
  81. }
  82. if (message.method.endsWith(".processCancelled")) {
  83. onCancelled(sender.tab.id, message.options);
  84. }
  85. return Promise.resolve({});
  86. }
  87. function onTabUpdated(tabId, changeInfo, tab) {
  88. refreshTab(tab);
  89. }
  90. async function onTabCreated(tab) {
  91. refreshTab(tab);
  92. }
  93. function onLoad(tabId) {
  94. refresh(tabId, getProperties({}, "", DEFAULT_COLOR, BUTTON_DEFAULT_TOOLTIP_MESSAGE));
  95. }
  96. function onInitialize(tabId, options, step) {
  97. refresh(tabId, getProperties(options, BUTTON_INITIALIZING_BADGE_MESSAGE, step == 1 ? DEFAULT_COLOR : [4, 229, 36, 192], BUTTON_INITIALIZING_TOOLTIP_MESSAGE + " (" + step + "/2)", WAIT_ICON_PATH_PREFIX + "0.png"));
  98. }
  99. function onError(tabId, options) {
  100. refresh(tabId, getProperties(options, BUTTON_ERROR_BADGE_MESSAGE, [229, 4, 12, 192]));
  101. }
  102. function onForbiddenDomain(tab, options) {
  103. if (singlefile.extension.core.bg.util.isAllowedProtocol(tab.url)) {
  104. refresh(tab.id, getProperties(options, BUTTON_BLOCKED_BADGE_MESSAGE, [255, 255, 255, 1], BUTTON_BLOCKED_TOOLTIP_MESSAGE));
  105. }
  106. }
  107. function onCancelled(tabId, options) {
  108. refresh(tabId, getProperties(options, "", DEFAULT_COLOR, BUTTON_DEFAULT_TOOLTIP_MESSAGE));
  109. }
  110. function onEnd(tabId, options) {
  111. refresh(tabId, getProperties(options, BUTTON_OK_BADGE_MESSAGE, [4, 229, 36, 192]));
  112. }
  113. function onProgress(tabId, index, maxIndex, options) {
  114. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  115. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  116. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  117. refresh(tabId, getProperties(options, "", [4, 229, 36, 192], BUTTON_SAVE_PROGRESS_TOOLTIP_MESSAGE + (progress * 5) + "%", path, [128, 128, 128, 192]));
  118. }
  119. async function refreshTab(tab) {
  120. const options = { autoSave: await singlefile.extension.core.bg.autosave.isEnabled(tab) };
  121. const properties = getCurrentProperties(tab.id, options);
  122. if (singlefile.extension.core.bg.util.isAllowedURL(tab.url)) {
  123. await refresh(tab.id, properties);
  124. } else {
  125. try {
  126. await onForbiddenDomain(tab, options);
  127. } catch (error) {
  128. /* ignored */
  129. }
  130. }
  131. }
  132. function getCurrentProperties(tabId, options) {
  133. if (options.autoSave) {
  134. return getProperties(options);
  135. } else {
  136. const allTabsData = singlefile.extension.core.bg.tabsData.getTemporary(tabId);
  137. delete allTabsData[tabId].button;
  138. return getProperties(options);
  139. }
  140. }
  141. function getProperties(options, text = "", color = DEFAULT_COLOR, title = BUTTON_DEFAULT_TOOLTIP_MESSAGE, path = DEFAULT_ICON_PATH) {
  142. return options.autoSave ?
  143. {
  144. setBadgeBackgroundColor: { color: AUTOSAVE_COLOR },
  145. setBadgeText: { BUTTON_AUTOSAVE_ACTIVE_BADGE_MESSAGE },
  146. setTitle: { title: BUTTON_AUTOSAVE_ACTIVE_TOOLTIP_MESSAGE },
  147. setIcon: { path: DEFAULT_ICON_PATH }
  148. } :
  149. {
  150. setBadgeBackgroundColor: { color },
  151. setBadgeText: { text },
  152. setTitle: { title },
  153. setIcon: { path }
  154. };
  155. }
  156. async function refresh(tabId, tabData) {
  157. const allTabsData = singlefile.extension.core.bg.tabsData.getTemporary(tabId);
  158. const oldTabData = allTabsData[tabId].button || {};
  159. allTabsData[tabId].button = tabData;
  160. if (!tabData.pendingRefresh) {
  161. tabData.pendingRefresh = Promise.resolve();
  162. }
  163. try {
  164. await tabData.pendingRefresh;
  165. } catch (error) {
  166. /* ignored */
  167. }
  168. tabData.pendingRefresh = refreshAsync(tabId, tabData, oldTabData);
  169. }
  170. async function refreshAsync(tabId, tabData, oldTabData) {
  171. for (const browserActionMethod of Object.keys(tabData)) {
  172. if (browserActionMethod != "pendingRefresh" && (!oldTabData[browserActionMethod] || JSON.stringify(oldTabData[browserActionMethod]) != JSON.stringify(tabData[browserActionMethod]))) {
  173. try {
  174. await refreshProperty(tabId, browserActionMethod, tabData[browserActionMethod]);
  175. } catch (error) {
  176. /* ignored */
  177. }
  178. }
  179. }
  180. }
  181. async function refreshProperty(tabId, browserActionMethod, browserActionParameter) {
  182. if (browser.browserAction[browserActionMethod]) {
  183. const parameter = JSON.parse(JSON.stringify(browserActionParameter));
  184. parameter.tabId = tabId;
  185. await browser.browserAction[browserActionMethod](parameter);
  186. }
  187. }
  188. })();