ui-button.js 7.0 KB

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