ui.js 6.8 KB

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