ui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_SELECTED_TABS = "save-selected-tabs";
  32. const MENU_ID_SAVE_UNPINNED_TABS = "save-unpinned-tabs";
  33. const MENU_ID_SAVE_ALL_TABS = "save-tabs";
  34. const MENU_ID_AUTO_SAVE_DISABLED = "auto-save-disabled";
  35. const MENU_ID_AUTO_SAVE_TAB = "auto-save-tab";
  36. const MENU_ID_AUTO_SAVE_UNPINNED = "auto-save-unpinned";
  37. const MENU_ID_AUTO_SAVE_ALL = "auto-save-all";
  38. const tabs = {};
  39. browser.runtime.onInstalled.addListener(refreshContextMenu);
  40. if (browser.menus && browser.menus.onClicked) {
  41. browser.menus.onClicked.addListener(async (event, tab) => {
  42. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  43. processTab(tab);
  44. }
  45. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  46. processTab(tab, { selected: true });
  47. }
  48. if (event.menuItemId == MENU_ID_SAVE_FRAME) {
  49. processTab(tab, { frameId: event.frameId });
  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. if (event.menuItemId == MENU_ID_SAVE_UNPINNED_TABS) {
  56. const tabs = await browser.tabs.query({ currentWindow: true, pinned: false });
  57. tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
  58. }
  59. if (event.menuItemId == MENU_ID_SAVE_ALL_TABS) {
  60. const tabs = await browser.tabs.query({ currentWindow: true });
  61. tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
  62. }
  63. if (event.menuItemId == MENU_ID_AUTO_SAVE_TAB) {
  64. if (!tabs[tab.id]) {
  65. tabs[tab.id] = {};
  66. }
  67. tabs[tab.id].autoSave = event.checked;
  68. }
  69. if (event.menuItemId == MENU_ID_AUTO_SAVE_DISABLED) {
  70. Object.keys(tabs).forEach(tabId => tabs[tabId].autoSave = false);
  71. tabs.autoSaveUnpinned = tabs.autoSaveAll = false;
  72. }
  73. if (event.menuItemId == MENU_ID_AUTO_SAVE_ALL) {
  74. tabs.autoSaveAll = event.checked;
  75. }
  76. if (event.menuItemId == MENU_ID_AUTO_SAVE_UNPINNED) {
  77. tabs.autoSaveUnpinned = event.checked;
  78. }
  79. });
  80. }
  81. browser.browserAction.onClicked.addListener(async tab => {
  82. if (isAllowedURL(tab.url)) {
  83. const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
  84. if (!tabs.length) {
  85. processTab(tab);
  86. } else {
  87. tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
  88. }
  89. }
  90. });
  91. browser.tabs.onActivated.addListener(async activeInfo => {
  92. const tab = await browser.tabs.get(activeInfo.tabId);
  93. await refreshContextMenuState(tab);
  94. onTabActivated(tab);
  95. });
  96. browser.tabs.onCreated.addListener(async tab => {
  97. await refreshContextMenuState(tab);
  98. onTabActivated(tab);
  99. });
  100. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  101. if (tabs[tab.id] && tabs[tab.id].autoSave || tabs.autoSaveAll || (tabs.autoSaveUnpinned && !tab.pinned)) {
  102. if (changeInfo.status == "complete") {
  103. processTab(tab, { autoSave: true });
  104. }
  105. }
  106. onTabActivated(tab);
  107. });
  108. browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
  109. browser.runtime.onMessage.addListener((request, sender) => {
  110. if (request.processProgress && request.maxIndex) {
  111. onTabProgress(sender.tab.id, request.index, request.maxIndex);
  112. }
  113. if (request.processEnd) {
  114. onTabEnd(sender.tab.id);
  115. }
  116. if (request.processError) {
  117. if (request.error) {
  118. console.error("Initialization error", request.error); // eslint-disable-line no-console
  119. }
  120. onTabError(sender.tab.id);
  121. }
  122. });
  123. return { update: refreshContextMenu };
  124. async function refreshContextMenu() {
  125. const config = await singlefile.config.get();
  126. if (browser.menus && browser.menus.removeAll && browser.menus.create) {
  127. if (config.contextMenuEnabled) {
  128. await browser.menus.removeAll();
  129. browser.menus.create({
  130. id: MENU_ID_SAVE_PAGE,
  131. contexts: ["page"],
  132. title: DEFAULT_TITLE
  133. });
  134. browser.menus.create({
  135. id: "separator-1",
  136. contexts: ["all"],
  137. type: "separator"
  138. });
  139. browser.menus.create({
  140. id: MENU_ID_SAVE_SELECTED,
  141. contexts: ["selection"],
  142. title: "Save selection"
  143. });
  144. browser.menus.create({
  145. id: MENU_ID_SAVE_FRAME,
  146. contexts: ["frame"],
  147. title: "Save frame"
  148. });
  149. browser.menus.create({
  150. id: MENU_ID_SAVE_SELECTED_TABS,
  151. contexts: ["page"],
  152. title: "Save selected tabs"
  153. });
  154. browser.menus.create({
  155. id: MENU_ID_SAVE_UNPINNED_TABS,
  156. contexts: ["page"],
  157. title: "Save unpinned tabs"
  158. });
  159. browser.menus.create({
  160. id: MENU_ID_SAVE_ALL_TABS,
  161. contexts: ["page"],
  162. title: "Save all tabs"
  163. });
  164. browser.menus.create({
  165. id: "separator-2",
  166. contexts: ["all"],
  167. type: "separator"
  168. });
  169. browser.menus.create({
  170. id: MENU_ID_AUTO_SAVE_DISABLED,
  171. type: "radio",
  172. title: "Disable Auto-save",
  173. contexts: ["all"],
  174. checked: true
  175. });
  176. browser.menus.create({
  177. id: MENU_ID_AUTO_SAVE_TAB,
  178. type: "radio",
  179. title: "Auto-save this tab",
  180. contexts: ["all"],
  181. checked: false
  182. });
  183. browser.menus.create({
  184. id: MENU_ID_AUTO_SAVE_UNPINNED,
  185. type: "radio",
  186. title: "Auto-save unpinned tabs",
  187. contexts: ["all"],
  188. checked: false
  189. });
  190. browser.menus.create({
  191. id: MENU_ID_AUTO_SAVE_ALL,
  192. type: "radio",
  193. title: "Auto-save all tabs",
  194. contexts: ["all"],
  195. checked: false
  196. });
  197. } else {
  198. await browser.menus.removeAll();
  199. }
  200. }
  201. }
  202. async function refreshContextMenuState(tab) {
  203. if (browser.menus && browser.menus.update) {
  204. await browser.menus.update(MENU_ID_AUTO_SAVE_DISABLED, { checked: !tabs[tab.id] || !tabs[tab.id].autoSave });
  205. await browser.menus.update(MENU_ID_AUTO_SAVE_TAB, { checked: tabs[tab.id] && tabs[tab.id].autoSave });
  206. await browser.menus.update(MENU_ID_AUTO_SAVE_UNPINNED, { checked: tabs.autoSaveUnpinned });
  207. await browser.menus.update(MENU_ID_AUTO_SAVE_ALL, { checked: tabs.autoSaveAll });
  208. }
  209. }
  210. async function processTab(tab, options) {
  211. const tabId = tab.id;
  212. try {
  213. refreshBadge(tabId, {
  214. id: tabId,
  215. text: "...",
  216. color: DEFAULT_COLOR,
  217. title: "Initializing SingleFile (1/2)",
  218. path: DEFAULT_ICON_PATH,
  219. progress: -1,
  220. barProgress: -1
  221. });
  222. await singlefile.core.processTab(tab, options);
  223. refreshBadge(tabId, {
  224. id: tabId,
  225. text: "...",
  226. color: [4, 229, 36, 255],
  227. title: "Initializing SingleFile (2/2)",
  228. path: DEFAULT_ICON_PATH,
  229. progress: -1,
  230. barProgress: -1
  231. });
  232. } catch (error) {
  233. if (error) {
  234. onTabError(tabId);
  235. } else {
  236. refreshBadge(tabId, {
  237. id: tabId,
  238. text: "↻",
  239. color: [255, 141, 1, 255],
  240. title: "Reload the page",
  241. path: DEFAULT_ICON_PATH,
  242. progress: -1,
  243. barProgress: -1
  244. });
  245. }
  246. }
  247. }
  248. function onTabError(tabId) {
  249. refreshBadge(tabId, {
  250. text: "ERR",
  251. color: [229, 4, 12, 255],
  252. title: DEFAULT_TITLE,
  253. path: DEFAULT_ICON_PATH,
  254. progress: -1,
  255. barProgress: -1
  256. });
  257. }
  258. function onTabEnd(tabId) {
  259. refreshBadge(tabId, {
  260. text: "OK",
  261. color: tabs.autoSaveAll || tabs.autoSaveUnpinned || tabs[tabId].autoSave ? [255, 141, 1, 255] : [4, 229, 36, 255],
  262. title: DEFAULT_TITLE,
  263. path: DEFAULT_ICON_PATH,
  264. progress: -1,
  265. barProgress: -1
  266. });
  267. }
  268. function onTabProgress(tabId, index, maxIndex) {
  269. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  270. const barProgress = Math.floor((index / maxIndex) * 8);
  271. refreshBadge(tabId, {
  272. progress,
  273. text: "",
  274. title: "Save progress: " + (progress * 5) + "%",
  275. color: [4, 229, 36, 255],
  276. barProgress,
  277. path: WAIT_ICON_PATH_PREFIX + barProgress + ".png"
  278. });
  279. }
  280. function onTabRemoved(tabId) {
  281. delete tabs[tabId];
  282. }
  283. function onTabActivated(tab) {
  284. if (isAllowedURL(tab.url) && browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
  285. if (isAllowedURL(tab.url)) {
  286. browser.browserAction.enable(tab.id);
  287. if (browser.runtime.lastError) {
  288. /* ignored */
  289. }
  290. } else {
  291. browser.browserAction.disable(tab.id);
  292. if (browser.runtime.lastError) {
  293. /* ignored */
  294. }
  295. }
  296. }
  297. }
  298. function isAllowedURL(url) {
  299. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !FORBIDDEN_URLS.find(storeUrl => url.startsWith(storeUrl));
  300. }
  301. async function refreshBadge(tabId, tabData) {
  302. if (!tabs[tabId]) {
  303. tabs[tabId] = {};
  304. }
  305. if (!tabs[tabId].pendingRefresh) {
  306. tabs[tabId].pendingRefresh = Promise.resolve();
  307. }
  308. tabs[tabId].pendingRefresh = tabs[tabId].pendingRefresh.then(() => refreshBadgeAsync(tabId, tabData));
  309. await tabs[tabId].pendingRefresh;
  310. }
  311. async function refreshBadgeAsync(tabId, tabData, lastTabData) {
  312. for (let property of BADGE_PROPERTIES) {
  313. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod, tabData, lastTabData);
  314. }
  315. }
  316. async function refreshBadgeProperty(tabId, property, browserActionMethod, tabData) {
  317. const value = tabData[property];
  318. const browserActionParameter = { tabId };
  319. if (browser.browserAction && browser.browserAction[browserActionMethod]) {
  320. browserActionParameter[property] = value;
  321. if (!tabs[tabId]) {
  322. tabs[tabId] = {};
  323. }
  324. if (!tabs[tabId].badge) {
  325. tabs[tabId].badge = {};
  326. }
  327. if (JSON.stringify(tabs[tabId].badge[browserActionMethod]) != JSON.stringify(value)) {
  328. tabs[tabId].badge[browserActionMethod] = value;
  329. await browser.browserAction[browserActionMethod](browserActionParameter);
  330. }
  331. }
  332. }
  333. })();