config.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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, URL, Blob, FileReader */
  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. loadDeferredImages: true,
  34. loadDeferredImagesMaxIdleTime: 1500,
  35. filenameTemplate: "{page-title} ({date-iso} {time-locale}).html",
  36. infobarTemplate: "",
  37. confirmInfobarContent: false,
  38. confirmFilename: false,
  39. filenameConflictAction: "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.contextMenuEnabled === undefined) {
  96. config.contextMenuEnabled = true;
  97. }
  98. if (config.filenameTemplate === undefined) {
  99. if (config.appendSaveDate || config.appendSaveDate === undefined) {
  100. config.filenameTemplate = DEFAULT_CONFIG.filenameTemplate;
  101. } else {
  102. config.filenameTemplate = "{page-title}.html";
  103. }
  104. delete config.appendSaveDate;
  105. }
  106. if (config.infobarTemplate === undefined) {
  107. config.infobarTemplate = "";
  108. }
  109. if (config.removeImports === undefined) {
  110. config.removeImports = true;
  111. }
  112. if (config.shadowEnabled === undefined) {
  113. config.shadowEnabled = true;
  114. }
  115. if (config.maxResourceSize === undefined) {
  116. config.maxResourceSize = DEFAULT_CONFIG.maxResourceSize;
  117. }
  118. if (config.maxResourceSize === 0) {
  119. config.maxResourceSize = 1;
  120. }
  121. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  122. delete config.removeUnusedCSSRules;
  123. config.removeUnusedStyles = true;
  124. }
  125. if (config.removeAudioSrc === undefined) {
  126. config.removeAudioSrc = true;
  127. }
  128. if (config.removeVideoSrc === undefined) {
  129. config.removeVideoSrc = true;
  130. }
  131. if (config.displayInfobar === undefined) {
  132. config.displayInfobar = true;
  133. }
  134. if (config.backgroundSave === undefined) {
  135. config.backgroundSave = true;
  136. }
  137. if (config.autoSaveDelay === undefined) {
  138. config.autoSaveDelay = DEFAULT_CONFIG.autoSaveDelay;
  139. }
  140. if (config.removeAlternativeFonts === undefined) {
  141. config.removeAlternativeFonts = true;
  142. }
  143. if (config.removeAlternativeMedias === undefined) {
  144. config.removeAlternativeMedias = true;
  145. }
  146. if (config.removeAlternativeImages === undefined) {
  147. if (config.removeAlternativeImages === undefined) {
  148. config.removeAlternativeImages = true;
  149. } else {
  150. config.removeAlternativeImages = config.removeSrcSet;
  151. }
  152. }
  153. if (config.groupDuplicateImages === undefined) {
  154. config.groupDuplicateImages = true;
  155. }
  156. if (config.removeHiddenElements === undefined) {
  157. config.removeHiddenElements = true;
  158. }
  159. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  160. config.autoSaveLoadOrUnload = true;
  161. config.autoSaveLoad = false;
  162. config.autoSaveUnload = false;
  163. }
  164. if (config.confirmFilename === undefined) {
  165. config.confirmFilename = false;
  166. }
  167. if (config.confirmInfobarContent === undefined) {
  168. config.confirmInfobarContent = config.confirmInfobar;
  169. delete config.confirmInfobar;
  170. }
  171. if (config.filenameConflictAction === undefined) {
  172. config.filenameConflictAction = config.conflictAction || DEFAULT_CONFIG.filenameConflictAction;
  173. delete config.conflictAction;
  174. }
  175. if (config.loadDeferredImages === undefined) {
  176. config.loadDeferredImages = config.lazyLoadImages || true;
  177. delete config.lazyLoadImages;
  178. }
  179. if (config.loadDeferredImagesMaxIdleTime === undefined) {
  180. config.loadDeferredImagesMaxIdleTime = config.maxLazyLoadImagesIdleTime || DEFAULT_CONFIG.loadDeferredImagesMaxIdleTime;
  181. delete config.maxLazyLoadImagesIdleTime;
  182. }
  183. }
  184. async function getOptions() {
  185. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  186. return config.profiles[tabsData.profileName || DEFAULT_PROFILE_NAME];
  187. }
  188. async function getConfig() {
  189. await pendingUpgradePromise;
  190. return browser.storage.local.get(["profiles", "rules"]);
  191. }
  192. return {
  193. DISABLED_PROFILE_NAME,
  194. DEFAULT_PROFILE_NAME,
  195. async createProfile(profileName) {
  196. const config = await getConfig();
  197. if (Object.keys(config.profiles).includes(profileName)) {
  198. throw new Error("Duplicate profile name");
  199. }
  200. config.profiles[profileName] = DEFAULT_CONFIG;
  201. await browser.storage.local.set({ profiles: config.profiles });
  202. },
  203. async getProfiles() {
  204. const config = await getConfig();
  205. return config.profiles;
  206. },
  207. async getOptions(profileName, url, autoSave) {
  208. const config = await getConfig();
  209. const urlRule = config.rules.sort((ruleLeft, ruleRight) => ruleRight.url.length - ruleLeft.url.length).find(rule => url && url.includes(rule.url));
  210. return urlRule ? config.profiles[urlRule[autoSave ? "autoSaveProfile" : "profile"]] : config.profiles[profileName || singlefile.config.DEFAULT_PROFILE_NAME];
  211. },
  212. async updateProfile(profileName, profile) {
  213. const config = await getConfig();
  214. if (!Object.keys(config.profiles).includes(profileName)) {
  215. throw new Error("Profile not found");
  216. }
  217. config.profiles[profileName] = profile;
  218. await browser.storage.local.set({ profiles: config.profiles });
  219. },
  220. async renameProfile(oldProfileName, profileName) {
  221. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  222. if (!Object.keys(config.profiles).includes(oldProfileName)) {
  223. throw new Error("Profile not found");
  224. }
  225. if (Object.keys(config.profiles).includes(profileName)) {
  226. throw new Error("Duplicate profile name");
  227. }
  228. if (oldProfileName == DEFAULT_PROFILE_NAME) {
  229. throw new Error("Default settings cannot be renamed");
  230. }
  231. if (tabsData.profileName == oldProfileName) {
  232. tabsData.profileName = profileName;
  233. await singlefile.tabsData.set(tabsData);
  234. }
  235. config.profiles[profileName] = config.profiles[oldProfileName];
  236. config.rules.forEach(rule => {
  237. if (rule.profile == oldProfileName) {
  238. rule.profile = profileName;
  239. }
  240. if (rule.autoSaveProfile == oldProfileName) {
  241. rule.autoSaveProfile = profileName;
  242. }
  243. });
  244. delete config.profiles[oldProfileName];
  245. await browser.storage.local.set({ profiles: config.profiles, rules: config.rules });
  246. },
  247. async deleteProfile(profileName) {
  248. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  249. if (!Object.keys(config.profiles).includes(profileName)) {
  250. throw new Error("Profile not found");
  251. }
  252. if (profileName == DEFAULT_PROFILE_NAME) {
  253. throw new Error("Default settings cannot be deleted");
  254. }
  255. if (tabsData.profileName == profileName) {
  256. delete tabsData.profileName;
  257. await singlefile.tabsData.set(tabsData);
  258. }
  259. config.rules.forEach(rule => {
  260. if (rule.profile == profileName) {
  261. rule.profile = DEFAULT_PROFILE_NAME;
  262. }
  263. if (rule.autoSaveProfile == profileName) {
  264. rule.autoSaveProfile = DEFAULT_PROFILE_NAME;
  265. }
  266. });
  267. delete config.profiles[profileName];
  268. await browser.storage.local.set({ profiles: config.profiles, rules: config.rules });
  269. },
  270. async getRules() {
  271. const config = await getConfig();
  272. return config.rules;
  273. },
  274. async addRule(url, profile, autoSaveProfile) {
  275. if (!url) {
  276. throw new Error("URL is empty");
  277. }
  278. const config = await getConfig();
  279. if (config.rules.find(rule => rule.url == url)) {
  280. throw new Error("URL already exists");
  281. }
  282. config.rules.push({
  283. url,
  284. profile,
  285. autoSaveProfile
  286. });
  287. await browser.storage.local.set({ rules: config.rules });
  288. },
  289. async deleteRule(url) {
  290. if (!url) {
  291. throw new Error("URL is empty");
  292. }
  293. const config = await getConfig();
  294. config.rules = config.rules.filter(rule => rule.url != url);
  295. await browser.storage.local.set({ rules: config.rules });
  296. },
  297. async updateRule(url, newURL, profile, autoSaveProfile) {
  298. if (!url || !newURL) {
  299. throw new Error("URL is empty");
  300. }
  301. const config = await getConfig();
  302. const urlConfig = config.rules.find(rule => rule.url == url);
  303. if (!urlConfig) {
  304. throw new Error("URL not found");
  305. }
  306. if (config.rules.find(rule => rule.url == newURL && rule.url != url)) {
  307. throw new Error("New URL already exists");
  308. }
  309. urlConfig.url = newURL;
  310. urlConfig.profile = profile;
  311. urlConfig.autoSaveProfile = autoSaveProfile;
  312. await browser.storage.local.set({ rules: config.rules });
  313. },
  314. async reset() {
  315. await pendingUpgradePromise;
  316. const tabsData = await singlefile.tabsData.get();
  317. delete tabsData.profileName;
  318. await singlefile.tabsData.set(tabsData);
  319. await browser.storage.local.remove(["profiles", "rules"]);
  320. await browser.storage.local.set({ profiles: { [DEFAULT_PROFILE_NAME]: DEFAULT_CONFIG }, rules: [] });
  321. },
  322. async export() {
  323. const config = await getConfig();
  324. const url = URL.createObjectURL(new Blob([JSON.stringify({ profiles: config.profiles, rules: config.rules }, null, 2)], { type: "text/json" }));
  325. const downloadInfo = {
  326. url,
  327. filename: "singlefile-settings.json",
  328. saveAs: true
  329. };
  330. const downloadId = await browser.downloads.download(downloadInfo);
  331. return new Promise((resolve, reject) => {
  332. browser.downloads.onChanged.addListener(onChanged);
  333. function onChanged(event) {
  334. if (event.id == downloadId && event.state) {
  335. if (event.state.current == "complete") {
  336. URL.revokeObjectURL(url);
  337. resolve({});
  338. browser.downloads.onChanged.removeListener(onChanged);
  339. }
  340. if (event.state.current == "interrupted" && (!event.error || event.error.current != "USER_CANCELED")) {
  341. URL.revokeObjectURL(url);
  342. reject(new Error(event.state.current));
  343. browser.downloads.onChanged.removeListener(onChanged);
  344. }
  345. }
  346. }
  347. });
  348. },
  349. async import(file) {
  350. const reader = new FileReader();
  351. reader.readAsText(file);
  352. const serializedConfig = await new Promise((resolve, reject) => {
  353. reader.addEventListener("load", () => resolve(reader.result), false);
  354. reader.addEventListener("error", reject, false);
  355. });
  356. const config = JSON.parse(serializedConfig);
  357. await browser.storage.local.remove(["profiles", "rules"]);
  358. await browser.storage.local.set({ profiles: config.profiles, rules: config.rules });
  359. }
  360. };
  361. })();