ui-button.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_16.png";
  23. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_16_wait";
  24. const DEFAULT_TITLE = browser.i18n.getMessage("buttonDefaultTooltip");
  25. const DEFAULT_COLOR = [2, 147, 20, 255];
  26. browser.browserAction.onClicked.addListener(async tab => {
  27. if (singlefile.ui.isAllowedURL(tab.url)) {
  28. const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
  29. if (!tabs.length) {
  30. singlefile.ui.saveTab(tab);
  31. } else {
  32. tabs.forEach(tab => (tab.active || tab.highlighted) && singlefile.ui.isAllowedURL(tab.url) && singlefile.ui.saveTab(tab));
  33. }
  34. }
  35. });
  36. browser.tabs.onActivated.addListener(async activeInfo => {
  37. const tab = await browser.tabs.get(activeInfo.tabId);
  38. await onTabActivated(tab);
  39. });
  40. browser.tabs.onCreated.addListener(async tab => {
  41. await refreshProperty(tab.id, "setBadgeBackgroundColor", { color: DEFAULT_COLOR });
  42. await onTabActivated(tab);
  43. });
  44. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabActivated(tab));
  45. browser.runtime.onMessage.addListener((request, sender) => {
  46. if (request.processReset) {
  47. onReset(sender.tab.id);
  48. }
  49. if (request.processProgress) {
  50. if (request.maxIndex) {
  51. onProgress(sender.tab.id, request.index, request.maxIndex, request.options);
  52. }
  53. }
  54. if (request.processEnd) {
  55. onEnd(sender.tab.id, request.options);
  56. }
  57. if (request.processError) {
  58. if (request.error) {
  59. console.error("Initialization error", request.error); // eslint-disable-line no-console
  60. }
  61. onError(sender.tab.id, request.options);
  62. }
  63. if (request.processCancelled) {
  64. onCancelled(sender.tab.id, request.options);
  65. }
  66. });
  67. return {
  68. onInitialize,
  69. onProgress,
  70. onEnd,
  71. onError,
  72. refresh: async tabId => {
  73. if (tabId) {
  74. const tabsData = await singlefile.tabsData.get();
  75. await refresh(tabId, getProperties({ autoSave: tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tabId] && tabsData[tabId].autoSave) }));
  76. }
  77. }
  78. };
  79. function onReset(tabId) {
  80. refresh(tabId, getProperties({}, "", DEFAULT_COLOR, DEFAULT_TITLE));
  81. }
  82. function onInitialize(tabId, options, step) {
  83. if (step == 1) {
  84. onReset(tabId);
  85. }
  86. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonInitializingBadge"), step == 1 ? DEFAULT_COLOR : [4, 229, 36, 255], browser.i18n.getMessage("buttonInitializingTooltip") + " (" + step + "/2)", WAIT_ICON_PATH_PREFIX + "0.png"));
  87. }
  88. function onError(tabId, options) {
  89. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonErrorBadge"), [229, 4, 12, 255]));
  90. }
  91. function onCancelled(tabId, options) {
  92. refresh(tabId, getProperties(options, "", DEFAULT_COLOR, DEFAULT_TITLE));
  93. }
  94. function onEnd(tabId, options) {
  95. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonOKBadge"), [4, 229, 36, 255]));
  96. }
  97. function onProgress(tabId, index, maxIndex, options) {
  98. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  99. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  100. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  101. refresh(tabId, getProperties(options, "", [4, 229, 36, 255], browser.i18n.getMessage("buttonSaveProgressTooltip") + (progress * 5) + "%", path, [128, 128, 128, 255]));
  102. }
  103. async function onTabActivated(tab) {
  104. const autoSave = await singlefile.autosave.enabled(tab.id);
  105. const properties = getCurrentProperties(tab.id, { autoSave });
  106. await refresh(tab.id, properties, true);
  107. if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
  108. if (singlefile.ui.isAllowedURL(tab.url)) {
  109. try {
  110. await browser.browserAction.enable(tab.id);
  111. } catch (error) {
  112. /* ignored */
  113. }
  114. } else {
  115. try {
  116. await browser.browserAction.disable(tab.id);
  117. } catch (error) {
  118. /* ignored */
  119. }
  120. }
  121. }
  122. }
  123. function getCurrentProperties(tabId, options) {
  124. if (options.autoSave) {
  125. return getProperties(options);
  126. } else {
  127. const tabsData = singlefile.tabsData.getTemporary();
  128. const tabData = tabsData[tabId] && tabsData[tabId].button;
  129. if (tabData) {
  130. return tabData;
  131. } else {
  132. return getProperties(options);
  133. }
  134. }
  135. }
  136. function getProperties(options, text, color, title = DEFAULT_TITLE, path = DEFAULT_ICON_PATH, autoColor = [208, 208, 208, 255]) {
  137. return {
  138. setBadgeText: { text: options.autoSave ? browser.i18n.getMessage("buttonAutoSaveActiveBadge") : (text || "") },
  139. setBadgeBackgroundColor: { color: options.autoSave ? autoColor : color || DEFAULT_COLOR },
  140. setTitle: { title: options.autoSave ? browser.i18n.getMessage("buttonAutoSaveActiveTooltip") : title },
  141. setIcon: { path: options.autoSave ? DEFAULT_ICON_PATH : path }
  142. };
  143. }
  144. async function refresh(tabId, tabData) {
  145. const tabsData = singlefile.tabsData.getTemporary();
  146. if (!tabsData[tabId]) {
  147. tabsData[tabId] = {};
  148. }
  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 == "setBadgeBackgroundColor" || !oldTabData[browserActionMethod] || JSON.stringify(oldTabData[browserActionMethod]) != JSON.stringify(tabData[browserActionMethod])) {
  164. await refreshProperty(tabId, browserActionMethod, tabData[browserActionMethod]);
  165. }
  166. }
  167. }
  168. async function refreshProperty(tabId, browserActionMethod, browserActionParameter) {
  169. if (browser.browserAction[browserActionMethod]) {
  170. const parameter = JSON.parse(JSON.stringify(browserActionParameter));
  171. parameter.tabId = tabId;
  172. await browser.browserAction[browserActionMethod](parameter);
  173. }
  174. }
  175. })();