ui-button.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 => {
  70. if (tabId) {
  71. const tabsData = await singlefile.tabsData.get();
  72. await refresh(tabId, getProperties({ autoSave: tabsData.autoSaveAll || tabsData.autoSaveUnpinned || (tabsData[tabId] && tabsData[tabId].autoSave) }));
  73. }
  74. }
  75. };
  76. function onReset(tabId) {
  77. refresh(tabId, getProperties({}, "", DEFAULT_COLOR, DEFAULT_TITLE));
  78. }
  79. function onInitialize(tabId, options, step) {
  80. if (step == 1) {
  81. onReset(tabId);
  82. }
  83. 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"));
  84. }
  85. function onError(tabId, options) {
  86. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonErrorBadge"), [229, 4, 12, 255]));
  87. }
  88. function onCancelled(tabId, options) {
  89. refresh(tabId, getProperties(options, "", DEFAULT_COLOR, DEFAULT_TITLE));
  90. }
  91. function onEnd(tabId, options) {
  92. refresh(tabId, getProperties(options, browser.i18n.getMessage("buttonOKBadge"), [4, 229, 36, 255]));
  93. }
  94. function onProgress(tabId, index, maxIndex, options) {
  95. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  96. const barProgress = Math.min(Math.floor((index / maxIndex) * 8), 8);
  97. const path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  98. refresh(tabId, getProperties(options, "", [4, 229, 36, 255], browser.i18n.getMessage("buttonSaveProgressTooltip") + (progress * 5) + "%", path, [128, 128, 128, 255]));
  99. }
  100. async function onTabActivated(tab) {
  101. const autoSave = await singlefile.autosave.enabled(tab.id);
  102. const properties = getCurrentProperties(tab.id, { autoSave });
  103. await refresh(tab.id, properties, true);
  104. if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
  105. if (singlefile.ui.isAllowedURL(tab.url)) {
  106. try {
  107. await browser.browserAction.enable(tab.id);
  108. } catch (error) {
  109. /* ignored */
  110. }
  111. } else {
  112. try {
  113. await browser.browserAction.disable(tab.id);
  114. } catch (error) {
  115. /* ignored */
  116. }
  117. }
  118. }
  119. }
  120. function getCurrentProperties(tabId, options) {
  121. if (options.autoSave) {
  122. return getProperties(options);
  123. } else {
  124. const tabsData = singlefile.tabsData.getTemporary();
  125. const tabData = tabsData[tabId] && tabsData[tabId].button;
  126. if (tabData) {
  127. return tabData;
  128. } else {
  129. return getProperties(options);
  130. }
  131. }
  132. }
  133. function getProperties(options, text, color, title = DEFAULT_TITLE, path = DEFAULT_ICON_PATH, autoColor = [208, 208, 208, 255]) {
  134. return {
  135. setBadgeText: { text: options.autoSave ? browser.i18n.getMessage("buttonAutoSaveActiveBadge") : (text || "") },
  136. setBadgeBackgroundColor: { color: options.autoSave ? autoColor : color || DEFAULT_COLOR },
  137. setTitle: { title: options.autoSave ? browser.i18n.getMessage("buttonAutoSaveActiveTooltip") : title },
  138. setIcon: { path: options.autoSave ? DEFAULT_ICON_PATH : path }
  139. };
  140. }
  141. async function refresh(tabId, tabData) {
  142. const tabsData = singlefile.tabsData.getTemporary();
  143. if (!tabsData[tabId]) {
  144. tabsData[tabId] = {};
  145. }
  146. const oldTabData = tabsData[tabId].button || {};
  147. tabsData[tabId].button = tabData;
  148. if (!tabData.pendingRefresh) {
  149. tabData.pendingRefresh = Promise.resolve();
  150. }
  151. try {
  152. await tabData.pendingRefresh;
  153. } catch (error) {
  154. /* ignored */
  155. }
  156. tabData.pendingRefresh = refreshAsync(tabId, tabData, oldTabData);
  157. }
  158. async function refreshAsync(tabId, tabData, oldTabData) {
  159. for (const browserActionMethod of Object.keys(tabData)) {
  160. if (!oldTabData[browserActionMethod] || JSON.stringify(oldTabData[browserActionMethod]) != JSON.stringify(tabData[browserActionMethod])) {
  161. await refreshProperty(tabId, browserActionMethod, tabData[browserActionMethod]);
  162. }
  163. }
  164. }
  165. async function refreshProperty(tabId, browserActionMethod, browserActionParameter) {
  166. if (browser.browserAction[browserActionMethod]) {
  167. const parameter = JSON.parse(JSON.stringify(browserActionParameter));
  168. parameter.tabId = tabId;
  169. await browser.browserAction[browserActionMethod](parameter);
  170. }
  171. }
  172. })();