ui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 = (() => {
  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 = "Process this page with SingleFile";
  25. const DEFAULT_COLOR = [2, 147, 20, 255];
  26. const BADGE_PROPERTIES = [{ name: "text", browserActionMethod: "setBadgeText" }, { name: "color", browserActionMethod: "setBadgeBackgroundColor" }, { name: "title", browserActionMethod: "setTitle" }, { name: "path", browserActionMethod: "setIcon" }];
  27. const STORE_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  28. const MENU_ID_SAVE_PAGE = "save-page";
  29. const MENU_ID_SAVE_SELECTED = "save-selected";
  30. const tabs = {};
  31. const badgeTabs = {};
  32. let badgeRefreshPending = [];
  33. browser.runtime.onInstalled.addListener(refreshContextMenu);
  34. browser.menus.onClicked.addListener((event, tab) => {
  35. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  36. processTab(tab);
  37. }
  38. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  39. processTab(tab, { selected: true });
  40. }
  41. });
  42. browser.browserAction.onClicked.addListener(async tab => {
  43. if (isAllowedURL(tab.url)) {
  44. const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
  45. if (!tabs.length) {
  46. processTab(tab);
  47. } else {
  48. tabs.forEach(processTab);
  49. }
  50. }
  51. });
  52. browser.tabs.onActivated.addListener(async activeInfo => {
  53. const tab = browser.tabs.get(activeInfo.tabId);
  54. onTabActivated(tab.id, isAllowedURL(tab.url));
  55. });
  56. browser.tabs.onCreated.addListener(tab => onTabActivated(tab.id, isAllowedURL(tab.url)));
  57. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabActivated(tab.id, isAllowedURL(tab.url)));
  58. browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
  59. browser.runtime.onMessage.addListener((request, sender) => {
  60. if (request.processProgress && request.maxIndex) {
  61. onTabProgress(sender.tab.id, request.index, request.maxIndex);
  62. }
  63. if (request.processEnd) {
  64. onTabEnd(sender.tab.id);
  65. }
  66. if (request.processError) {
  67. if (request.error) {
  68. console.error("Initialization error", request.error); // eslint-disable-line no-console
  69. }
  70. onTabError(sender.tab.id);
  71. }
  72. });
  73. return { update: refreshContextMenu };
  74. async function refreshContextMenu() {
  75. const config = await singlefile.config.get();
  76. if (config.contextMenuEnabled) {
  77. await browser.menus.removeAll();
  78. browser.menus.create({
  79. id: MENU_ID_SAVE_PAGE,
  80. contexts: ["page"],
  81. title: "Save page with SingleFile"
  82. });
  83. browser.menus.create({
  84. id: MENU_ID_SAVE_SELECTED,
  85. contexts: ["selection"],
  86. title: "Save selection"
  87. });
  88. } else {
  89. await browser.menus.removeAll();
  90. }
  91. }
  92. async function processTab(tab, options) {
  93. const tabId = tab.id;
  94. try {
  95. await singlefile.core.processTab(tab, options);
  96. tabs[tabId] = {
  97. id: tabId,
  98. text: "...",
  99. color: DEFAULT_COLOR,
  100. title: "initializing...",
  101. path: DEFAULT_ICON_PATH,
  102. progress: -1,
  103. barProgress: -1
  104. };
  105. refreshBadge(tabId);
  106. } catch (e) {
  107. tabs[tabId] = {
  108. id: tabId,
  109. text: "↻",
  110. color: [255, 141, 1, 255],
  111. title: "reload the page",
  112. path: DEFAULT_ICON_PATH,
  113. progress: -1,
  114. barProgress: -1
  115. };
  116. refreshBadge(tabId);
  117. }
  118. }
  119. function onTabError(tabId) {
  120. const tabData = tabs[tabId];
  121. tabData.text = "ERR";
  122. tabData.color = [229, 4, 12, 255];
  123. tabData.title = DEFAULT_TITLE;
  124. tabData.path = DEFAULT_ICON_PATH;
  125. tabData.progress = -1;
  126. tabData.barProgress = -1;
  127. refreshBadge(tabId);
  128. }
  129. function onTabEnd(tabId) {
  130. const tabData = tabs[tabId];
  131. tabData.text = "OK";
  132. tabData.color = [4, 229, 36, 255];
  133. tabData.title = DEFAULT_TITLE;
  134. tabData.path = DEFAULT_ICON_PATH;
  135. tabData.progress = -1;
  136. tabData.barProgress = -1;
  137. refreshBadge(tabId);
  138. }
  139. function onTabProgress(tabId, index, maxIndex) {
  140. const tabData = tabs[tabId];
  141. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  142. if (tabData.progress != progress) {
  143. tabData.progress = progress;
  144. tabData.text = "";
  145. tabData.title = "progress: " + (progress * 5) + "%";
  146. tabData.color = [4, 229, 36, 255];
  147. const barProgress = Math.floor((index / maxIndex) * 15);
  148. if (tabData.barProgress != barProgress) {
  149. tabData.barProgress = barProgress;
  150. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  151. }
  152. refreshBadge(tabId);
  153. }
  154. }
  155. function onTabRemoved(tabId) {
  156. delete tabs[tabId];
  157. }
  158. function onTabActivated(tabId, isActive) {
  159. if (isActive) {
  160. browser.browserAction.enable(tabId);
  161. } else {
  162. browser.browserAction.disable(tabId);
  163. }
  164. }
  165. function isAllowedURL(url) {
  166. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  167. }
  168. async function refreshBadge(tabId) {
  169. await Promise.all(badgeRefreshPending);
  170. const promise = refreshBadgeAsync(tabId);
  171. badgeRefreshPending.push(promise);
  172. await promise;
  173. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  174. }
  175. async function refreshBadgeAsync(tabId) {
  176. for (let property of BADGE_PROPERTIES) {
  177. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  178. }
  179. }
  180. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  181. const tabData = tabs[tabId];
  182. const value = tabData[property];
  183. if (!badgeTabs[tabId]) {
  184. badgeTabs[tabId] = {};
  185. }
  186. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  187. const browserActionParameter = { tabId };
  188. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  189. await browser.browserAction[browserActionMethod](browserActionParameter);
  190. }
  191. }
  192. })();