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