ui-button.js 7.3 KB

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