ui-button.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. onTabActivated,
  51. onTabUpdated,
  52. onInitialize,
  53. onProgress,
  54. onEnd,
  55. onForbiddenDomain,
  56. onError,
  57. refreshTab,
  58. refresh: async (tab, options) => {
  59. if (tab.id) {
  60. await refresh(tab.id, getProperties(options));
  61. }
  62. }
  63. };
  64. function onMessage(message, sender) {
  65. if (message.method.endsWith(".loadURL")) {
  66. onLoad(sender.tab.id);
  67. }
  68. if (message.method.endsWith(".processProgress")) {
  69. if (message.maxIndex) {
  70. onProgress(sender.tab.id, message.index, message.maxIndex, message.options);
  71. }
  72. }
  73. if (message.method.endsWith(".processEnd")) {
  74. onEnd(sender.tab.id, message.options);
  75. }
  76. if (message.method.endsWith(".processError")) {
  77. if (message.error) {
  78. console.error("Initialization error", message.error); // eslint-disable-line no-console
  79. }
  80. onError(sender.tab.id, message.options);
  81. }
  82. if (message.method.endsWith(".processCancelled")) {
  83. onCancelled(sender.tab.id, message.options);
  84. }
  85. return Promise.resolve({});
  86. }
  87. function onTabUpdated(tabId, changeInfo, tab) {
  88. refreshTab(tab);
  89. }
  90. async function onTabCreated(tab) {
  91. refreshTab(tab);
  92. }
  93. async function onTabActivated(tab) {
  94. refreshTab(tab);
  95. }
  96. function onLoad(tabId) {
  97. refresh(tabId, getProperties({}, "", DEFAULT_COLOR, BUTTON_DEFAULT_TOOLTIP_MESSAGE));
  98. }
  99. function onInitialize(tabId, options, step) {
  100. 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"));
  101. }
  102. function onError(tabId, options) {
  103. refresh(tabId, getProperties(options, BUTTON_ERROR_BADGE_MESSAGE, [229, 4, 12, 192]));
  104. }
  105. function onForbiddenDomain(tab, options) {
  106. if (singlefile.extension.core.bg.util.isAllowedProtocol(tab.url)) {
  107. refresh(tab.id, getProperties(options, BUTTON_BLOCKED_BADGE_MESSAGE, [255, 255, 255, 1], BUTTON_BLOCKED_TOOLTIP_MESSAGE));
  108. }
  109. }
  110. function onCancelled(tabId, options) {
  111. refresh(tabId, getProperties(options, "", DEFAULT_COLOR, BUTTON_DEFAULT_TOOLTIP_MESSAGE));
  112. }
  113. function onEnd(tabId, options) {
  114. refresh(tabId, getProperties(options, BUTTON_OK_BADGE_MESSAGE, [4, 229, 36, 192]));
  115. }
  116. function onProgress(tabId, index, maxIndex, options) {
  117. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  118. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  119. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  120. refresh(tabId, getProperties(options, "", [4, 229, 36, 192], BUTTON_SAVE_PROGRESS_TOOLTIP_MESSAGE + (progress * 5) + "%", path, [128, 128, 128, 192]));
  121. }
  122. async function refreshTab(tab) {
  123. const options = { autoSave: await singlefile.extension.core.bg.autosave.isEnabled(tab) };
  124. const properties = getCurrentProperties(tab.id, options);
  125. if (singlefile.extension.core.bg.util.isAllowedURL(tab.url)) {
  126. await refresh(tab.id, properties);
  127. } else {
  128. try {
  129. await onForbiddenDomain(tab, options);
  130. } catch (error) {
  131. /* ignored */
  132. }
  133. }
  134. }
  135. function getCurrentProperties(tabId, options) {
  136. if (options.autoSave) {
  137. return getProperties(options);
  138. } else {
  139. const allTabsData = singlefile.extension.core.bg.tabsData.getTemporary(tabId);
  140. delete allTabsData[tabId].button;
  141. return getProperties(options);
  142. }
  143. }
  144. function getProperties(options, text, color, title = BUTTON_DEFAULT_TOOLTIP_MESSAGE, path = DEFAULT_ICON_PATH, autoColor = [208, 208, 208, 192]) {
  145. return {
  146. setBadgeBackgroundColor: { color: options.autoSave ? autoColor : color || DEFAULT_COLOR },
  147. setBadgeText: { text: options.autoSave ? BUTTON_AUTOSAVE_ACTIVE_BADGE_MESSAGE : (text || "") },
  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 allTabsData = singlefile.extension.core.bg.tabsData.getTemporary(tabId);
  154. const oldTabData = allTabsData[tabId].button || {};
  155. allTabsData[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 != "pendingRefresh" && (!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. })();