ui-button.js 6.9 KB

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