ui.js 7.9 KB

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