ui.js 7.9 KB

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