ui-button.js 7.5 KB

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