ui-button.js 7.4 KB

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