ui.js 12 KB

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