ui-button.js 7.1 KB

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