config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.config = (() => {
  22. const DEFAULT_PROFILE_NAME = "__Default_Settings__";
  23. const DISABLED_PROFILE_NAME = "__Disabled_Settings__";
  24. const DEFAULT_CONFIG = {
  25. removeHiddenElements: true,
  26. removeUnusedStyles: true,
  27. removeFrames: false,
  28. removeImports: true,
  29. removeScripts: true,
  30. rawDocument: false,
  31. compressHTML: true,
  32. compressCSS: true,
  33. lazyLoadImages: true,
  34. maxLazyLoadImagesIdleTime: 1500,
  35. filenameTemplate: "{page-title} ({date-iso} {time-locale}).html",
  36. infobarTemplate: "",
  37. confirmInfobar: false,
  38. confirmFilename: false,
  39. conflictAction: "uniquify",
  40. contextMenuEnabled: true,
  41. shadowEnabled: true,
  42. maxResourceSizeEnabled: false,
  43. maxResourceSize: 10,
  44. removeAudioSrc: true,
  45. removeVideoSrc: true,
  46. displayInfobar: true,
  47. displayStats: false,
  48. backgroundSave: true,
  49. autoSaveDelay: 1,
  50. autoSaveLoad: false,
  51. autoSaveUnload: false,
  52. autoSaveLoadOrUnload: true,
  53. removeAlternativeFonts: true,
  54. removeAlternativeMedias: true,
  55. removeAlternativeImages: true,
  56. groupDuplicateImages: true,
  57. saveRawPage: false
  58. };
  59. let pendingUpgradePromise = upgrade();
  60. browser.runtime.onMessage.addListener(request => {
  61. if (request.getOptions) {
  62. return getOptions();
  63. }
  64. });
  65. async function upgrade() {
  66. const config = await browser.storage.local.get();
  67. const defaultConfig = config;
  68. if (!config.profiles) {
  69. delete defaultConfig.tabsData;
  70. applyUpgrade(defaultConfig);
  71. const config = { profiles: {}, rules: [] };
  72. config.profiles[DEFAULT_PROFILE_NAME] = defaultConfig;
  73. browser.storage.local.remove(Object.keys(DEFAULT_CONFIG));
  74. return browser.storage.local.set(config);
  75. } else {
  76. if (!config.rules) {
  77. config.rules = [];
  78. }
  79. Object.keys(config.profiles).forEach(profileName => applyUpgrade(config.profiles[profileName]));
  80. await browser.storage.local.remove(["profiles", "defaultProfile", "rules"]);
  81. return browser.storage.local.set({ profiles: config.profiles, rules: config.rules });
  82. }
  83. }
  84. function applyUpgrade(config) {
  85. if (config.removeScripts === undefined) {
  86. config.removeScripts = true;
  87. }
  88. config.compressHTML = config.compressCSS = config.compress;
  89. if (config.compressCSS === undefined) {
  90. config.compressCSS = true;
  91. }
  92. if (config.compressHTML === undefined) {
  93. config.compressHTML = true;
  94. }
  95. if (config.lazyLoadImages === undefined) {
  96. config.lazyLoadImages = true;
  97. }
  98. if (config.contextMenuEnabled === undefined) {
  99. config.contextMenuEnabled = true;
  100. }
  101. if (config.filenameTemplate === undefined) {
  102. if (config.appendSaveDate || config.appendSaveDate === undefined) {
  103. config.filenameTemplate = DEFAULT_CONFIG.filenameTemplate;
  104. } else {
  105. config.filenameTemplate = "{page-title}.html";
  106. }
  107. delete config.appendSaveDate;
  108. }
  109. if (config.infobarTemplate === undefined) {
  110. config.infobarTemplate = "";
  111. }
  112. if (config.removeImports === undefined) {
  113. config.removeImports = true;
  114. }
  115. if (config.shadowEnabled === undefined) {
  116. config.shadowEnabled = true;
  117. }
  118. if (config.maxResourceSize === undefined) {
  119. config.maxResourceSize = DEFAULT_CONFIG.maxResourceSize;
  120. }
  121. if (config.maxResourceSize === 0) {
  122. config.maxResourceSize = 1;
  123. }
  124. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  125. delete config.removeUnusedCSSRules;
  126. config.removeUnusedStyles = true;
  127. }
  128. if (config.removeAudioSrc === undefined) {
  129. config.removeAudioSrc = true;
  130. }
  131. if (config.removeVideoSrc === undefined) {
  132. config.removeVideoSrc = true;
  133. }
  134. if (config.displayInfobar === undefined) {
  135. config.displayInfobar = true;
  136. }
  137. if (config.backgroundSave === undefined) {
  138. config.backgroundSave = true;
  139. }
  140. if (config.autoSaveDelay === undefined) {
  141. config.autoSaveDelay = DEFAULT_CONFIG.autoSaveDelay;
  142. }
  143. if (config.removeAlternativeFonts === undefined) {
  144. config.removeAlternativeFonts = true;
  145. }
  146. if (config.removeAlternativeMedias === undefined) {
  147. config.removeAlternativeMedias = true;
  148. }
  149. if (config.removeAlternativeImages === undefined) {
  150. if (config.removeAlternativeImages === undefined) {
  151. config.removeAlternativeImages = true;
  152. } else {
  153. config.removeAlternativeImages = config.removeSrcSet;
  154. }
  155. }
  156. if (config.groupDuplicateImages === undefined) {
  157. config.groupDuplicateImages = true;
  158. }
  159. if (config.removeHiddenElements === undefined) {
  160. config.removeHiddenElements = true;
  161. }
  162. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  163. config.autoSaveLoadOrUnload = true;
  164. config.autoSaveLoad = false;
  165. config.autoSaveUnload = false;
  166. }
  167. if (config.maxLazyLoadImagesIdleTime === undefined) {
  168. config.maxLazyLoadImagesIdleTime = DEFAULT_CONFIG.maxLazyLoadImagesIdleTime;
  169. }
  170. if (config.confirmFilename === undefined) {
  171. config.confirmFilename = false;
  172. }
  173. if (config.conflictAction === undefined) {
  174. config.conflictAction = DEFAULT_CONFIG.conflictAction;
  175. }
  176. }
  177. async function getOptions() {
  178. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  179. return config.profiles[tabsData.profileName || DEFAULT_PROFILE_NAME];
  180. }
  181. async function getConfig() {
  182. await pendingUpgradePromise;
  183. return browser.storage.local.get(["profiles", "rules"]);
  184. }
  185. return {
  186. DISABLED_PROFILE_NAME,
  187. DEFAULT_PROFILE_NAME,
  188. async createProfile(profileName) {
  189. const config = await getConfig();
  190. if (Object.keys(config.profiles).includes(profileName)) {
  191. throw new Error("Duplicate profile name");
  192. }
  193. config.profiles[profileName] = DEFAULT_CONFIG;
  194. await browser.storage.local.set({ profiles: config.profiles });
  195. },
  196. async getProfiles() {
  197. const config = await getConfig();
  198. return config.profiles;
  199. },
  200. async getOptions(profileName, url, autoSave) {
  201. const config = await getConfig();
  202. const urlRule = config.rules.find(rule => url && url.includes(rule.url));
  203. return urlRule ? config.profiles[urlRule[autoSave ? "autoSaveProfile" : "profile"]] : config.profiles[profileName || singlefile.config.DEFAULT_PROFILE_NAME];
  204. },
  205. async updateProfile(profileName, profile) {
  206. const config = await getConfig();
  207. if (!Object.keys(config.profiles).includes(profileName)) {
  208. throw new Error("Profile not found");
  209. }
  210. config.profiles[profileName] = profile;
  211. await browser.storage.local.set({ profiles: config.profiles });
  212. },
  213. async renameProfile(oldProfileName, profileName) {
  214. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  215. if (!Object.keys(config.profiles).includes(oldProfileName)) {
  216. throw new Error("Profile not found");
  217. }
  218. if (Object.keys(config.profiles).includes(profileName)) {
  219. throw new Error("Duplicate profile name");
  220. }
  221. if (oldProfileName == DEFAULT_PROFILE_NAME) {
  222. throw new Error("Default settings cannot be renamed");
  223. }
  224. if (tabsData.profileName == oldProfileName) {
  225. tabsData.profileName = profileName;
  226. await singlefile.tabsData.set(tabsData);
  227. }
  228. config.profiles[profileName] = config.profiles[oldProfileName];
  229. config.rules.forEach(rule => {
  230. if (rule.profile == oldProfileName) {
  231. rule.profile = profileName;
  232. }
  233. if (rule.autoSaveProfile == oldProfileName) {
  234. rule.autoSaveProfile = profileName;
  235. }
  236. });
  237. delete config.profiles[oldProfileName];
  238. await browser.storage.local.set({ profiles: config.profiles, rules: config.rules });
  239. },
  240. async deleteProfile(profileName) {
  241. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  242. if (!Object.keys(config.profiles).includes(profileName)) {
  243. throw new Error("Profile not found");
  244. }
  245. if (profileName == DEFAULT_PROFILE_NAME) {
  246. throw new Error("Default settings cannot be deleted");
  247. }
  248. if (tabsData.profileName == profileName) {
  249. delete tabsData.profileName;
  250. await singlefile.tabsData.set(tabsData);
  251. }
  252. config.rules.forEach(rule => {
  253. if (rule.profile == profileName) {
  254. rule.profile = DEFAULT_PROFILE_NAME;
  255. }
  256. if (rule.autoSaveProfile == profileName) {
  257. rule.autoSaveProfile = DEFAULT_PROFILE_NAME;
  258. }
  259. });
  260. delete config.profiles[profileName];
  261. await browser.storage.local.set({ profiles: config.profiles, rules: config.rules });
  262. },
  263. async getRules() {
  264. const config = await getConfig();
  265. return config.rules;
  266. },
  267. async addRule(url, profile, autoSaveProfile) {
  268. if (!url) {
  269. throw new Error("URL is empty");
  270. }
  271. const config = await getConfig();
  272. if (config.rules.find(rule => rule.url == url)) {
  273. throw new Error("URL already exists");
  274. }
  275. config.rules.push({
  276. url,
  277. profile,
  278. autoSaveProfile
  279. });
  280. await browser.storage.local.set({ rules: config.rules });
  281. },
  282. async deleteRule(url) {
  283. if (!url) {
  284. throw new Error("URL is empty");
  285. }
  286. const config = await getConfig();
  287. config.rules = config.rules.filter(rule => rule.url != url);
  288. await browser.storage.local.set({ rules: config.rules });
  289. },
  290. async updateRule(url, newURL, profile, autoSaveProfile) {
  291. if (!url || !newURL) {
  292. throw new Error("URL is empty");
  293. }
  294. const config = await getConfig();
  295. const urlConfig = config.rules.find(rule => rule.url == url);
  296. if (!urlConfig) {
  297. throw new Error("URL not found");
  298. }
  299. if (config.rules.find(rule => rule.url == newURL && rule.url != url)) {
  300. throw new Error("New URL already exists");
  301. }
  302. urlConfig.url = newURL;
  303. urlConfig.profile = profile;
  304. urlConfig.autoSaveProfile = autoSaveProfile;
  305. await browser.storage.local.set({ rules: config.rules });
  306. },
  307. async reset() {
  308. await pendingUpgradePromise;
  309. const tabsData = await singlefile.tabsData.get();
  310. delete tabsData.profileName;
  311. await singlefile.tabsData.set(tabsData);
  312. await browser.storage.local.remove(["profiles", "rules"]);
  313. await browser.storage.local.set({ profiles: { [DEFAULT_PROFILE_NAME]: DEFAULT_CONFIG }, rules: [] });
  314. }
  315. };
  316. })();