ui-button.js 6.3 KB

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