ui.js 13 KB

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