1
0

ui-button.js 7.0 KB

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