ui.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 = "Save 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: DEFAULT_TITLE
  89. });
  90. browser.menus.create({
  91. id: MENU_ID_SAVE_SELECTED,
  92. contexts: ["selection"],
  93. title: "Save selection with SingleFile"
  94. });
  95. browser.menus.create({
  96. id: MENU_ID_SAVE_FRAME,
  97. contexts: ["frame"],
  98. title: "Save frame with SingleFile"
  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. onTabError(tabId);
  122. } else {
  123. tabs[tabId] = {
  124. id: tabId,
  125. text: "↻",
  126. color: [255, 141, 1, 255],
  127. title: "Reload the page",
  128. path: DEFAULT_ICON_PATH,
  129. progress: -1,
  130. barProgress: -1
  131. };
  132. refreshBadge(tabId);
  133. }
  134. }
  135. }
  136. function onTabError(tabId) {
  137. const tabData = tabs[tabId];
  138. tabData.text = "ERR";
  139. tabData.color = [229, 4, 12, 255];
  140. tabData.title = DEFAULT_TITLE;
  141. tabData.path = DEFAULT_ICON_PATH;
  142. tabData.progress = -1;
  143. tabData.barProgress = -1;
  144. refreshBadge(tabId);
  145. }
  146. function onTabEnd(tabId) {
  147. const tabData = tabs[tabId];
  148. tabData.text = "OK";
  149. tabData.color = [4, 229, 36, 255];
  150. tabData.title = DEFAULT_TITLE;
  151. tabData.path = DEFAULT_ICON_PATH;
  152. tabData.progress = -1;
  153. tabData.barProgress = -1;
  154. refreshBadge(tabId);
  155. }
  156. function onTabProgress(tabId, index, maxIndex) {
  157. const tabData = tabs[tabId];
  158. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  159. if (tabData.progress != progress) {
  160. tabData.progress = progress;
  161. tabData.text = "";
  162. tabData.title = "Save progress: " + (progress * 5) + "%";
  163. tabData.color = [4, 229, 36, 255];
  164. const barProgress = Math.floor((index / maxIndex) * 8);
  165. if (tabData.barProgress != barProgress) {
  166. tabData.barProgress = barProgress;
  167. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  168. }
  169. refreshBadge(tabId);
  170. }
  171. }
  172. function onTabRemoved(tabId) {
  173. delete tabs[tabId];
  174. }
  175. function onTabActivated(tabId, isActive) {
  176. if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
  177. if (isActive) {
  178. browser.browserAction.enable(tabId);
  179. } else {
  180. browser.browserAction.disable(tabId);
  181. }
  182. }
  183. }
  184. function isAllowedURL(url) {
  185. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  186. }
  187. async function refreshBadge(tabId) {
  188. await Promise.all(badgeRefreshPending);
  189. const promise = refreshBadgeAsync(tabId);
  190. badgeRefreshPending.push(promise);
  191. await promise;
  192. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  193. }
  194. async function refreshBadgeAsync(tabId) {
  195. for (let property of BADGE_PROPERTIES) {
  196. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  197. }
  198. }
  199. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  200. const tabData = tabs[tabId];
  201. const value = tabData[property];
  202. if (!badgeTabs[tabId]) {
  203. badgeTabs[tabId] = {};
  204. }
  205. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  206. const browserActionParameter = { tabId };
  207. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  208. if (browser.browserAction && browser.browserAction[browserActionMethod]) {
  209. await browser.browserAction[browserActionMethod](browserActionParameter);
  210. }
  211. }
  212. }
  213. })();