ui.js 7.0 KB

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