1
0

ui-button.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.runtime.onMessage.addListener((request, sender) => {
  37. if (request.processReset) {
  38. onReset(sender.tab.id);
  39. }
  40. if (request.processProgress) {
  41. if (request.maxIndex) {
  42. onProgress(sender.tab.id, request.index, request.maxIndex, request.options);
  43. }
  44. }
  45. if (request.processEnd) {
  46. onEnd(sender.tab.id, request.options);
  47. }
  48. if (request.processError) {
  49. if (request.error) {
  50. console.error("Initialization error", request.error); // eslint-disable-line no-console
  51. }
  52. onError(sender.tab.id, request.options);
  53. }
  54. if (request.processCancelled) {
  55. onCancelled(sender.tab.id, request.options);
  56. }
  57. });
  58. return {
  59. onTabCreated,
  60. onTabActivated,
  61. onTabUpdated,
  62. onInitialize,
  63. onProgress,
  64. onEnd,
  65. onError,
  66. refresh: async tabId => {
  67. if (tabId) {
  68. const tabsData = await singlefile.tabsData.get();
  69. await refresh(tabId, getProperties({ autoSave: tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tabId] && tabsData[tabId].autoSave) }));
  70. }
  71. }
  72. };
  73. function onTabUpdated(tabId, changeInfo, tab) {
  74. refreshTab(tab);
  75. }
  76. async function onTabCreated(tab) {
  77. await refreshProperty(tab.id, "setBadgeBackgroundColor", { color: DEFAULT_COLOR });
  78. refreshTab(tab);
  79. }
  80. async function onTabActivated(tab) {
  81. refreshTab(tab);
  82. }
  83. function onReset(tabId) {
  84. refresh(tabId, getProperties({}, "", DEFAULT_COLOR, DEFAULT_TITLE));
  85. }
  86. function onInitialize(tabId, options, step) {
  87. if (step == 1) {
  88. onReset(tabId);
  89. }
  90. 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"));
  91. }
  92. function onError(tabId, options) {
  93. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonErrorBadge"), [229, 4, 12, 255]));
  94. }
  95. function onCancelled(tabId, options) {
  96. refresh(tabId, getProperties(options, "", DEFAULT_COLOR, DEFAULT_TITLE));
  97. }
  98. function onEnd(tabId, options) {
  99. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonOKBadge"), [4, 229, 36, 255]));
  100. }
  101. function onProgress(tabId, index, maxIndex, options) {
  102. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  103. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  104. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  105. refresh(tabId, getProperties(options, "", [4, 229, 36, 255], browser.i18n.getMessage("buttonSaveProgressTooltip") + (progress * 5) + "%", path, [128, 128, 128, 255]));
  106. }
  107. async function refreshTab(tab) {
  108. const autoSave = await singlefile.autosave.enabled(tab.id);
  109. const properties = getCurrentProperties(tab.id, { autoSave });
  110. await refresh(tab.id, properties, true);
  111. if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
  112. if (singlefile.ui.isAllowedURL(tab.url)) {
  113. try {
  114. await browser.browserAction.enable(tab.id);
  115. } catch (error) {
  116. /* ignored */
  117. }
  118. } else {
  119. try {
  120. await browser.browserAction.disable(tab.id);
  121. } catch (error) {
  122. /* ignored */
  123. }
  124. }
  125. }
  126. }
  127. function getCurrentProperties(tabId, options) {
  128. if (options.autoSave) {
  129. return getProperties(options);
  130. } else {
  131. const tabsData = singlefile.tabsData.getTemporary();
  132. const tabData = tabsData[tabId] && tabsData[tabId].button;
  133. if (tabData) {
  134. return tabData;
  135. } else {
  136. return getProperties(options);
  137. }
  138. }
  139. }
  140. function getProperties(options, text, color, title = DEFAULT_TITLE, path = DEFAULT_ICON_PATH, autoColor = [208, 208, 208, 255]) {
  141. return {
  142. setBadgeText: { text: options.autoSave ? browser.i18n.getMessage("buttonAutoSaveActiveBadge") : (text || "") },
  143. setBadgeBackgroundColor: { color: options.autoSave ? autoColor : color || DEFAULT_COLOR },
  144. setTitle: { title: options.autoSave ? browser.i18n.getMessage("buttonAutoSaveActiveTooltip") : title },
  145. setIcon: { path: options.autoSave ? DEFAULT_ICON_PATH : path }
  146. };
  147. }
  148. async function refresh(tabId, tabData) {
  149. const tabsData = singlefile.tabsData.getTemporary();
  150. if (!tabsData[tabId]) {
  151. tabsData[tabId] = {};
  152. }
  153. const oldTabData = tabsData[tabId].button || {};
  154. tabsData[tabId].button = tabData;
  155. if (!tabData.pendingRefresh) {
  156. tabData.pendingRefresh = Promise.resolve();
  157. }
  158. try {
  159. await tabData.pendingRefresh;
  160. } catch (error) {
  161. /* ignored */
  162. }
  163. tabData.pendingRefresh = refreshAsync(tabId, tabData, oldTabData);
  164. }
  165. async function refreshAsync(tabId, tabData, oldTabData) {
  166. for (const browserActionMethod of Object.keys(tabData)) {
  167. if (browserActionMethod == "setBadgeBackgroundColor" || !oldTabData[browserActionMethod] || JSON.stringify(oldTabData[browserActionMethod]) != JSON.stringify(tabData[browserActionMethod])) {
  168. await refreshProperty(tabId, browserActionMethod, tabData[browserActionMethod]);
  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. })();