ui-options.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global browser, window, document, localStorage, FileReader, location, fetch, TextDecoder, DOMParser, HTMLElement */
  24. (async () => {
  25. const HELP_ICON_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAABIUlEQVQ4y+2TsarCMBSGvxTBRdqiUZAWOrhJB9EXcPKFfCvfQYfulUKHDqXg4CYUJSioYO4mSDX3ttzt3n87fMlHTpIjlsulxpDZbEYYhgghSNOUOI5Ny2mZYBAELBYLer0eAJ7ncTweKYri4x7LJJRS0u12n7XrukgpjSc0CpVSXK/XZ32/31FKNW85z3PW6zXT6RSAJEnIsqy5UGvNZrNhu90CcDqd+C6tT6J+v//2Th+PB2VZ1hN2Oh3G4zGTyQTbtl/YbrdjtVpxu91+Ljyfz0RRhG3bzOfzF+Y4TvNXvlwuaK2pE4tfzr/wzwsty0IIURlL0998KxRCMBqN8H2/wlzXJQxD2u12vVkeDoeUZUkURRU+GAw4HA7s9/sK+wK6CWHasQ/S/wAAAABJRU5ErkJggg==";
  26. const HELP_PAGE_PATH = "/extension/ui/pages/help.html";
  27. const { DEFAULT_PROFILE_NAME, DISABLED_PROFILE_NAME, CURRENT_PROFILE_NAME } = await browser.runtime.sendMessage({ method: "config.getConstants" });
  28. const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
  29. const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
  30. const removeUnusedFontsLabel = document.getElementById("removeUnusedFontsLabel");
  31. const removeFramesLabel = document.getElementById("removeFramesLabel");
  32. const removeImportsLabel = document.getElementById("removeImportsLabel");
  33. const removeScriptsLabel = document.getElementById("removeScriptsLabel");
  34. const saveRawPageLabel = document.getElementById("saveRawPageLabel");
  35. const saveToClipboardLabel = document.getElementById("saveToClipboardLabel");
  36. const addProofLabel = document.getElementById("addProofLabel");
  37. const saveToGDriveLabel = document.getElementById("saveToGDriveLabel");
  38. const compressHTMLLabel = document.getElementById("compressHTMLLabel");
  39. const compressCSSLabel = document.getElementById("compressCSSLabel");
  40. const loadDeferredImagesLabel = document.getElementById("loadDeferredImagesLabel");
  41. const loadDeferredImagesMaxIdleTimeLabel = document.getElementById("loadDeferredImagesMaxIdleTimeLabel");
  42. const loadDeferredImagesKeepZoomLevelLabel = document.getElementById("loadDeferredImagesKeepZoomLevelLabel");
  43. const addMenuEntryLabel = document.getElementById("addMenuEntryLabel");
  44. const filenameTemplateLabel = document.getElementById("filenameTemplateLabel");
  45. const filenameMaxLengthLabel = document.getElementById("filenameMaxLengthLabel");
  46. const shadowEnabledLabel = document.getElementById("shadowEnabledLabel");
  47. const setMaxResourceSizeLabel = document.getElementById("setMaxResourceSizeLabel");
  48. const maxResourceSizeLabel = document.getElementById("maxResourceSizeLabel");
  49. const confirmFilenameLabel = document.getElementById("confirmFilenameLabel");
  50. const filenameConflictActionLabel = document.getElementById("filenameConflictActionLabel");
  51. const filenameConflictActionUniquifyLabel = document.getElementById("filenameConflictActionUniquifyLabel");
  52. const filenameConflictActionOverwriteLabel = document.getElementById("filenameConflictActionOverwriteLabel");
  53. const filenameConflictActionPromptLabel = document.getElementById("filenameConflictActionPromptLabel");
  54. const filenameConflictActionSkipLabel = document.getElementById("filenameConflictActionSkipLabel");
  55. const removeAudioLabel = document.getElementById("removeAudioLabel");
  56. const removeVideoLabel = document.getElementById("removeVideoLabel");
  57. const displayInfobarLabel = document.getElementById("displayInfobarLabel");
  58. const displayStatsLabel = document.getElementById("displayStatsLabel");
  59. const backgroundSaveLabel = document.getElementById("backgroundSaveLabel");
  60. const applySystemThemeLabel = document.getElementById("applySystemThemeLabel");
  61. const autoSaveDelayLabel = document.getElementById("autoSaveDelayLabel");
  62. const autoSaveLoadLabel = document.getElementById("autoSaveLoadLabel");
  63. const autoSaveUnloadLabel = document.getElementById("autoSaveUnloadLabel");
  64. const autoSaveLoadOrUnloadLabel = document.getElementById("autoSaveLoadOrUnloadLabel");
  65. const autoSaveRepeatLabel = document.getElementById("autoSaveRepeatLabel");
  66. const autoSaveRepeatDelayLabel = document.getElementById("autoSaveRepeatDelayLabel");
  67. const removeAlternativeFontsLabel = document.getElementById("removeAlternativeFontsLabel");
  68. const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
  69. const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
  70. const saveCreatedBookmarksLabel = document.getElementById("saveCreatedBookmarksLabel");
  71. const replaceBookmarkURLLabel = document.getElementById("replaceBookmarkURLLabel");
  72. const titleLabel = document.getElementById("titleLabel");
  73. const userInterfaceLabel = document.getElementById("userInterfaceLabel");
  74. const filenameLabel = document.getElementById("filenameLabel");
  75. const htmlContentLabel = document.getElementById("htmlContentLabel");
  76. const imagesLabel = document.getElementById("imagesLabel");
  77. const stylesheetsLabel = document.getElementById("stylesheetsLabel");
  78. const fontsLabel = document.getElementById("fontsLabel");
  79. const otherResourcesLabel = document.getElementById("otherResourcesLabel");
  80. const autoSaveLabel = document.getElementById("autoSaveLabel");
  81. const autoSettingsLabel = document.getElementById("autoSettingsLabel");
  82. const autoSettingsUrlLabel = document.getElementById("autoSettingsUrlLabel");
  83. const autoSettingsProfileLabel = document.getElementById("autoSettingsProfileLabel");
  84. const autoSettingsAutoSaveProfileLabel = document.getElementById("autoSettingsAutoSaveProfileLabel");
  85. const showAllProfilesLabel = document.getElementById("showAllProfilesLabel");
  86. const showAutoSaveProfileLabel = document.getElementById("showAutoSaveProfileLabel");
  87. const groupDuplicateImagesLabel = document.getElementById("groupDuplicateImagesLabel");
  88. const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
  89. const autoCloseLabel = document.getElementById("autoCloseLabel");
  90. const openEditorLabel = document.getElementById("openEditorLabel");
  91. const autoOpenEditorLabel = document.getElementById("autoOpenEditorLabel");
  92. const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
  93. const includeInfobarLabel = document.getElementById("includeInfobarLabel");
  94. const miscLabel = document.getElementById("miscLabel");
  95. const helpLabel = document.getElementById("helpLabel");
  96. const synchronizeLabel = document.getElementById("synchronizeLabel");
  97. const addProfileButton = document.getElementById("addProfileButton");
  98. const deleteProfileButton = document.getElementById("deleteProfileButton");
  99. const renameProfileButton = document.getElementById("renameProfileButton");
  100. const resetButton = document.getElementById("resetButton");
  101. const exportButton = document.getElementById("exportButton");
  102. const importButton = document.getElementById("importButton");
  103. const fileInput = document.getElementById("fileInput");
  104. const profileNamesInput = document.getElementById("profileNamesInput");
  105. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  106. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  107. const removeUnusedFontsInput = document.getElementById("removeUnusedFontsInput");
  108. const removeFramesInput = document.getElementById("removeFramesInput");
  109. const removeImportsInput = document.getElementById("removeImportsInput");
  110. const removeScriptsInput = document.getElementById("removeScriptsInput");
  111. const saveRawPageInput = document.getElementById("saveRawPageInput");
  112. const saveToClipboardInput = document.getElementById("saveToClipboardInput");
  113. const addProofInput = document.getElementById("addProofInput");
  114. const saveToGDriveInput = document.getElementById("saveToGDriveInput");
  115. const compressHTMLInput = document.getElementById("compressHTMLInput");
  116. const compressCSSInput = document.getElementById("compressCSSInput");
  117. const loadDeferredImagesInput = document.getElementById("loadDeferredImagesInput");
  118. const loadDeferredImagesMaxIdleTimeInput = document.getElementById("loadDeferredImagesMaxIdleTimeInput");
  119. const loadDeferredImagesKeepZoomLevelInput = document.getElementById("loadDeferredImagesKeepZoomLevelInput");
  120. const contextMenuEnabledInput = document.getElementById("contextMenuEnabledInput");
  121. const filenameTemplateInput = document.getElementById("filenameTemplateInput");
  122. const filenameMaxLengthInput = document.getElementById("filenameMaxLengthInput");
  123. const shadowEnabledInput = document.getElementById("shadowEnabledInput");
  124. const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
  125. const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
  126. const confirmFilenameInput = document.getElementById("confirmFilenameInput");
  127. const filenameConflictActionInput = document.getElementById("filenameConflictActionInput");
  128. const removeAudioSrcInput = document.getElementById("removeAudioSrcInput");
  129. const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
  130. const displayInfobarInput = document.getElementById("displayInfobarInput");
  131. const displayStatsInput = document.getElementById("displayStatsInput");
  132. const backgroundSaveInput = document.getElementById("backgroundSaveInput");
  133. const applySystemThemeInput = document.getElementById("applySystemThemeInput");
  134. const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
  135. const autoSaveLoadInput = document.getElementById("autoSaveLoadInput");
  136. const autoSaveUnloadInput = document.getElementById("autoSaveUnloadInput");
  137. const autoSaveLoadOrUnloadInput = document.getElementById("autoSaveLoadOrUnloadInput");
  138. const autoSaveRepeatInput = document.getElementById("autoSaveRepeatInput");
  139. const autoSaveRepeatDelayInput = document.getElementById("autoSaveRepeatDelayInput");
  140. const removeAlternativeFontsInput = document.getElementById("removeAlternativeFontsInput");
  141. const removeAlternativeImagesInput = document.getElementById("removeAlternativeImagesInput");
  142. const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
  143. const saveCreatedBookmarksInput = document.getElementById("saveCreatedBookmarksInput");
  144. const replaceBookmarkURLInput = document.getElementById("replaceBookmarkURLInput");
  145. const groupDuplicateImagesInput = document.getElementById("groupDuplicateImagesInput");
  146. const infobarTemplateInput = document.getElementById("infobarTemplateInput");
  147. const includeInfobarInput = document.getElementById("includeInfobarInput");
  148. const confirmInfobarInput = document.getElementById("confirmInfobarInput");
  149. const autoCloseInput = document.getElementById("autoCloseInput");
  150. const openEditorInput = document.getElementById("openEditorInput");
  151. const autoOpenEditorInput = document.getElementById("autoOpenEditorInput");
  152. const expandAllButton = document.getElementById("expandAllButton");
  153. const rulesDeleteAllButton = document.getElementById("rulesDeleteAllButton");
  154. const ruleUrlInput = document.getElementById("ruleUrlInput");
  155. const ruleProfileInput = document.getElementById("ruleProfileInput");
  156. const ruleAutoSaveProfileInput = document.getElementById("ruleAutoSaveProfileInput");
  157. const ruleEditProfileInput = document.getElementById("ruleEditProfileInput");
  158. const ruleEditAutoSaveProfileInput = document.getElementById("ruleEditAutoSaveProfileInput");
  159. const ruleAddButton = document.getElementById("ruleAddButton");
  160. const rulesElement = document.querySelector(".rules-table");
  161. const rulesContainerElement = document.querySelector(".rules-table-container");
  162. const ruleEditUrlInput = document.getElementById("ruleEditUrlInput");
  163. const ruleEditButton = document.getElementById("ruleEditButton");
  164. const createURLElement = rulesElement.querySelector(".rule-create");
  165. const showAllProfilesInput = document.getElementById("showAllProfilesInput");
  166. const showAutoSaveProfileInput = document.getElementById("showAutoSaveProfileInput");
  167. const synchronizeInput = document.getElementById("synchronizeInput");
  168. const resetAllButton = document.getElementById("resetAllButton");
  169. const resetCurrentButton = document.getElementById("resetCurrentButton");
  170. const resetCancelButton = document.getElementById("resetCancelButton");
  171. const confirmButton = document.getElementById("confirmButton");
  172. const cancelButton = document.getElementById("cancelButton");
  173. const promptInput = document.getElementById("promptInput");
  174. const promptCancelButton = document.getElementById("promptCancelButton");
  175. const promptConfirmButton = document.getElementById("promptConfirmButton");
  176. let sidePanelDisplay;
  177. if (location.href.endsWith("#side-panel")) {
  178. sidePanelDisplay = true;
  179. document.querySelector(".options-title").remove();
  180. document.documentElement.classList.add("side-panel");
  181. }
  182. browser.runtime.onMessage.addListener(message => {
  183. if (message.method == "options.refresh" || (message.method == "options.refreshPanel" && sidePanelDisplay)) {
  184. refresh(message.profileName);
  185. }
  186. });
  187. let pendingSave = Promise.resolve();
  188. let autoSaveProfileChanged;
  189. ruleProfileInput.onchange = () => {
  190. if (!autoSaveProfileChanged && ruleProfileInput.value != CURRENT_PROFILE_NAME) {
  191. ruleAutoSaveProfileInput.value = ruleProfileInput.value;
  192. }
  193. };
  194. ruleAutoSaveProfileInput.onchange = () => {
  195. autoSaveProfileChanged = true;
  196. };
  197. rulesDeleteAllButton.addEventListener("click", async () => {
  198. if (await confirm(browser.i18n.getMessage("optionsDeleteDisplayedRulesConfirm"), "flex-end")) {
  199. await browser.runtime.sendMessage({ method: "config.deleteRules", profileName: !showAllProfilesInput.checked && profileNamesInput.value });
  200. await refresh();
  201. await refreshExternalComponents();
  202. }
  203. }, false);
  204. createURLElement.onsubmit = async event => {
  205. event.preventDefault();
  206. try {
  207. await browser.runtime.sendMessage({ method: "config.addRule", url: ruleUrlInput.value, profileName: ruleProfileInput.value, autoSaveProfileName: ruleAutoSaveProfileInput.value });
  208. } catch (error) {
  209. // ignored
  210. }
  211. ruleUrlInput.value = "";
  212. ruleProfileInput.value = ruleAutoSaveProfileInput.value = DEFAULT_PROFILE_NAME;
  213. autoSaveProfileChanged = false;
  214. await refresh();
  215. await refreshExternalComponents();
  216. ruleUrlInput.focus();
  217. };
  218. ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
  219. ruleAddButton.disabled = !ruleUrlInput.value;
  220. const rules = await browser.runtime.sendMessage({ method: "config.getRules" });
  221. if (rules.find(rule => rule.url == ruleUrlInput.value)) {
  222. ruleAddButton.disabled = true;
  223. }
  224. };
  225. ruleEditUrlInput.onclick = ruleEditUrlInput.onkeyup = ruleEditUrlInput.onchange = async () => {
  226. ruleEditButton.disabled = !ruleEditUrlInput.value;
  227. const rules = await browser.runtime.sendMessage({ method: "config.getRules" });
  228. if (rules.find(rule => rule.url == ruleEditUrlInput.value)) {
  229. ruleEditButton.disabled = true;
  230. }
  231. };
  232. if (getLocalStorageItem("optionShowAutoSaveProfile")) {
  233. showAutoSaveProfileInput.checked = true;
  234. rulesContainerElement.classList.remove("compact");
  235. }
  236. showAutoSaveProfileInput.addEventListener("click", () => {
  237. if (showAutoSaveProfileInput.checked) {
  238. setLocalStorageItem("optionShowAutoSaveProfile", 1);
  239. rulesContainerElement.classList.remove("compact");
  240. } else {
  241. removeLocalStorageItem("optionShowAutoSaveProfile");
  242. rulesContainerElement.classList.add("compact");
  243. }
  244. }, false);
  245. if (getLocalStorageItem("optionShowAllProfiles")) {
  246. showAllProfilesInput.checked = true;
  247. }
  248. showAllProfilesInput.addEventListener("click", () => {
  249. if (showAllProfilesInput.checked) {
  250. setLocalStorageItem("optionShowAllProfiles", 1);
  251. } else {
  252. removeLocalStorageItem("optionShowAllProfiles");
  253. }
  254. }, false);
  255. addProfileButton.addEventListener("click", async () => {
  256. const profileName = await prompt(browser.i18n.getMessage("profileAddPrompt"), "flex-start");
  257. if (profileName) {
  258. try {
  259. await browser.runtime.sendMessage({ method: "config.createProfile", profileName, fromProfileName: profileNamesInput.value });
  260. } catch (error) {
  261. // ignored
  262. }
  263. if (sidePanelDisplay) {
  264. await refresh();
  265. } else {
  266. await refresh(profileName);
  267. }
  268. await refreshExternalComponents();
  269. }
  270. }, false);
  271. deleteProfileButton.addEventListener("click", async () => {
  272. if (await confirm(browser.i18n.getMessage("profileDeleteConfirm"), "flex-start")) {
  273. try {
  274. await browser.runtime.sendMessage({ method: "config.deleteProfile", profileName: profileNamesInput.value });
  275. } catch (error) {
  276. // ignored
  277. }
  278. profileNamesInput.value = null;
  279. await refresh();
  280. await refreshExternalComponents();
  281. }
  282. }, false);
  283. renameProfileButton.addEventListener("click", async () => {
  284. const profileName = await prompt(browser.i18n.getMessage("profileRenamePrompt"), "flex-start", profileNamesInput.value);
  285. if (profileName) {
  286. try {
  287. await browser.runtime.sendMessage({ method: "config.renameProfile", profileName: profileNamesInput.value, newProfileName: profileName });
  288. } catch (error) {
  289. // ignored
  290. }
  291. await refresh(profileName);
  292. await refreshExternalComponents();
  293. }
  294. }, false);
  295. resetButton.addEventListener("click", async () => {
  296. const choice = await reset();
  297. if (choice) {
  298. if (choice == "all") {
  299. await browser.runtime.sendMessage({ method: "config.resetProfiles" });
  300. await refresh(DEFAULT_PROFILE_NAME);
  301. await refreshExternalComponents();
  302. }
  303. if (choice == "current") {
  304. await browser.runtime.sendMessage({ method: "config.resetProfile", profileName: profileNamesInput.value });
  305. await refresh();
  306. await refreshExternalComponents();
  307. }
  308. await update();
  309. }
  310. }, false);
  311. exportButton.addEventListener("click", async () => {
  312. await browser.runtime.sendMessage({ method: "config.exportConfig" });
  313. }, false);
  314. importButton.addEventListener("click", () => {
  315. fileInput.onchange = async () => {
  316. if (fileInput.files.length) {
  317. const reader = new FileReader();
  318. reader.readAsText(fileInput.files[0]);
  319. const serializedConfig = await new Promise((resolve, reject) => {
  320. reader.addEventListener("load", () => resolve(reader.result), false);
  321. reader.addEventListener("error", reject, false);
  322. });
  323. const config = JSON.parse(serializedConfig);
  324. await browser.runtime.sendMessage({ method: "config.importConfig", config });
  325. await refresh(DEFAULT_PROFILE_NAME);
  326. await refreshExternalComponents();
  327. fileInput.value = "";
  328. }
  329. };
  330. fileInput.click();
  331. }, false);
  332. autoSaveUnloadInput.addEventListener("click", async () => {
  333. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  334. autoSaveLoadOrUnloadInput.checked = true;
  335. }
  336. }, false);
  337. autoSaveLoadInput.addEventListener("click", async () => {
  338. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  339. autoSaveLoadOrUnloadInput.checked = true;
  340. }
  341. }, false);
  342. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  343. if (autoSaveLoadOrUnloadInput.checked) {
  344. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  345. } else {
  346. autoSaveUnloadInput.checked = false;
  347. autoSaveLoadInput.checked = true;
  348. }
  349. }, false);
  350. expandAllButton.addEventListener("click", () => {
  351. if (expandAllButton.className) {
  352. expandAllButton.className = "";
  353. } else {
  354. expandAllButton.className = "opened";
  355. }
  356. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  357. }, false);
  358. removeScriptsInput.addEventListener("click", () => {
  359. if (!removeScriptsInput.checked) {
  360. removeHiddenElementsInput.checked = false;
  361. removeUnusedStylesInput.checked = false;
  362. }
  363. }, false);
  364. saveCreatedBookmarksInput.addEventListener("click", saveCreatedBookmarks, false);
  365. saveToGDriveInput.addEventListener("click", async () => {
  366. if (!saveToGDriveInput.checked) {
  367. await browser.runtime.sendMessage({ method: "downloads.disableGDrive" });
  368. }
  369. }, false);
  370. addProofInput.addEventListener("click", async () => {
  371. if (addProofInput.checked) {
  372. addProofInput.checked = false;
  373. if (await confirm(browser.i18n.getMessage("optionsAddProofConfirm"))) {
  374. addProofInput.checked = true;
  375. }
  376. await update();
  377. }
  378. });
  379. synchronizeInput.checked = (await browser.runtime.sendMessage({ method: "config.isSync" })).sync;
  380. synchronizeInput.addEventListener("click", async () => {
  381. if (synchronizeInput.checked) {
  382. await browser.runtime.sendMessage({ method: "config.enableSync" });
  383. await refresh(DEFAULT_PROFILE_NAME);
  384. } else {
  385. await browser.runtime.sendMessage({ method: "config.disableSync" });
  386. await refresh();
  387. }
  388. }, false);
  389. document.body.onchange = async event => {
  390. let target = event.target;
  391. if (target != ruleUrlInput &&
  392. target != ruleProfileInput &&
  393. target != ruleAutoSaveProfileInput &&
  394. target != ruleEditUrlInput &&
  395. target != ruleEditProfileInput &&
  396. target != ruleEditAutoSaveProfileInput &&
  397. target != showAutoSaveProfileInput &&
  398. target != saveCreatedBookmarksInput) {
  399. if (target != profileNamesInput && target != showAllProfilesInput) {
  400. await update();
  401. }
  402. if (target == profileNamesInput) {
  403. await refresh(profileNamesInput.value);
  404. if (sidePanelDisplay) {
  405. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  406. tabsData.profileName = profileNamesInput.value;
  407. await browser.runtime.sendMessage({ method: "tabsData.set", tabsData: tabsData });
  408. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  409. }
  410. } else {
  411. if (target == contextMenuEnabledInput) {
  412. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  413. }
  414. if (target == openEditorInput) {
  415. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  416. }
  417. await refresh();
  418. }
  419. }
  420. };
  421. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  422. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  423. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  424. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  425. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  426. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  427. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  428. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  429. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  430. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  431. saveToClipboardLabel.textContent = browser.i18n.getMessage("optionSaveToClipboard");
  432. addProofLabel.textContent = browser.i18n.getMessage("optionAddProof");
  433. saveToGDriveLabel.textContent = browser.i18n.getMessage("optionSaveToGDrive");
  434. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  435. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  436. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  437. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  438. loadDeferredImagesKeepZoomLevelLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesKeepZoomLevel");
  439. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  440. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  441. filenameMaxLengthLabel.textContent = browser.i18n.getMessage("optionFilenameMaxLength");
  442. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  443. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  444. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  445. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  446. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  447. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  448. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  449. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  450. filenameConflictActionSkipLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionSkip");
  451. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  452. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  453. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  454. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  455. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  456. applySystemThemeLabel.textContent = browser.i18n.getMessage("optionApplySystemTheme");
  457. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  458. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  459. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  460. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  461. autoSaveRepeatLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeat");
  462. autoSaveRepeatDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveRepeatDelay");
  463. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  464. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  465. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  466. saveCreatedBookmarksLabel.textContent = browser.i18n.getMessage("optionSaveCreatedBookmarks");
  467. replaceBookmarkURLLabel.textContent = browser.i18n.getMessage("optionReplaceBookmarkURL");
  468. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  469. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  470. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  471. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  472. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  473. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  474. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  475. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  476. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  477. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  478. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  479. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  480. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  481. includeInfobarLabel.textContent = browser.i18n.getMessage("optionIncludeInfobar");
  482. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  483. autoCloseLabel.textContent = browser.i18n.getMessage("optionAutoClose");
  484. openEditorLabel.textContent = browser.i18n.getMessage("optionOpenEditor");
  485. autoOpenEditorLabel.textContent = browser.i18n.getMessage("optionAutoOpenEditor");
  486. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  487. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  488. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  489. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  490. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  491. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  492. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  493. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  494. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  495. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  496. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  497. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  498. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  499. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  500. synchronizeLabel.textContent = browser.i18n.getMessage("optionSynchronize");
  501. resetAllButton.textContent = browser.i18n.getMessage("optionsResetAllButton");
  502. resetCurrentButton.textContent = browser.i18n.getMessage("optionsResetCurrentButton");
  503. resetCancelButton.textContent = promptCancelButton.textContent = cancelButton.textContent = browser.i18n.getMessage("optionsCancelButton");
  504. confirmButton.textContent = promptConfirmButton.textContent = browser.i18n.getMessage("optionsOKButton");
  505. document.getElementById("resetConfirmLabel").textContent = browser.i18n.getMessage("optionsResetConfirm");
  506. if (location.href.endsWith("#")) {
  507. document.querySelector(".new-window-link").remove();
  508. document.documentElement.classList.add("maximized");
  509. }
  510. const tabsData = await browser.runtime.sendMessage({ method: "tabsData.get" });
  511. getHelpContents();
  512. refresh(tabsData.profileName);
  513. async function refresh(profileName) {
  514. const [profiles, rules] = await Promise.all([browser.runtime.sendMessage({ method: "config.getProfiles" }), browser.runtime.sendMessage({ method: "config.getRules" })]);
  515. const selectedProfileName = profileName || profileNamesInput.value || DEFAULT_PROFILE_NAME;
  516. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  517. profileNamesInput.options.length = 0;
  518. ruleProfileInput.options.length = 0;
  519. ruleAutoSaveProfileInput.options.length = 0;
  520. ruleEditProfileInput.options.length = 0;
  521. ruleEditAutoSaveProfileInput.options.length = 0;
  522. let optionElement = document.createElement("option");
  523. optionElement.value = DEFAULT_PROFILE_NAME;
  524. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  525. [CURRENT_PROFILE_NAME].concat(...Object.keys(profiles)).forEach(profileName => {
  526. const optionElement = document.createElement("option");
  527. optionElement.value = optionElement.textContent = profileName;
  528. if (profileName == DEFAULT_PROFILE_NAME) {
  529. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  530. }
  531. if (profileName != CURRENT_PROFILE_NAME) {
  532. profileNamesInput.appendChild(optionElement);
  533. }
  534. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  535. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  536. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  537. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  538. });
  539. profileNamesInput.disabled = profileNamesInput.options.length == 1;
  540. optionElement = document.createElement("option");
  541. optionElement.value = DISABLED_PROFILE_NAME;
  542. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  543. ruleAutoSaveProfileInput.appendChild(optionElement);
  544. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  545. const rulesDataElement = rulesElement.querySelector(".rules-data");
  546. Array.from(rulesDataElement.childNodes).forEach(node => (!node.className || !node.className.includes("rule-edit")) && node.remove());
  547. const editURLElement = rulesElement.querySelector(".rule-edit");
  548. createURLElement.hidden = false;
  549. editURLElement.hidden = true;
  550. ruleProfileInput.value = ruleAutoSaveProfileInput.value = selectedProfileName;
  551. let rulesDisplayed;
  552. rules.forEach(rule => {
  553. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  554. rulesDisplayed = true;
  555. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  556. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  557. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  558. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  559. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  560. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  561. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  562. ruleElement.hidden = false;
  563. ruleElement.className = "tr data";
  564. rulesDataElement.appendChild(ruleElement);
  565. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  566. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  567. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  568. ruleDeleteButton.addEventListener("click", async () => {
  569. if (await confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"), "flex-end")) {
  570. await browser.runtime.sendMessage({ method: "config.deleteRule", url: rule.url });
  571. await refresh();
  572. await refreshExternalComponents();
  573. }
  574. }, false);
  575. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  576. ruleUpdateButton.addEventListener("click", async () => {
  577. if (editURLElement.hidden) {
  578. createURLElement.hidden = true;
  579. editURLElement.hidden = false;
  580. rulesDataElement.replaceChild(editURLElement, ruleElement);
  581. ruleEditUrlInput.value = rule.url;
  582. ruleEditProfileInput.value = rule.profile;
  583. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  584. ruleEditUrlInput.focus();
  585. editURLElement.onsubmit = async event => {
  586. event.preventDefault();
  587. rulesElement.appendChild(editURLElement);
  588. await browser.runtime.sendMessage({ method: "config.updateRule", url: rule.url, newUrl: ruleEditUrlInput.value, profileName: ruleEditProfileInput.value, autoSaveProfileName: ruleEditAutoSaveProfileInput.value });
  589. await refresh();
  590. await refreshExternalComponents();
  591. ruleUrlInput.focus();
  592. };
  593. }
  594. }, false);
  595. }
  596. });
  597. rulesDeleteAllButton.disabled = !rulesDisplayed;
  598. rulesElement.appendChild(createURLElement);
  599. profileNamesInput.value = selectedProfileName;
  600. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == DEFAULT_PROFILE_NAME;
  601. const profileOptions = profiles[selectedProfileName];
  602. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements || profileOptions.saveRawPage;
  603. removeHiddenElementsInput.disabled = profileOptions.saveRawPage;
  604. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  605. removeUnusedFontsInput.checked = profileOptions.removeUnusedFonts;
  606. removeFramesInput.checked = profileOptions.removeFrames || profileOptions.saveRawPage;
  607. removeFramesInput.disabled = profileOptions.saveRawPage;
  608. removeImportsInput.checked = profileOptions.removeImports;
  609. removeScriptsInput.checked = profileOptions.removeScripts;
  610. saveRawPageInput.checked = profileOptions.saveRawPage;
  611. saveToClipboardInput.checked = profileOptions.saveToClipboard && !profileOptions.saveToGDrive;
  612. saveToClipboardInput.disabled = profileOptions.saveToGDrive;
  613. addProofInput.checked = profileOptions.addProof;
  614. saveToGDriveInput.checked = profileOptions.saveToGDrive && !profileOptions.saveToClipboard;
  615. saveToGDriveInput.disabled = profileOptions.saveToClipboard;
  616. compressHTMLInput.checked = profileOptions.compressHTML;
  617. compressCSSInput.checked = profileOptions.compressCSS;
  618. loadDeferredImagesInput.checked = profileOptions.loadDeferredImages && !profileOptions.saveRawPage;
  619. loadDeferredImagesInput.disabled = profileOptions.saveRawPage;
  620. loadDeferredImagesMaxIdleTimeInput.value = profileOptions.loadDeferredImagesMaxIdleTime;
  621. loadDeferredImagesKeepZoomLevelInput.checked = profileOptions.loadDeferredImagesKeepZoomLevel && !profileOptions.saveRawPage;
  622. loadDeferredImagesKeepZoomLevelInput.disabled = !profileOptions.loadDeferredImages || profileOptions.saveRawPape;
  623. loadDeferredImagesMaxIdleTimeInput.disabled = !profileOptions.loadDeferredImages || profileOptions.saveRawPage;
  624. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  625. filenameTemplateInput.value = profileOptions.filenameTemplate;
  626. filenameMaxLengthInput.value = profileOptions.filenameMaxLength;
  627. filenameTemplateInput.disabled = profileOptions.saveToClipboard;
  628. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  629. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  630. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  631. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  632. confirmFilenameInput.checked = profileOptions.confirmFilename;
  633. confirmFilenameInput.disabled = profileOptions.saveToClipboard;
  634. filenameConflictActionInput.value = profileOptions.filenameConflictAction;
  635. filenameConflictActionInput.disabled = profileOptions.saveToClipboard || profileOptions.saveToGDrive;
  636. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  637. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  638. displayInfobarInput.checked = profileOptions.displayInfobar;
  639. displayStatsInput.checked = profileOptions.displayStats;
  640. backgroundSaveInput.checked = profileOptions.backgroundSave;
  641. backgroundSaveInput.disabled = profileOptions.saveToGDrive;
  642. applySystemThemeInput.checked = profileOptions.applySystemTheme;
  643. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  644. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  645. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  646. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  647. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  648. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  649. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  650. autoSaveRepeatInput.checked = profileOptions.autoSaveRepeat;
  651. autoSaveRepeatInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  652. autoSaveRepeatDelayInput.value = profileOptions.autoSaveRepeatDelay;
  653. autoSaveRepeatDelayInput.disabled = !profileOptions.autoSaveRepeat;
  654. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  655. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  656. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  657. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  658. saveCreatedBookmarksInput.checked = profileOptions.saveCreatedBookmarks;
  659. replaceBookmarkURLInput.checked = profileOptions.saveCreatedBookmarks && profileOptions.backgroundSave && profileOptions.replaceBookmarkURL;
  660. replaceBookmarkURLInput.disabled = !profileOptions.saveCreatedBookmarks || !profileOptions.backgroundSave || profileOptions.saveToClipboard || profileOptions.saveToGDrive;
  661. infobarTemplateInput.value = profileOptions.infobarTemplate;
  662. includeInfobarInput.checked = profileOptions.includeInfobar;
  663. confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
  664. autoCloseInput.checked = profileOptions.autoClose;
  665. openEditorInput.checked = profileOptions.openEditor;
  666. autoOpenEditorInput.checked = profileOptions.autoOpenEditor;
  667. removeFramesInput.disabled = saveRawPageInput.checked;
  668. removeFramesInput.checked = removeFramesInput.checked || saveRawPageInput.checked;
  669. loadDeferredImagesInput.disabled = saveRawPageInput.checked;
  670. if (saveRawPageInput.checked) {
  671. loadDeferredImagesInput.checked = false;
  672. }
  673. }
  674. function getProfileText(profileName) {
  675. return profileName == DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  676. }
  677. async function update() {
  678. await pendingSave;
  679. pendingSave = browser.runtime.sendMessage({
  680. method: "config.updateProfile",
  681. profileName: profileNamesInput.value,
  682. profile: {
  683. removeHiddenElements: removeHiddenElementsInput.checked,
  684. removeUnusedStyles: removeUnusedStylesInput.checked,
  685. removeUnusedFonts: removeUnusedFontsInput.checked,
  686. removeFrames: removeFramesInput.checked,
  687. removeImports: removeImportsInput.checked,
  688. removeScripts: removeScriptsInput.checked,
  689. saveRawPage: saveRawPageInput.checked,
  690. saveToClipboard: saveToClipboardInput.checked,
  691. addProof: addProofInput.checked,
  692. saveToGDrive: saveToGDriveInput.checked,
  693. compressHTML: compressHTMLInput.checked,
  694. compressCSS: compressCSSInput.checked,
  695. loadDeferredImages: loadDeferredImagesInput.checked,
  696. loadDeferredImagesMaxIdleTime: Math.max(loadDeferredImagesMaxIdleTimeInput.value, 0),
  697. loadDeferredImagesKeepZoomLevel: loadDeferredImagesKeepZoomLevelInput.checked,
  698. contextMenuEnabled: contextMenuEnabledInput.checked,
  699. filenameTemplate: filenameTemplateInput.value,
  700. filenameMaxLength: filenameMaxLengthInput.value,
  701. shadowEnabled: shadowEnabledInput.checked,
  702. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  703. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  704. confirmFilename: confirmFilenameInput.checked,
  705. filenameConflictAction: filenameConflictActionInput.value,
  706. removeAudioSrc: removeAudioSrcInput.checked,
  707. removeVideoSrc: removeVideoSrcInput.checked,
  708. displayInfobar: displayInfobarInput.checked,
  709. displayStats: displayStatsInput.checked,
  710. backgroundSave: backgroundSaveInput.checked,
  711. applySystemTheme: applySystemThemeInput.checked,
  712. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  713. autoSaveLoad: autoSaveLoadInput.checked,
  714. autoSaveUnload: autoSaveUnloadInput.checked,
  715. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  716. autoSaveRepeat: autoSaveRepeatInput.checked,
  717. autoSaveRepeatDelay: Math.max(autoSaveRepeatDelayInput.value, 1),
  718. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  719. removeAlternativeImages: removeAlternativeImagesInput.checked,
  720. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  721. saveCreatedBookmarks: saveCreatedBookmarksInput.checked,
  722. replaceBookmarkURL: replaceBookmarkURLInput.checked,
  723. groupDuplicateImages: groupDuplicateImagesInput.checked,
  724. infobarTemplate: infobarTemplateInput.value,
  725. includeInfobar: includeInfobarInput.checked,
  726. confirmInfobarContent: confirmInfobarInput.checked,
  727. autoClose: autoCloseInput.checked,
  728. openEditor: openEditorInput.checked,
  729. autoOpenEditor: autoOpenEditorInput.checked
  730. }
  731. });
  732. await pendingSave;
  733. }
  734. async function refreshExternalComponents() {
  735. try {
  736. await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
  737. if (sidePanelDisplay) {
  738. await browser.runtime.sendMessage({ method: "options.refresh", profileName: profileNamesInput.value });
  739. } else {
  740. await browser.runtime.sendMessage({ method: "options.refreshPanel", profileName: profileNamesInput.value });
  741. }
  742. } catch (error) {
  743. // ignored
  744. }
  745. }
  746. async function saveCreatedBookmarks() {
  747. if (saveCreatedBookmarksInput.checked) {
  748. saveCreatedBookmarksInput.checked = false;
  749. try {
  750. const permissionGranted = await browser.permissions.request({ permissions: ["bookmarks"] });
  751. if (permissionGranted) {
  752. saveCreatedBookmarksInput.checked = true;
  753. await update();
  754. await refresh();
  755. await browser.runtime.sendMessage({ method: "bookmarks.saveCreatedBookmarks" });
  756. } else {
  757. await disableOption();
  758. }
  759. } catch (error) {
  760. await disableOption();
  761. }
  762. } else {
  763. await disableOption();
  764. }
  765. async function disableOption() {
  766. await update();
  767. await refresh();
  768. await browser.runtime.sendMessage({ method: "bookmarks.disable" });
  769. }
  770. }
  771. async function confirm(message, position = "center") {
  772. document.getElementById("confirmLabel").textContent = message;
  773. document.getElementById("formConfirmContainer").style.setProperty("display", "flex");
  774. document.querySelector("#formConfirmContainer .popup-content").style.setProperty("align-self", position);
  775. confirmButton.focus();
  776. document.body.style.setProperty("overflow-y", "hidden");
  777. return new Promise(resolve => {
  778. confirmButton.onclick = event => hideAndResolve(event, true);
  779. cancelButton.onclick = event => hideAndResolve(event);
  780. window.onkeyup = event => {
  781. if (event.key == "Escape") {
  782. hideAndResolve(event);
  783. }
  784. };
  785. function hideAndResolve(event, value) {
  786. event.preventDefault();
  787. document.getElementById("formConfirmContainer").style.setProperty("display", "none");
  788. document.body.style.setProperty("overflow-y", "");
  789. resolve(value);
  790. }
  791. });
  792. }
  793. async function reset() {
  794. document.getElementById("formResetContainer").style.setProperty("display", "flex");
  795. resetCancelButton.focus();
  796. document.body.style.setProperty("overflow-y", "hidden");
  797. return new Promise(resolve => {
  798. resetAllButton.onclick = event => hideAndResolve(event, "all");
  799. resetCurrentButton.onclick = event => hideAndResolve(event, "current");
  800. resetCancelButton.onclick = event => hideAndResolve(event);
  801. window.onkeyup = event => {
  802. if (event.key == "Escape") {
  803. hideAndResolve(event);
  804. }
  805. };
  806. function hideAndResolve(event, value) {
  807. event.preventDefault();
  808. document.getElementById("formResetContainer").style.setProperty("display", "none");
  809. document.body.style.setProperty("overflow-y", "");
  810. resolve(value);
  811. }
  812. });
  813. }
  814. async function prompt(message, position = "center", defaultValue = "") {
  815. document.getElementById("promptLabel").textContent = message;
  816. document.getElementById("formPromptContainer").style.setProperty("display", "flex");
  817. document.querySelector("#formPromptContainer .popup-content").style.setProperty("align-self", position);
  818. promptInput.value = defaultValue;
  819. promptInput.focus();
  820. document.body.style.setProperty("overflow-y", "hidden");
  821. return new Promise(resolve => {
  822. promptConfirmButton.onclick = event => hideAndResolve(event, promptInput.value);
  823. promptCancelButton.onclick = event => hideAndResolve(event);
  824. window.onkeyup = event => {
  825. if (event.key == "Escape") {
  826. hideAndResolve(event);
  827. }
  828. };
  829. function hideAndResolve(event, value) {
  830. event.preventDefault();
  831. document.getElementById("formPromptContainer").style.setProperty("display", "none");
  832. document.body.style.setProperty("overflow-y", "");
  833. resolve(value);
  834. }
  835. });
  836. }
  837. async function getHelpContents() {
  838. const helpPage = await fetch(browser.runtime.getURL(HELP_PAGE_PATH));
  839. const content = new TextDecoder().decode(await helpPage.arrayBuffer());
  840. const doc = (new DOMParser()).parseFromString(content, "text/html");
  841. const items = doc.querySelectorAll("[data-options-label]");
  842. items.forEach(itemElement => {
  843. const optionLabel = document.getElementById(itemElement.dataset.optionsLabel);
  844. const helpIconContainer = document.createElement("span");
  845. const helpIcon = document.createElement("img");
  846. helpIcon.src = HELP_ICON_URL;
  847. helpIconContainer.className = "help-icon";
  848. helpIconContainer.onclick = () => {
  849. helpContent.hidden = !helpContent.hidden;
  850. return false;
  851. };
  852. helpIcon.tabIndex = 0;
  853. helpIconContainer.onkeyup = event => {
  854. if (event.code == "Enter") {
  855. helpContent.hidden = !helpContent.hidden;
  856. return false;
  857. }
  858. };
  859. helpIconContainer.appendChild(helpIcon);
  860. optionLabel.appendChild(helpIconContainer);
  861. const helpContent = document.createElement("div");
  862. helpContent.hidden = true;
  863. helpContent.className = "help-content";
  864. itemElement.childNodes.forEach(node => {
  865. if (node instanceof HTMLElement && node.className != "option") {
  866. helpContent.appendChild(document.importNode(node, true));
  867. }
  868. });
  869. helpContent.querySelectorAll("a[href]").forEach(linkElement => {
  870. const hrefValue = linkElement.getAttribute("href");
  871. if (hrefValue.startsWith("#")) {
  872. linkElement.href = browser.runtime.getURL(HELP_PAGE_PATH + linkElement.getAttribute("href"));
  873. linkElement.target = "_blank";
  874. }
  875. });
  876. optionLabel.parentElement.insertAdjacentElement("afterEnd", helpContent);
  877. });
  878. }
  879. function getLocalStorageItem(key) {
  880. try {
  881. return localStorage.getItem(key);
  882. } catch (error) {
  883. // ignored
  884. }
  885. }
  886. function setLocalStorageItem(key, value) {
  887. try {
  888. return localStorage.setItem(key, value);
  889. } catch (error) {
  890. // ignored
  891. }
  892. }
  893. function removeLocalStorageItem(key) {
  894. try {
  895. return localStorage.removeItem(key);
  896. } catch (error) {
  897. // ignored
  898. }
  899. }
  900. })();